0% found this document useful (0 votes)
2 views41 pages

5 - Inheritance

The document discusses Object Oriented Programming (OOP) concepts, specifically focusing on inheritance and polymorphism. It explains the 'is a' relationship between superclasses and subclasses, how subclasses inherit fields and methods, and the use of the super keyword in constructors. Additionally, it covers method overriding, access modifiers, and the implications of using final and protected members in class design.

Uploaded by

marwenramdan165
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views41 pages

5 - Inheritance

The document discusses Object Oriented Programming (OOP) concepts, specifically focusing on inheritance and polymorphism. It explains the 'is a' relationship between superclasses and subclasses, how subclasses inherit fields and methods, and the use of the super keyword in constructors. Additionally, it covers method overriding, access modifiers, and the implications of using final and protected members in class design.

Uploaded by

marwenramdan165
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Programming II

Object Oriented Programming (OOP)


Inheritance and polymorphism

1
Inheritance:
• Real life objects are typically specialized versions of other
more general objects.

2
Example:

3
Example:

4
Example:

5
The “is a “ Relationship:
• The relationship between a superclass and an inherited is
called an “is a” relationship.
➢An Employee “is a” person.
➢Salaried employee “is an” Employee.
• A specialized object has:
➢All of the characteristics of the general object, plus
additional characteristics that make it special.
• In object-oriented programming, inheritance is used to
create an “is a” relationship among classes.

6
The “is a “ Relationship:
• We can extend the capabilities of a class.
• Inheritance involves a superclass and a subclass.
➢The superclass is the general class and the subclass is
the specialized class.
• The subclass is based on, or extended from, the
superclass.
➢Super classes are also called base classes, and
subclasses are also called derived classes.
• The relationship of classes can be thought of as parent
classes and child classes.

7
Inheritance:
• The subclass inherits fields and methods from the
superclass without any of them being rewritten.
• New fields and methods may be added to the subclass.
• The Java keyword, extends, is used on the class header
to define the subclass.
public class Employee extends person

8
Example:

9
Class person:

10
Class person:

11
Class student:

Test Program:

12
Class student (Create the attributes)

13
Class student (Create the constructor)
Super Keyword: (reusability)

14
Class Student (Complete Attributes and methods)

15
Class Student (Complete Attributes and methods)

16
Test Program:

Output:

17
Inheritance, Fields and Methods:
• Members of the superclass that are marked private:
➢ are not inherited by the subclass, and
➢may only be accessed from the subclass by public
methods of the superclass.
• Members of the superclass that are marked public:
➢ are inherited by the subclass, and
➢ may be directly accessed from the subclass.

18
Inheritance and Constructors:
• Constructors are not inherited.
• The superclass constructor can be explicitly called from
the subclass using the super keyword.
• When a subclass is instantiated (create object from
subclass), the superclass default constructor is executed
first. >> incase of object without any arguments

19
Note:
• When a subclass is instantiated (create object from
subclass), the superclass default constructor is executed
first.
Superclass:

20
Subclass:

Test program:

21
Test Program:

22
Overriding Superclass Methods:
• A subclass may have a method with the same signature as
a superclass method.
• The subclass method overrides the superclass method.
• This is known as method overriding.
• A subclass method that overrides a superclass method
must have the same signature as the superclass method.
• An object of the subclass invokes the subclass's version
of the method, not the superclass’s.
• The @Override annotation should be used just before the
subclass method.

23
Example:

24
Example: Class Employee:

25
Example: Class Salaried Employee:

26
Example:
Test Program:

Output:

27
Example:
Class Person:

Class Employee:

28
Example:
Test Program:

Output:

29
Polymorphism:
Reference Type & Object Type:
reference_type reference_variable = new object_type()
• You can create an object from a reference type superclass
and an object type subclass.
• You can't create an object from a reference type subclass
and an object type superclass.
• If you create an object from a reference type superclass
and an object type subclass, you can use only the methods
in the superclass.

30
Reference Type & Object Type:

Output:

31
Reference Type & Object Type:

Output:

32
Reference Type & Object Type:
Salaried Employee Class:

Test Program:

Output:

33
Preventing a Method from Being Overridden:
• The final modifier will prevent the overriding of a
superclass method in a subclass.
Public final void display()
• If a subclass attempts to override a final method, the
compiler generates an error.
• This ensures that a particular superclass method is used
by subclasses rather than a modified version of it.

34
Example:
Employee Class:

Salaried Employee Class:

Run from main method:

35
Protected Members:
• Using protected instead of private makes some tasks
easier.
• Any class that is derived from the class, or is in the same
package, has unrestricted access to the protected member.
• It is always better to make all fields private and then
provide public methods for accessing those fields.
• Any method in the same package may access the protected
member.

36
Example:

• Generates error.
• The solution is protected member.

37
Example:

38
Example:

39
Example:

40
Access Modifiers:

Modifier Same class Same Subclass Other


Package (different classes
Package)

Public Yes Yes Yes Yes

Protected Yes Yes Yes No

default Yes Yes No No

private Yes No No No

41

You might also like