Unit-04
Unit-04
Polymorphism and Software Reuse: Introduction, Types of Polymorphism
(Compile Time and Run Time Polymorphism), Mechanisms for Software
Reuse, Efficiency and Polymorphism
Case Study: A bank account system
Need of Inheritance
Inheritance is a mechanism by which one object can acquire all the properties (i.e.
data members) and behavior (i.e. member functions or methods) of a parent
object.
The basic idea of Inheritance is to create the new class (called child class or
derived or subclass) from an existing class (called parent class or Base or
Superclass). That is, the child class inherits the properties (methods and fields) of
the parent class.
Inheritance
Important Terminology
Super class: The class whose features are inherited is known as superclass(or a
base class or a parent class)
Sub class: The class that inherits the other class is known as a subclass(or a
derived class, extended class, or child class). The subclass can add its own fields
and methods in addition to the superclass fields and methods.
The keyword used for inheritance is extends
Types of Inheritance
Single
MultiLevel
Hierarchical
Multiple ( Supported only through interfaces)
Hybrid ( supported only through interfaces)
Types of Inheritance
Benefits of Inheritance
Inheritance helps in code reuse.The child class may use the code defined
in the parent class without re-writing it.
Inheritance can save time and effort as the main code need not be written
again.
Inheritance provides a clear model structure which is easy to understand.
Cost of Inheritance
Inheritance decreases the execution speed due to the increased time and
effort it takes, the program to jump through all the levels of overloaded
classes.
Inheritance makes the two classes (base and inherited class) get tightly
coupled. This means one cannot be used independently of each other.
The changes made in the parent class will affect the behavior of child class
too.
Constructors in derived classes
Constructor of a derived class must
also take care of the construction of the
fields of the base class, using the
special Java construct super()
The super() statement must appear as
the first executable statement in the
body of the constructor of the derived
class.
Method Overriding
If subclass (child class) has the same method as declared in the parent class, it is known as method
overriding in Java.
If a subclass provides the specific implementation of the method that has been declared by one of its
parent class, it is known as method overriding.
Usage:
Method overriding is used to provide the specific implementation of a method which is already provided
by its superclass.
Method overriding is used for runtime polymorphism
Method Overriding
If subclass (child class) has the same method as declared in the parent class, it is known as method
overriding in Java.
If a subclass provides the specific implementation of the method that has been declared by one of its
parent class, it is known as method overriding.
Rules for method overriding:
The method must have the same name as in the parent class
The method must have the same parameter as in the parent class.
There must be an Inheritance
Abstract Classes and Interfaces
Abstraction: Hiding the internal implementation of the feature and only showing the functionality to the
users.
Abstract class vs Interface
Type of methods: Interface can have only abstract methods. An abstract class can have abstract and non-
abstract methods.
Final Variables: Variables declared in a Java interface are by default final. An abstract class may contain
non-final variables.
Type of variables: Abstract class can have final, non-final, static and non-static variables. The interface
has only static and final variables.
Abstract Classes and Interfaces
Abstract class vs Interface
Implementation: Abstract class can provide the implementation of the interface. Interface can’t provide
the implementation of an abstract class.
Inheritance vs Abstraction: A Java interface can be implemented using the keyword “implements” and
an abstract class can be extended using the keyword “extends”.
Multiple implementations: An interface can extend another Java interface only, an abstract class can
extend another Java class and implement multiple Java interfaces
Accessibility of Data Members: Members of a Java interface are public by default. A Java abstract class
can have class members like private, protected, etc.
Abstract Classes and Interfaces
Abstract Classes and Interfaces
Abstract Classes and Interfaces
Polymorphism
Polymorphism in java is the ability of object to tkae many forms.
Polymorphism is a feature of the object-oriented programming language, Java, which allows a single
task to be performed in different way.
The derivation of the word Polymorphism is from two different Greek words- poly and morphs.
“Poly” means numerous, and “Morphs” means forms.[6]
Polymorphism
Types of Polymorphism
Method Overloading
Method Overriding
Static/ compile time Polymorphism
Dynamic/ Runtime Polymorphism
Static/ Compile-time Polymorphism
Types of Polymorphism
Method Overriding
It should be noted that runtime polymorphism can only be achieved through functions
and not data members.
Overriding is done by using a reference variable of the superclass. Which method to be
called is determined based on the object which is being referred to by the reference
variable. This is also known as Upcasting.
Upcasting is done when the Parent class’s reference variable refers to the object of the
child class.
class A{}
class B extends A{}
A a=new B(); //upcasting
Dynamic/ Run-time Polymorphism
Mechanism for Software Reuse
Mechanism for software reuse
Efficiency and Polymorphism
Case Study: A bank Account System
References
2. Geeksforgeeks.org
3. Javapoint.com
4. Btechsmartclass.com
5. Mygreatlearning.com
6.