Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance

 
Veena Pointi
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Anthony & Zarina,it is pretty clear to me now.
 
Veena Pointi
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic