polymorphism in java polymorphism in java with example types of polymorphism in java compile time polymorphism in java example of polymorphism in java dynamic polymorphism in java polymorphism example in java runtime polymorphism in java run time polymorphism in java define polymorphism in javapolymorphism in java example definition of polymorphism in java
Polymorphism:
Polymorphism is
one of the Object Oriented programming principle. Polymorphism in java is
supported by using other concepts like Abstraction, Encapsulation and
Inheritance.
Definition :
The mechanism of
representing one form in multiple forms is known as polymorphism.
The principle polymorphism is not a programming concept but
it is not of the stated principle. In java programming the principle of
polymorphism is implemented with programming concept called method overriding.
The polymorphism
concept in java say’s original method of base class is known as first form or
one form and redefined methods are known as multiple forms.
The principle polymorphism is classified into two types they
are
1)
Static /
compile time polymorphism.
2)
Runtime / Dynamic polymorphism.
1) Static polymorphism:
A
static polymorphism is one which method binds at compile time is known as
Compile time or static polymorphism.
Limitation:
Poor utilizaition of memory resource(main memory).
2) Runtime or Dynamic polymorphism:
The
runtime polymorphism in which method binds at runtime is known as Runtime
polymorphism.
Advantage Over Static polymorphism
Runtime
polymorphism in which memory resource (main memory space ) is effectively
utilized.
OOPs Concepts With Real Time Examples
The Following Example Illustrate the Concept of Compile time / Runtime Polymorphism
The Following Example Illustrate the Concept of Compile time / Runtime Polymorphism
Class BaseDemo
{
Void repeat()
{
System.out.println(“this is fun1() of Base demo”);
}
}
Class DerviedDemo1
extends BaseDemo
{
Void repeat()
{
System.out.println(“this is fun1() of Derived demo1”);
}
}
Class DerivedDemo2
extends BaseDemo
{
Void repeat()
{
System.out.println(“this is fun1() of Derived demo2”);
}
}
Class demo
{
Public static void main(String args[])
{
BaseDemo BD=new
BaseDemo();
BD.repeat();
BaseDemo BD=new
DerivedDemo1();
BD.repeat();
BaseDemo BD=new
DerivedDemo2();
BD.repeat();
}
}
}
Analysis:
1) The created object BD of Class BaseDemo(Base class)
statements like this
BaseDemo BD=new DerivedDemo2();
BD.repeat() ;
BD of BaseDemo checks the method at compile such that
repeat() is in BaseDemo class or not.
Then at runtime checks repeat() in DerivedDemo1,
DerivedDemo2 classes are not.
From the above program Consider these things BD contains
address of BaseDemo(), DerivedDemo1(), DerivedDemo2(). Hence BD is Polymorphic
object.
2) The method repeat() is available in one form and it is
redefined in multiple forms.Hence repeat() is known as polymorphic method.
3) The statement BD.fun1() is actually one statement but is
gives multiple results.Hence this statement is known as polymorphic statement.
No comments:
Post a Comment