Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inheritance" Watch "Inheritance" New topic
Author

Inheritance

Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
class P {
void printS1(){System.out.print("P.printS1 ");}
void printS2() {System.out.print("P.printS2 ");}
void printS1S2(){printS1();printS2();}
}
class Q extends P {
void printS1(){System.out.print("Q.printS1 ");}
void printS2(){System.out.print("Q.printS2 ");}
public static void main(String[] args) {
new Q().printS1S2();
}
}
This is one of the questions asked in dan's mock exam by topics.The o/p is Q.printS1 Q.printS2.Can anybody explain how? the controll flow?I mean when you call printS1S2() using object of Q,controll is transfered to printS1S2() method of class P,as class Q doesn't contain that method.There inside again printS1() & printS2() are called....how JVM takes decision about which methods to call,weather printS1(),printS2() methods of class P or class Q?
And how mehtods get called if printS1() method of both class P and class Q are static?
Thanks
Veena


SCJP1.4
"Continuous effort - not strength or intelligence - is the key to unlocking our potential."
*Winston Churchill
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055

how JVM takes decision about which methods to call,weather printS1(),printS2() methods of class P or class Q?

This is called dynamic method lookup. The method invoked depends on the type of the object (not the object refence) at runtime. For example, I have
P ref = new P();
ref.printS1();
the printS1 of P will be invoked. Continuing, if now I have
ref = new Q();
ref.printS1();
the printS1 of Q will be invoked. Note that the object reference ref is now referring to a different object. The object reference is not the same as the object it refers to. The object reference ref is of type P, while the type of the object it is referring to at that point in time is an object of type Q. The object reference ref can still refer to an object of type Q, since, because of inheritance, an object of type Q is also an object type P. (A cat is a mammal because cat is a subclass of mammal...)
This is called polymorphism. It allows your code to be more flexible because you can invoke different implementations of a method while the application is already running.
zarina mohammad
Ranch Hand

Joined: Jun 26, 2002
Posts: 104
And how mehtods get called if printS1() method of both class P and class Q are static?

If the method printS1() is changed to static in both subclass(Q) and its superclass(P). then the method in the superclass is called.because
static method cannot override(but it can hide) the method of the superclass unlike the instance method which can override the superclass method.
-zarina
Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
Thank you Anthony & Zarina,it is pretty clear to me now.
Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
Zarina,
Can you explain the following statement with example?
static method cannot override(but it can hide) the method of the superclass unlike the instance method which can override the superclass method.

class P {
static void printS1(){
System.out.print("P.printS1 ");
}
void printS2() {
System.out.print("P.printS2 ");
}
void printS1S2(){
printS1();
printS2();
}
}
class Q extends P {
static void printS1(){
System.out.print("Q.printS1 ");
}
void printS2(){
System.out.print("Q.printS2 ");
}
public static void main(String[] args) {
Q q1=new Q();
q1.printS1S2();
q1.printS1();
}
}
Output is P.printS1() Q.printS2() Q.printS1().It prints P.printS1() when printS1() is called through printS1S2(),it prints Q.printS1 when it is called directly.Can you explain this senario?
Thanks
Veena
zarina mohammad
Ranch Hand

Joined: Jun 26, 2002
Posts: 104
Veena, here is how it works

1) q1.printS1S2() prints P.printS1 but not Q.printS1 because printS1()(in superclass) is a static method which is hiding the printS1()(also a static method) of the subclass
Secondly it also prints Q.printS2 but not Q.printS1 since the runtime object is of type Q
2)q1.printS1()prints Q.printS1 since the subclass printS1() hides the superclass printS1()
hope this clears your doubt
zarina
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance
 
Similar Threads
Question on inheritance from Dan's exam
Dan's Q
Why doesn't the subclass variable hide it?
anotherone from Dan's
Shadowing static methods