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 about polymorphism! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "about polymorphism!" Watch "about polymorphism!" New topic
Author

about polymorphism!

dragon ji
Ranch Hand

Joined: Oct 31, 2002
Posts: 110
follows the code:
class Base {
int i=99;
public void amethod(){
System.out.println("Base.amethod()");
}
}
public class RType extends Base{
int i=-1;
public static void main(String argv[]){
Base b = new RType();//<= Note the type
System.out.println(b.i);
b.amethod();
}
public void amethod(){
System.out.println("RType.amethod()");
}
}
the answer is :Note how the type of the reference is b Base but the type of actual class is RType. The call to amethod will invoke the version in RType but the call to output b.i will reference the field i in the Base class.
But I can't understand why the call to output b.i will reference the field i in the Base class?



scjp 1.4<br />challenge haven't limit!
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2578

The methods exhibit dynamic polymorphism, i.e. overriding. Not the member variables.
HTH,
- Manish
dragon ji
Ranch Hand

Joined: Oct 31, 2002
Posts: 110
oh,I get it now.thanx
Don Bosco
Ranch Hand

Joined: Oct 31, 2002
Posts: 108
Manish,
Dynamic Polymorphism and Overriding are not synonymous. Dynamic Polymorphism occurs when subclass methods are called using base class reference. When you are not using base class reference, it is Compile Time Polymorphism.
//Dynamic Polymorphism
Base b = new Derived();
b.OverridenMethod();
//Compile Time Polymorphism
Derived d = new Derived();
d.OverridenMethod();
If you already know this then just ignore this message. No hard feelings.


SCJP 1.4<p>Wingardium Leviosa!!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: about polymorphism!