" Polymorphism is one of the most important concepts in OOP.
It describes the ability of something
to have or to be displayed in more than one form. The different forms arise because these entities
can be assigned different meanings and used in various ways in multiple contexts.
Polymorphism
Static or Compile Time Dynamic or Runtime
Wlrtual/Qveridling
Method
Method Operato
Overloading Overloding
2
Inheritance and Polymorphism
Java_-_Inhe..orphism.pdf Open
Static Polymorphism
Also called compile time polymorphism.
Class can have more than one methods with same name but different number or sequence or
type of arguments.
Type of the object is determined at compile time(by the compiler). Known as static binding.
3
Inheritance and Polymorphism
Method Overloading
void test (){
methods within the same class that share the System.out .print ln ( "No parameters ");
same name, but their parameter declarations
are different
1/ Overload test for one integer parameter.
compile time polymorphism implementation of void test (int a) {
java System. out.println ("a: a) ;
" When an overloaded method is invoked, Java
uses the type and/or number of arguments as // Overload test for two integer parameters.
void test (int a, int b) {
itsguide to determine which version of the System. out. print ln("a and b: " + b);
overloaded method to actually call
" Note :return types do not playa role in 1l Overload test for a double parameter
overload resolution double test (double a)
System. out .println ("double a: + a) :
Xetun a a ;
Method Overloading
int add (int a, int b) {
return a + b;
int add (int a, int b, int c) {
add(3.8, 6.5);
return a + b t c;
}
double add(double a, double b) {
return a + b;
Constructor Overloading
class Box
double width;
" In Java, we canoverload constructors like double height:
double depth:
methods.
1/ construct clone of an object
The constructor overloading can be defined as Box (Box o b ) / / pass object to constructor
width ob. width;
the concept of having more than one he ight ob.height;
constructor with different parameters so that dept h ob. depth:
every constructor can perform a different task.
11 constructor used when all dimensions specified
Box double , double h, double d)
Wdth
depth-d;
constructor used when no di mens ions specified
Box
dth use 1 to 1ndicate
helgt 1 1 a n uninit1alized
dept
used when cube is Created
Final Keyword
Afield can be declared as final, which means its content cannot be modified making it aconstant.
Initialize a final field when it is declared : Give it a value when it is declared or assign a value within
a constructor.
Inheritance
" Inheritance is one of the key features of Object Oriented Programming.
" Inheritance provided mechanism that allowed aclass to inherit propertyof another class.
" When a Class extends another class it inherits all non-private members including fields and
methods.
" Inheritance in Java can be best understood in terms of Parent and Child relationship, also known as
Super class(Parent) and Sub class(child) in Java language.
extends keyword is used to describe inheritance in Java.
Therefore, asubclass is a specialized version of a superclass. It inherits allof the members defined
by the superclass and adds its own, unique elements.
Note : Java does not support the inheritance of multiple superclasses into a single subclass. no class
can bea superclass of itself.
IITerLdice
Animal
4
Non-ttying
bird
Flying
Reptile
bird
Penguin Ernu Owl Snake Lizard
ngle Inheritance
" In this inheritance, a derived class is created from a single base class.
Single Inheritance
Vehicle
Class A Base Class
Extends Car
Class B Derived Class
Single Inheritance
Multi-Level Inheritance
" Inthis inheritance, a derived class is created from another derived class.
Class A Student
Marks
Class B
CassC Sports
Multilevel nneitance Multi-level Inheritance
Hierarchial Inheritance
" In this inheritance, more than one derived classes are created
from a single base.
Student Sper cass
Class A Par ent
Science Commerce Arts
Class B Class C
Hierarchical Inheritance
The hybrid inheritance can be achieved by using the following combinations:
Single and Multiple Inheritance (not supported but can be achieved through interface)
" Muitilevel and Hierarchical Inheritance
" Hierarchical and Single Inheritance Grand Father
" Multiple and Multilevel Inheritance Single
Inherit inheritance
Father
inherit Hierarchical
inheritance
Son Daughter
Hybrid
Inheritance
Hybrid Inheritance
Multiple Inheritance
" In this inheritance, a derived class is created What is Diamond Problem?
from more than one base class. Java does
not support multiple inheritance.
In multiple inheritance there is a chance of Class A
multiple properties of multiple objects with
the same name being available to the sub Extencs
VOId method
Extends
class object. This leads to ambiguity.
Class B ee Class C
Class D
Inheritance - Advantages and Disadvantages
" Advantages of Inheritance
" inheritance - It can make application code more flexible to change because classes that inherit
form a common superclass can be used interchangeably.
Reusability - Facility to use public methods of base class without rewriting the same.
Extensibility -Extending the base class logic as per business logic of the derived class.
Disadvantages of lnheritance
" It increase's timefeffort to take the program to jump through all the leveis of inherited classes.
" Main Disadvantage of Using inheritance is that the two classes(base and inherited class) get
tightly coupled.
Inheritance and Polymorphism 15