OOPortal OOPortal

Inheritance/Polymorphism  «Prev  Next»
Lesson 5 A derived class - The IS-A relationship
Objective Several pairs of example classes fit definition

IS-A relationship in Object Oriented Thinking

Determine whether several pairs of example classes fit the definition of class and subclass.
In our student/graduate student example, grad_student is the derived class and student is the base class. The use of the keyword public following the colon in the derived class header means that the protected and public members of student are to be inherited as protected and public members of grad_student. Members that are private are inaccessible. This is referred to as public inheritance.
Public inheritance also means that the derived class grad_student is a subtype or subclass of student. Thus, a graduate student is a student, but a student does not have to be a graduate student. This subtyping relationship is called the ISA relationship. This is also called interface inheritance.
A derived class is a modification of the base class that inherits the public and protected members of the base class.
The only members that cannot be inherited are constructors and any member function that overloads the assignment operator.
Let us look at an example of public inheritance using the student and grad_student classes.

Distinguish between "is-a" relationship and "has-a" relationship

The is-a relationship 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, so any attributes and behaviors of a vehicle are also attributes and behaviors of a car. By contrast, the "has-a relationship" represents composition. The folloing link discussses Aggregation. In a "has-a relationship", an object contains one or more objects of other classes as members. For example, a car includes many components. A car has a
  1. steering wheel,
  2. brake pedal, and
  3. transmission.

Derived-class member functions might require access to base-class data members and member functions. A derived class can access the non-private members of its base class. Base-class members that should not be accessible to the member functions of derived classes should be declared private in the base class. A derived class[1] can change the values of private base-class members, but only through non-private member functions provided in the base class and inherited into the derived class.

Public Inheritance - Quiz

Click the Quiz link below to take a brief multiple-choice quiz on public inheritance.
Public Inheritance - Quiz

Classes Subtypes Exercise

When you have completed the quiz, return here and click the Exercise link to determine whether several pairs of example classes fit the definition of class and subclass.
Classes Subtypes - Exercise

[1]derived class: A derived class is a class created or derived from another existing class.