PBO 08 - Inheritance (Part1)

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 22

• Pemrograman Berorientasi Objek

Oleh: Indriani Noor Hapsari

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.

• Large-scale projects face many issues:


– getting many programmers to work together
– getting code finished on time
– avoiding redundant code
– finding and fixing bugs
– maintaining, improving, and reusing existing code

• code reuse: The practice of writing program code once and


using it in many contexts.

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.

• Thus can be satisfied with three different methods.

– 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

• each subdivision also has specific rules:


– employee receives a smaller (1-3 page) manual of these rules
– smaller manual adds some new rules and also changes some rules
from the large manual

6
Separating behavior
• Why not just have a 22 page Lawyer manual, a 21-page
Secretary manual, a 23-page Marketer manual, etc.?

• Some advantages of the separate manuals:


– maintenance: Only one update if a common rule changes.
– locality: Quick discovery of all rules specific to lawyers.

• Some key ideas from this example:


– General rules are useful (the 20-page manual).
– Specific rules that may override general ones are also useful.

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

• inheritance hierarchy: A set of classes connected by is-a


relationships that can share common code.

8
Base and
Derived Classes
• A base class is a previously defined class that is used to define
new classes

• A derived class inherits all the data and function members of a


base class (in addition to its explicitly declared members.)

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

• The hybrid Inheritance is


a combination of all types
B C
of Inheritance.

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

• Base class constructors are always executed first.


• Destructors are executed in exactly the reverse
order of constructors
• The following example, shows you the ordering of
constructors.

20
Constructor- Initializers
• ctor-initializer is actually used to transfer the parameters to the
constructors of the base-class

class Magazine :public Publication


{
public:
Magazine( char * publshr, Date & aDate, int issues ):
Publication( publshr, aDate )
{issPYear = issues;
cout << "Magazine constructor\n";
}
~Magazine() { "Magazine destructor\n"; }
private:
int issPYear; // issues per year
};
int main()
{ Magazine aMag( "Zipp", Date(10, 1, 95), 12 );
return 0;
}

21
End of slides.

22

You might also like