• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

method call

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the following piece of code:
public class Superclass {
public static void main(String[] args) {
System.out.println(new Subclass().methodA());
}

Superclass() {
System.out.println("SuperClass Constructor Executed");
}

private int methodB() {
System.out.println("methodB in Superclass");
return 9;
}

int methodA() {
System.out.println("methodA in Superclass");
return methodB();
}
}

class Subclass extends Superclass {
Subclass() {
System.out.println("SubClass Constructor Executed");
}

protected int methodB() {
System.out.println("methodB in Subclass");
return 1;
}
}
the answer is:
a.SuperClass Constructor Executed
b.SubClass Constructor Executed
b.methodA in Superclass
c.methodB in Superclass
d. 9
why is the methodB in the superclass invoked, why not that in the subclass, as that is the class that has been instantiated???
thanks,
------------------
lakshmi
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Lakshmi
I dont have the exact explanation, but i know the reason for the superclass method is being invoked. It is because of the private access modifier, once the flow is inside superclass, it is sort of binded by the private access modifier, to call its method.
Try removing private access modifier and try u will get 1 in the place of 9.
hope this clarifies to certain extent.
thanks
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Lakshami
This is a case of dynamic polymorphism.
remeber two things
1.method envoking will depend upon the object of the class
2.and data members depends upon type of object
In This case the methodb() is not Accessible with the object of subclass although it is available bcoz of private access
so it will call the super one
Hope this would solve the prob
Thanks
Navi
 
Lakshmi Manikantan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry but i still don't understand.....could u be a bit more specific??
thanks
------------------
lakshmi
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
private methods can not be overridden so methodA() can't see the overridden methodB() and runs the original.
 
Lakshmi Manikantan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get it...thanx everybody...
lakshmi
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Thomas Paul:
private methods can not be overridden so methodA() can't see the overridden methodB() and runs the original.


Private methods can not be overridden. How come we could have an overridden methodB() in subclass?
 
Lakshmi Manikantan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The methodB() in subclass is not overridden but 'shadowed', it is a new method with the same signature as the superclass methodB() and is not the one inherited from the superclass.
------------------
lakshmi
 
Pay attention! Tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic