Hello,
class SuperClass {
public void printA() { System.out.println("Super class"); }
public void displayDotts() { System.out.println("#########"); }
}
class SubClass extends SuperClass {
public void printA() { System.out.println("Sub class"); }
public void displayDotts() { System.out.println("............."); }
}
public class OverrideDemo {
public static void main(String[] args) {
SuperClass s1 = new SubClass();
s1.printA();
s1.displayDotts();
}
}
1. Why does s1.printA() statement trigger SubClass's printA function
instead of SuperClass's printA function, as I declared of type
"SuperClass" eventhough referenced to "SubClass"?
2. Buf if I delete SubClass's printA function, how could SuperClass's
printA function being triggered?
3. Why does compilation error occurs if I delete SuperClass's printA
function?
Thanks for the answer.
RedGrittyBrick - 09 Jan 2007 19:07 GMT
> class SuperClass {
> public void printA() { System.out.println("Super class"); }
[quoted text clipped - 16 lines]
> 3. Why does compilation error occurs if I delete SuperClass's printA
> function?
Because s1 is a reference to a "SuperClass". Therefore s1's methods are
"SuperClass" methods.
Try
((SubClass) s1).printA();
A. Bolmarcich - 09 Jan 2007 19:07 GMT
> Hello,
>
[quoted text clipped - 17 lines]
> instead of SuperClass's printA function, as I declared of type
> "SuperClass" eventhough referenced to "SubClass"?
Because that is how the Java programming language is defined. At
run time, the class of the object referred to by the variable named
"s1" is used as the starting point of the dynamic method lookup for
the method named "printA".
> 2. Buf if I delete SubClass's printA function, how could SuperClass's
> printA function being triggered?
Because that is how the Java programming language is defined. If the
dynamic method lookup does not find the method in a class, the lookup
is done in the superclass of the class.
> 3. Why does compilation error occurs if I delete SuperClass's printA
> function?
Because that is how the Java programming language is defined. At
compile-time, the compile checks whether the type of the variable
named "s1" (one of the superclasses of that type) has a method named
"printA".
> Thanks for the answer.
Lew - 10 Jan 2007 13:42 GMT
>> Hello,
>>
[quoted text clipped - 37 lines]
> named "s1" (one of the superclasses of that type) has a method named
> "printA".
This is the essence of object-oriented programming. It's called
"polymorphism". Study this concept and the Java idioms for it; it will reward
you greatly.
- Lew
saketram - 10 Jan 2007 15:06 GMT
Thanking you all. Now I got to understand.
:-)
> >> Hello,
> >>
[quoted text clipped - 43 lines]
>
> - Lew
Laurent D.A.M. MENTEN - 09 Jan 2007 19:09 GMT
> Hello,
>
[quoted text clipped - 17 lines]
> instead of SuperClass's printA function, as I declared of type
> "SuperClass" eventhough referenced to "SubClass"?
Because s1 really is a reference to a SubClass instance and the JVM uses
it to lookup the method to invoke. If you really want the SuperClass
printA to be invoked, you'll have to cast s1 to a SuperClass reference.
> 2. Buf if I delete SubClass's printA function, how could SuperClass's
> printA function being triggered?
While looking up for the method printA, if it happens that the
referenced instance is not a reference to an instance of a class that
overloads the printA method, the JVM looks up the ancestor(s) of that class.
> 3. Why does compilation error occurs if I delete SuperClass's printA
> function?
Because even if s1 really is an instance of SubClass, you can only
invoke methods declared in SuperClass.
> Thanks for the answer.
I suggest you get a copy of "The java programming language" from Addison
Wesley. All of these things a explained and much more!