SEng2202
Object Oriented Programming
CHAPTER - 3
Inheritance
Objectives
After studying this chapter, students should be able to learn:
Basics of Inheritance
Types of Inheritance
The protected modifier
Super and final java key words
Benefits of inheritance
Aggregation
Polymorphism and its types
Method Overloading vs. Overriding
The Abstract class
Review Questions
Inheritance
Object-oriented programming (OOP) is popular because:
• It enables reuse of previous code saved as classes
All Java classes are arranged in a hierarchy
• Object is the superclass of all Java classes
Inheritance and hierarchical organization capture idea:
• One thing is a refinement or extension of another
Reusability - building new classes by utilizing existing classes.
Inheritance
It is always good/“productive” if we are able to reuse something that is
already exists rather than creating the same all over again.
This is achieved by creating new classes, reusing the data members and
methods of existing classes.
This mechanism of deriving a new class from existing/old class is called
“inheritance”.
The old class is known as “base” class, “super” class or “parent” class”;
and the new class is known as “sub” class, “derived” class, or “child”
class.
Inheritance
Parent
Inherited capability
Child
Inheritance
superclass = parent class OR base class
subclass = child class OR derived class
Example Cont.…
Inheritance
Inheritance
Inheritance relationships often are shown graphically in a UML class
diagram, with an arrow with an open arrow head pointing to the parent
class.
Vehicle
Car
Inheritance should create an is-a relationship, meaning the child is a
more specific version of the parent.
Declaring
Declaringsub
Subclasses
classes
In Java, we use the reserved word extends to establish an inheritance
relationship
Example
Declaring sub classes
subclass superclass
or extends or
derived class base class
Inheritance - example
Declaring Sub classes
Types of inheritance
The different types of inheritances are:
• Single inheritance (only one super class)
• Multiple inheritance (several super classes, not supported by Java)
• Hierarchical inheritance (one super class, many sub classes)
• Multi-Level inheritance (derived from a derived class)
• Hybrid inheritance (more than two types)
• Multi-path inheritance (inheritance of some properties from two sources).
Types of inheritance
A A B A
B C B C D
(a) Single Inheritance (b) Multiple Inheritance (c) Hierarchical Inheritance
A A A
B C B C
B
C D D
(d) Multi-Level Inheritance (e) Hybrid Inheritance (f) Multipath Inheritance
Single inheritance example
Single inheritance example (cont …)
Single inheritance example (cont …)
output
Multilevel Inheritance example
Multilevel Inheritance example(cont…)
output
Multiple inheritance
Multiple inheritance allows a class to be derived from two or more
classes, inheriting the members of all parents
Collisions, such as the same variable name in two parents, have to be
resolved
Java does not support multiple inheritance
In most cases, the use of interfaces gives us aspects of multiple
inheritance without the overhead.
The protected modifier
Visibility modifiers determine which class members are inherited and
which are not
Variables and methods declared with public visibility are inherited; those
with private visibility are not
But public variables violate the principle of encapsulation
There is a third visibility modifier that helps in inheritance situations:
protected
Cont.….
The protected visibility modifier allows a member of a base class to be
accessed in the child.
• protected visibility provides more encapsulation than public does
• protected visibility is not as tightly encapsulated as private visibility
The use of “super” keyword
“super” is a keyword used to refer to hidden variables of super class
from sub class.
• super.a=a;
It is used to call a constructor of super class from constructor of sub class
which should be first statement.
• super(a,b);
It is used to call a super class method from sub class method to avoid
redundancy of code
• super.addNumbers(a, b);
The use of “super” keyword
Why is super needed to access super-class members?
When a sub-class declares the variables or methods with the same names and
types as its super-class:
The re-declared variables/methods hide those of the super-class.
The use of “super” keyword
The use of “super” keyword
Although the i variable in B hides the i variable in A, super allows
access to the hidden variable of the super-class:
Using final keyword with Inheritance
final keyword is used to declare constants which can not change its
value of definition.
final variables can not change its value.
final methods can not be Overridden.
final Classes can not be extended or inherited
Preventing Overriding with final
A method declared final cannot be overridden in any sub-class:
Preventing Overriding with final
A class declared final cannot be inherited and has no sub-classes.
final class A { … }
This class declaration is considered illegal:
class B extends A { … }
Declaring a class final implicitly declares all its methods final.
The Benefits of Inheritance
Code reusability:- Inheritance automates the process of reusing the
code of the super classes in the subclasses.
With inheritance, an object can inherit its more general properties
from its parent object, and that saves the redundancy in
programming.
Code maintenance:- Organizing code into hierarchical classes makes
its maintenance and management easier.
The Benefits of Inheritance
Implementing OOP:- Inheritance helps to implement the basic OOP
philosophy to adapt computing to the problem and not the other way
around, because entities (objects) in the real world are often organized
into a hierarchy.
Increased Reliability :-(resulting from reuse and sharing of well-
tested code)
Aggregation
Aggregation in Java is a relationship between two classes that is best
described as a "has-a" and "whole/part" relationship.
It is a more specialized version of the association relationship. The
aggregate class contains a reference to another class and is said to have
ownership of that class.
Each class referenced is considered to be part-of the aggregate class.
For example, if you imagine that a Student class that stores information
about individual students at a school. Now assume a Subject class that
holds the details about a particular subject
Aggregation
If the Student class is defined to contain a Subject object then it can be
said that the Student object has-a Subject object.
The Subject object also makes up part-of the Student object after all,
there is no student without a subject to study. The Student object,
therefore, owns the Subject object.
Aggregation
Polymorphism
Polymorphism came from the two Greek words „poly‟ means many and
morph means forms i.e. many forms
If the same method has ability to take more than one form to perform
several tasks then it is called polymorphism.
A polymorphic reference is a variable that can refer to different types of
objects at different points in time.
It is of two types: Static Polymorphism and Dynamic Polymorphism.
Static Polymorphism
Static Polymorphism:
The polymorphism exhibited at compile time is called Static
polymorphism.
Here the compiler knows which method is called at the
compilation. This is also called compile time polymorphism or
static binding.
Achieving method overloading & method overriding using
private, static and final methods is an example of Static
Polymorphism.
Static Polymorphism
Dynamic Polymorphism
The polymorphism exhibited at run time is called dynamic
polymorphism.
In this dynamic polymorphism a method call is linked with method
body at the time of execution by JVM.
Java compiler does not know which method is called at the time of
compilation. This is also known as dynamic binding or run time
polymorphism.
Method overloading and method overriding are examples of Dynamic
Polymorphism in Java. Provided that the methods are instance
method.
Dynamic Polymorphism
Dynamic Polymorphism using method overriding
Writing two or more methods in super & sub classes with same name and same
signatures is called method overriding.
In method overriding JVM executes a method depending on the type of the
object.
Dynamic Polymorphism using method overriding
Overloading Vs Overriding
Overloading deals with multiple Overriding deals with two methods,
methods in the same class with the one in a parent class and the other one
same name but different in a child class, with the same method
signatures name and signatures/parameters.
Overloading lets you define Overriding lets you define a similar
operation in different ways for different
a similar operation in different ways
object types
for different data.
Abstract
AbstractClass
class
A method with method body is called concrete method. In general any
class will have all concrete methods.
A method without method body is called abstract method. A class that
contains abstract method is called abstract class. i.e. An abstract class
is a class with zero or more abstract methods
It is possible to implement the abstract methods differently in the
subclasses of an abstract class.
Abstract
AbstractClass
class
These different implementations will help the programmer to perform
different tasks depending on the need of the sub classes.
Moreover, the common members of the abstract class are also shared by
the sub classes.
The abstract methods and abstract class should be declared using the
keyword abstract.
We cannot create objects to abstract class because it is having
incomplete code.
Abstract class
Whenever an abstract class is created, subclass should be created to
it and the abstract methods should be implemented in the subclasses,
then we can create objects to the subclasses.
Abstract class reference can be used to refer to the objects of its sub
classes.
Abstract class references cannot refer to the individual methods of
sub classes.
A class cannot be both „abstract‟ & „final‟ simultaneously
So, it is illegal to declare a class as both abstract and final. Why?
e.g.: final abstract class A{} // invalid
Abstract class
Abstract class
Review Questions
1. What is inheritance?
2. What is the purpose of super keyword?
3. Which class is at the top of the class hierarchy in Java?
4. What are the constructor issues surrounding inheritance?
5. What is method overriding?
6. Using aggregation, show the relationship between Address and
Employee class.
7. Describe the two types of polymorphisms with example
8. What is the purpose of “final” java key word
9. Describe basic properties of abstract classes.
Adama Science and Technology University
School of Electrical Engineering And Computing 2023 G.C