Sunteți pe pagina 1din 16

pdfcrowd.com open in browser PRO version Are you a developer?

Try out the HTML to PDF API


Join to get new posts by mail
Never miss any post!!! We will mail you
directly whenever a new article is posted
Email Address
Join
Search
Type Your Search Word.....
You are here: Home / OOPs Concepts / Dynamic Polymorphism
OOPS I/O STREAMS COLLECTIONS THREADS INTERVIEW QUESTIONS ABOUT AUTHOR
Dynamic Polymorphism
By S. Nageswara Rao, Corporate Trainer on May 17, 2011
Static binding and static polymorphism is achieved through method overloading. Now
let us go for dynamic polymorphism. Observe the following code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Bird
{
public void eat() // I
{
System.out.println("All birds eat");
}
}
class Peacock extends Bird
{
public void eat() // II
{
System.out.println("Peacock eats grains");
}
}
class Vulture extends Bird
Free Online Database
zoho.com/creator/online-database
Create database apps in minutes. Just Drag-&-drop. Try NOW!
Java Bird Java Object C# to Java C++ Java
Java Tutorials,
tips, forums, faq
a blog for Java
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API
If you like our content, please show
your love by liking our page on
facebook & Google+
Nageswara Rao
28 followers
Follow
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
{
public void eat() // III
{
System.out.println("Vulture eats flesh");
}
}
class Crane extends Bird
{
public void eat() // IV
{
System.out.println("Crane eats fish");
}
}
public class DynamicPolyDemo
{
public static void main(String args[])
{
Bird b1 = new Bird(); b1.eat(); // calls I
Peacock p1 = new Peacock(); b1 = p1; b1.eat(); // calls II
Vulture v1 = new Vulture(); b1 = v1; b1.eat(); // calls III
Crane c1 = new Crane(); b1 = c1; b1.eat(); // calls IV
}
}
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API
Find us on Facebook
Way2Java
4,259 people like Way2Java.
Facebook social plugin
Like
In the above code, Bird is inherited by three classes Peacock, Vulture and
Crane. It is an example of hierarchical inheritance.
Subclasses Peacock, Vulture and Crane overrides the eat() method of Bird.
b1 = p1;
b1.eat();
In the above statement, subclass Peacock object p1 is assigned to super class Bird
object b1. Earlier, in Object Casting, we know if a subclass object is assigned to a
super class object, the super class object will call subclass overridden
method. As per this rule, b1 will call p1 eat() method. Infact, the b1 contains the
reference of p1.
b1 = v1;
b1.eat();
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API
Now, the Bird object b1 is assigned with the subclass Vulture object v1. Now the
earlier p1 reference is replaced by v1. Now b1 points to v1 location. Now b1 calls v1
eat method.
b1 = c1;
b1.eat();
Again, the v1 reference in the super class object b1 is replaced by Crane object c1.
Now, b1 calls Crane's eat() method.
Observe the code, the statement b1.eat() prints 4 different outputs by calling 4
different eat() methods. The same method when called different times giving different
values is known a polymorphism ("poly" means many and "morphism" means
forms).
Which eat() method is to be called is decided at runtime because in every statement
the address of the earlier subclass object is replaced. This address replacement
happens at runtime (and not at compile time). Which overridden method is to be
called is decided (or binded) dynamically at runtime. This feature is known as
dynamic binding. Because polymorphism is achieved dynamically at runtime, this
is known as dynamic polymorphism.
Dynamic binding is also called as dynamic method dispatch as which overridden
method is to be dispatched for execution is known at runtime.
To achieve dynamic polymorphism following rules must be obeyed.
1. There must be method overriding.
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API
2. Subclass object must be assigned to a super class object.
The rule followed is "when a subclass object is assigned to a super class
object, the super object will call subclass overridden method".
We know earlier, static polymorphism which is achieved through method overloading.
Interface Polymorphism
The same Bird program, in the earlier example, can be modified to suit with
interfaces. Now declare Bird as an interface. We know that reference variables can
be created with abstract classes and interfaces (but not objects). Read the following
program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
interface Bird
{
public abstract void eat(); // I
}
class Peacock implements Bird
{
public void eat() // II
{
System.out.println("Peacock eats grains");
}
}
class Vulture implements Bird
{
public void eat() // III
{
System.out.println("Vulture eats flesh");
Java Profiling Tools
semanticdesigns.com/DownLoad
Very low overhead, large systems. Client, Server, J2EE or embedded
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
}
}
class Crane implements Bird
{
public void eat() // IV
{
System.out.println("Crane eats fish");
}
}
public class DynamicPolyDemo
{
public static void main(String args[])
{
Bird b1; // reference variable of interface
Peacock p1 = new Peacock(); b1 = p1; b1.eat(); // calls II
Vulture v1 = new Vulture(); b1 = v1; b1.eat(); // calls III
Crane c1 = new Crane(); b1 = c1; b1.eat(); // calls IV
}
}
Here Bird is interface and not a concrete class (in the earlier program it is concrete
class).
Bird b1;
b1 is reference variable and not object.
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API
b1 = p1;
b1.eat();
Subclass Peacock object is assigned to the reference variable b1 of interface Bird.
Here, super class is an interface and eat() is an abstract method. When a subclass
object is assigned to a super class reference variable, the reference variable calls
subclass overridden method. Now, b1 calls subclass Peacock's eat() method.
The addresses of subclass objects, p1, v1 and c1, in the super class reference variable
b1 are replaced in every statement dynamically at runtime. Every time when
b1.eat() method is called, it prints different outputs. This is known as polymorphism.
Because polymorphism is achieved at runtime dynamically, this is known as
dynamic polymorphism. Here, dynamic polymorphism is achieved
through interfaces.
Some questions for you from java.lang package.
1. How many ways you can copy one object properties to another?
Ans: 3 ways Shallow copying, Deep copying and Cloning.
2. How many types of inner classes exist?
Ans: 4 types.
3. What are JAR files?
Ans: JAR file is a zipped file zipped by JVM.
4. How to convert a string into string form?
Ans: String to data type conversions byte, short, int, long, float, double, char and
boolean.
5. How to convert an object to string?
Ans: Object to String toString()
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API
6. Do you know the functionality and methods of following java.lang package classes?
Ans: 1. Root class class Object 2. class Character 3. class System
7. What are wrapper classes?
Ans: Wrapper classes
8. What is the garbage collection mechanism in Java?
Ans: Garbage Collection gc() and exit(0)
9. How to use the C++ destructor functionality where Java does not support
destructors?
Ans: Java Destructor finalize()
10. How to compare two objects?
Ans: Object comparison hashCode() & equals()

Google+
Nageswara Rao
Follow 28
If you have any Questions, please ask below
2 comments
C# to Java C++ Java String Java Java Void
Add a comment...
Comment using...
0
0
Share
0 0
0
Share
1
Share
1
Share
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API
14 Responses
sumithra
April 21, 2014 at 11:56 am | Permalink | Reply
in above pgm wat is the use of DynamicPoleDemo() class? why didnt crate object for
that and wont be call other three metods by using this objct ?
S. Nageswara Rao, Corporate Trainer
April 21, 2014 at 8:30 pm | Permalink | Reply
It is a matter of convenience and clean code.
Posted in OOPs Concepts, Polymorphism | Tagged dynamic method binding java, interface polymorphism,
java dynamic method dispatch, Java dynamic polymorphism, late binding in java
Facebook social plugin
Aleeza Butt BSCS through VU
good
Reply Like February 13 at 6:38am
Hadia Javed
nice.
Reply Like September 15, 2013 at 11:48pm
PAGE 1 OF 1
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API
vijay reddy
December 7, 2013 at 5:33 pm | Permalink | Reply
If we call eat() with sub class objects p1,v1,c1 then which methods will be invoked?
if we call p1.eat() then also it prints peacock eats grains then why should we use
object casting.Sir could you please explain?
S. Nageswara Rao, Corporate Trainer
December 8, 2013 at 5:08 am | Permalink | Reply
To call with other class object through dynamic polymorphism.
Mona
October 16, 2013 at 11:37 am | Permalink | Reply
Hi, Mr.Nageshwara Rao
I have a query regarding Derived Casting.
Can u please explain me with a program that y do v get an Runtime Error called Class
Cast Exception and what is the solution to be undertaken.
Regards,
Mona
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API
S. Nageswara Rao, Corporate Trainer
October 26, 2013 at 2:53 pm | Permalink | Reply
See this link. This link is developed keeping your query in mind.
http://way2java.com/java-general/java-object-casting-problems/
Raja
May 14, 2013 at 11:22 am | Permalink | Reply
Can you pl explain whats the significance of having dynamic polymorphism in Java?
What is the exact real life usage for this feature? In this case any way we are creating
subclass object & then assigning that to a superclass reference variable. Instead of
calling the method through the reference variable of super class we can invoke the
same using respective subclass variable itself. I am sure when this feature is there
they would have thought for good real life fit for this.
S. Nageswara Rao, Corporate Trainer
May 15, 2013 at 1:35 pm | Permalink | Reply
1. With one class (super class) object, we can get the result required by the
user.
2. User may pass any object he likes.
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API
3. If you call subclass object, each subclass may have different methods.
4. When you extend super class, the sub classes will have uniform methods.
5. The same method of super class, the sub class may fill with its own code.
mohan
October 23, 2012 at 12:53 pm | Permalink | Reply
how can reference variable of Runnable interface call getClass() of java.lang.Object
though getclass Method is not present in Runnable interface?
mohan
October 20, 2012 at 11:39 pm | Permalink | Reply
The rule followed is when a subclass object is assigned to a super class object, the
super object will call subclass overridden method.
Runnable r= new Thread();
System.out.println(the class name of r is +r.getClass());
could you explain how r is able to call getClass() as it is not in Runnable interface.
akshiya
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API
June 30, 2012 at 9:45 pm | Permalink | Reply
sir,pls tell me example program for separate method overloading and method
overriding by using constructor..
S. Nageswara Rao, Corporate Trainer
July 1, 2012 at 7:26 pm | Permalink | Reply
First of all constructors cannot be overridden but can be overloaded. You
wanted method overloading using constructors. Following is the program.
public class Demo
{
public Demo()
{
display();
display(10);
display(10, 20);
}
public void display()
{
System.out.println(Hello 1);
}
public void display(int x)
{
System.out.println(x);
}
public void display(int x, int y)
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API
{
System.out.println(x*y);
}
public static void main(String args[])
{
new Demo();
}
}
Gaurav Sharma
January 28, 2012 at 4:38 pm | Permalink | Reply
Hi
I have a query related to this post .
Though you are saying that
when a subclass object is assigned to a super class reference, the super reference will
call subclass overridden method
but we can also call the toString() method of Object Class and other methods of it
from the superClass/interface reference
Despite they are not been overridden..
So , How why it is happening ???
Will you please explain me
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API
Name * Email *
Regards
Gaurav Sharma
S. Nageswara Rao, Corporate Trainer
January 28, 2012 at 4:43 pm | Permalink | Reply
toString() functionality is very different. The advantage of object casting
(subclass to super class) is that factory methods work where we get an object
of an interface like Connection, Statement and ResultSet etc.
Leave a Reply
Website
Comment
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API
Copyright 2014 Java Tutorials, tips, forums, faq.
Post Comment
Notify me of follow-up comments by email.
Notify me of new posts by email.

S-ar putea să vă placă și