You are on page 1of 25

Object Oriented Programming (C#) (CSE 321)

Lecture 6

C# Inheritance

Prepared by:
Dr. Ahmed Samy Moursi
Computer Science and Engineering Department
https://ahmedhashwa.github.io/

1
Quote of the day (1)

Lecture 6
CSE 321 Object Oriented Programming (C#) 2
Agenda

Lecture 6
CSE 321 Object Oriented Programming (C#) 3
What is
Inheritance
What is Inheritance?

Lecture 6
◼ Inheritance is a key concept in software development.
◼ It’s a form of software reuse where a new class, known as
the derived class, is created from an existing class,
referred to as the base class.

CSE 321 Object Oriented Programming (C#) 5


How Does It Work?

Lecture 6
◼ The derived class inherits the members of the base class.
◼ It can enhance them with new or modified capabilities.
◼ This allows for software reuse, saving time during app
development and increasing the likelihood of effective
system implementation.

CSE 321 Object Oriented Programming (C#) 6


What Does a Derived Class Do?

Lecture 6
◼ The derived class typically adds its own fields, properties,
and methods.
◼ It makes the derived class more specific than its base
class.
◼ It represents a more specialized group of objects.
◼ It exhibits the behaviors of its base class along with
additional behaviors specific to itself.

CSE 321 Object Oriented Programming (C#) 7


Example of Inheritance

Lecture 6
◼ Consider a human father (base class) passing on most of
his qualities to his child (derived class).
◼ The child might modify these qualities or add new
capabilities not present in the father.

CSE 321 Object Oriented Programming (C#) 8


Understanding the Class Hierarchy

Lecture 6
◼ In the class hierarchy, the direct base class is the one from
which a derived class explicitly inherits.
◼ Any class above the direct base class in the hierarchy is an
indirect base class.
◼ The hierarchy begins with the class object, a C# keyword
that’s an alias for System.Object in the Framework Class
Library.

CSE 321 Object Oriented Programming (C#) 9


What is inheritance

Lecture 6
◼ In single inheritance, a class is derived from one direct base
class.
◼ C# supports only single inheritance.
◼ With object-oriented programming, you can, when
appropriate, focus on the commonalities among objects in the
system rather than the special cases.

CSE 321 Object Oriented Programming (C#) 10


Quote of the day (4)

Lecture 6
CSE 321 Object Oriented Programming (C#) 11
More on
Inheritance
OOP Relationships

Lecture 6
◼ Is-a represents inheritance.
◼ In an is-a relationship, an object of a derived class also can be
treated as an object of its base class. For example, a car is a
vehicle, and a truck is a vehicle.
◼ By contrast, has-a represents composition.
◼ In a has-a relationship, an object contains as members
references to other objects.
◼ For example, a car has a steering wheel, and a car object has a
reference to a steering-wheel object.

CSE 321 Object Oriented Programming (C#) 13


Inheritance remarks

Lecture 6
◼ New classes can inherit from classes in class libraries.
◼ Organizations develop their own class libraries and can take
advantage of others available worldwide.
◼ A derived class can customize methods it inherits from its base
class. In such cases, the derived class can override (redefine)
the base-class method with an appropriate implementation

CSE 321 Object Oriented Programming (C#) 14


Base Classes and Derived Classes

Lecture 6
◼ base classes tend to be more general, and derived classes tend to be more specific.
◼ Examples:
Base class Derived classes
Student GraduateStudent, UndergraduateStudent

Shape Circle, Triangle, Rectangle

Loan CarLoan, HomeImprovementLoan, MortgageLoan

Employee Faculty, Staff, HourlyWorker, CommissionWorker

SpaceObject Star, Moon, Planet, FlyingSaucer

BankAccount CheckingAccount, SavingsAccount

CSE 321 Object Oriented Programming (C#) 15


Inheritance in UML

Lecture 6
CSE 321 Object Oriented Programming (C#) 16
Inheritance in UML

Lecture 6
CSE 321 Object Oriented Programming (C#) 17
protected Members

Lecture 6
◼ A class’s public members are accessible wherever the app has a reference to an
object of that class or one of its derived classes.
◼ A class’s private members are accessible only within the class itself.
◼ A base class’s private members are inherited by its derived classes but are not
directly accessible by derived-class methods and properties.
◼ Using protected access offers an intermediate level of access between public and
private. A base class’s protected members can be accessed by members of that
base class and by members of its derived classes, but not by clients of the class.
◼ All non-private base-class members retain their original access modifier when they
become members of the derived class—public members of the base class become
public members of the derived class, and protected members of the base class
become protected members of the derived class.
◼ Derived-class methods can refer to public and protected members inherited from
the base class simply by using the member names. When a derived-class method
overrides a base-class method, the base-class version can be accessed from the
derived class by preceding the base-class method name with the keyword base and
the member access (.) operator.

CSE 321 Object Oriented Programming (C#) 18


Quote of the day (2)

Lecture 6
CSE 321 Object Oriented Programming (C#) 19
Samples of
Inheritance
Inheritance Sample in C#

Lecture 6
1. The first example creates class CommissionEmployee, which directly
inherits from class object. The class declares public auto-implemented
properties for the first name, last name and social security number,
and private instance variables for the commission rate and gross (i.e.,
total) sales amount.
2. The second example declares class BasePlusCommissionEmployee,
which also directly inherits from object. The class declares public auto-
implemented properties for the first name, last name and social
security number, and private instance variables for the commission
rate, gross sales amount and base salary. We create the class by
writing every line of code the class requires—we’ll soon see that it’s
more efficient to create this class by inheriting from
CommissionEmployee.

CSE 321 Object Oriented Programming (C#) 21


Inheritance Sample in C#

Lecture 6
3. The third example declares a separate BasePlusCommissionEmployee class that
extends class CommissionEmployee—that is, a BasePlusCommissionEmployee is a
CommissionEmployee who also has a base salary.
BasePlusCommissionEmployee attempts to access class CommissionEmployee’s
private members, but this results in compilation errors because a derived class
cannot access its base class’s private instance variables.
4. The fourth example shows that if base class CommissionEmployee’s instance
variables are declared as protected, a BasePlusCommissionEmployee class that
inherits from CommissionEmployee can access that data directly.
5. The fifth and final example demonstrates best practice by setting the Commission-
Employee instance variables back to private in class CommissionEmployee to
enforce good software engineering. Then we show how a separate
BasePlusCommissionEmployee class that inherits from class CommissionEmployee
can use CommissionEmployee’s public methods and properties to manipulate
CommissionEmployee’s private instance variables in a controlled manner.

CSE 321 Object Oriented Programming (C#) 22


Quote of the day (3)

Lecture 6
CSE 321 Object Oriented Programming (C#) 23
References
References

Lecture 6
◼ C# How to Program : Chapter 11

CSE 321 Object Oriented Programming (C#) 25

You might also like