Professional Documents
Culture Documents
PBO 08 - Inheritance (Part1)
PBO 08 - Inheritance (Part1)
PBO 08 - Inheritance (Part1)
SESI 8
Inheritance
(part 1)
BUKU REFERENSI
• Stuart Reges and Marty Stepp, Building Java Programs : A
Back to Basics Approach, Pearson, 2019.
• Savitch, Problem Solving with C++, Pearson International
Edition, 2006.
2
The software crisis
• software engineering: The practice of developing, designing,
documenting, testing large computer programs.
3
Inheritance
• Inheritance is a mechanism for defining new class types to
be a specialization or an augmentation of existing types.
• In OOPs, the concept of inheritance provides the idea of
reusability. This means that we can add additional features
to an existing class without modifying it.
• This is possible by deriving a new class from the existing
one. The new class will have combined features of both the
classes.
• Through inheritance, we can eliminate redundant code and
extend the use of existing classes
4
Need of Inheritance
• Consider a situation where we require all the features of a particular existing class and
we also need some additional features.
– 1) Modify the original existing class by acting new features into it. The disadvantage of this
method is that the users of the original class will get some extra facility and they can misuse
them
– 2) Rewrite the new class with all the required features i.e. The features of the existing class
plus the new features. The disadvantage of this method is that it will be consuming more
time and space.
– 3) Derive the new class from the existing class and only write the new features in the
derived class. The features of the original class will be inherited to the new class. This
method consumes less space and time. Moreover the original class is not modified. Hence
user of original class will not get extra facilities.
5
Law firm employee analogy
• common rules: hours, vacation, benefits, regulations ...
– all employees attend a common orientation to learn general
company rules
– each employee receives a 20-page manual of common rules
6
Separating behavior
• Why not just have a 22 page Lawyer manual, a 21-page
Secretary manual, a 23-page Marketer manual, etc.?
7
Is-a relationships, hierarchies
• is-a relationship: A hierarchical connection where one
category can be treated as a specialized version of another.
– every marketer is an employee
– every legal secretary is a secretary
8
Base and
Derived Classes
• A base class is a previously defined class that is used to define
new classes
9
Type of Inheritance
• Single Inheritance
• Multiple Inheritance
• Hierarchical Inheritance
• Multilevel Inheritance
• Hybrid Inheritance
10
Single Inheritance
Base Class A
• A derived class with only one
base class, is called Single
Inheritance.
B
Derived Class
11
Multiple Inheritance
• A derived class with several base
classes, is called multiple Inheritance.
Base Class A B
Derived Class C
12
Hierarchical Inheritance
• The properties of one class may be
inherited by more than one class is called hierarchical
Inheritance.
Base Class A
Derived Class B C D
13
Multilevel Inheritance
Base Class A
• The mechanism of
deriving a class from
another derived class is
Intermediate B
known as multilevel Base Class
Inheritance.
Derived Class C
14
Hybrid Inheritance
A
15
Inheritance
• Base Class
A base class can be defined as a normal C++ class,
which may consist of some data and functions.
class BaseClassName{
....
};
• Derived Class
class DerivedClassName : visibility-mode BaseClassName
A{class which acquires properties from base class
//visibility-mode is called derived class. A
: private/protected/public
....
derived
};
class can be defined by class in addition to its own
detail.
16
Visibility Mode
17
Access Privileges
18
Example of Inheritance
Publication:
• publisher
• date
Magazine: Book:
• # of issues per year • ISBN
• circulation • author
19
Order of Constructor and Destructor
Execution
20
Constructor- Initializers
• ctor-initializer is actually used to transfer the parameters to the
constructors of the base-class
21
End of slides.
22