File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Dynamic polymorphism and Overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Dynamic polymorphism and Overriding" Watch "Dynamic polymorphism and Overriding" New topic
Author

Dynamic polymorphism and Overriding

Don Bosco
Ranch Hand

Joined: Oct 31, 2002
Posts: 108
Guys,
I read some messages in the forum which use
Dynamic Polymorphism and Method Overriding synonymously. It is WRONG!!
Dynamic Polymorphism occurs when subclass methods are called using base class reference.
When you are not using base class reference to access overriden methods, it is Compile Time Polymorphism.
//Dynamic Polymorphism
Base b = new Derived();
b.OverridenMethod();
//Compile Time Polymorphism
Derived d = new Derived();
d.OverridenMethod();
In both the above cases, same method is called.


SCJP 1.4<p>Wingardium Leviosa!!
Don Bosco
Ranch Hand

Joined: Oct 31, 2002
Posts: 108
FYI,
Method Overloading is always Compile Time polymorphism.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
FYI,
Method Overloading is always Compile Time polymorphism.

Wohwohwoh not that fast, that's not quite correct!
What about the following code:

The created object is of type Test but referenced using the base class reference. method1() is overloaded in SuperTest and in Test...
method1 cannot be resolved at compile-time and if you try to decompile (using javap) you get the following bytecode:

On line 11, you can see that method1 is invoked using invokevirtual which means that method1 is looked up dynamically based on the runtime type of the object.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
You get the exact same bytecodes for main() if you remove the method "public void method1(Object o)" entirely.
Overloading is a non-issue here. The invokevirtual occurs because, as far as the compiler is concerned, "t" could be some other subclass of SuperTest that has overridden "public void method1 (String s)".


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
I agree that method1 in class Test is not useful for the problem at hand since both method1 declared in SuperTest are inherited. This was just to demonstrate that overloaded methods may not be resolved at compile time as Don claimed.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
But, given that the bytecode is identical whether or not there is overloading, I'm not convinced by this example.
Even if you take out "public void method1(int i)", thus removing all overloading, the compiler generates the same bytecode for main().
[ November 01, 2002: Message edited by: Ron Newman ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dynamic polymorphism and Overriding