• 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

practise question : guess output

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Check out the following example . Plz. give a explanation for the output as when i ran the code i was surprised by the result
thanks in advance.
---------------------------------------
class Parent
{
private void method1()
{
System.out.println("Parent's method1()");
}
public void method2()
{
System.out.println("Parent's method2()");
method1();
}
}

class Child extends Parent
{
private void method1()
{
System.out.println("Child's method1()");
}
public static void main(String args[])
{
Child c = new Child();
c.method2();
}
}
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What were you surprised by? The fact that the method1 in Parent was invoked rather than the one in Child? If that's the case, it's because that method is private, therefore, it is not overridden.
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It will print from both parent methods. The key to understand here is that when a method is inherited from parent class, any calls within that method to :
- static methods (with any access modifier)
- non-static methods (private only)
- static/non-static fields (with any access modfier)
- static nested classes
- inner classes
which are also declared in Child class, will end calling parents versions.
The only exception is Non-static method (non-private only). This will be called from Child's class.
Hope this makes sense.
Barkat
[ October 09, 2003: Message edited by: Barkat Mardhani ]
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am confused can someone explain this in detail?
Why is it that the parents method1 is being called? is it just because its private? would it behave any different if it were public method??
The way i see it is child object is aware of only parent-method2 and child-method1. I would expect that it doesn't know anything about parent-method1 since its private.
Any help would be appreciated.
TIA
-S
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi there
there is nuthing to be confused of..
when the object is created of child class
Child c = new Child();
at runtime the class of the current object is checked and the method of the respective class is called..though method2 is being called by c and it is not explicitly present in Child class but it has been inherited from its Super class..therefore method2 of super class would be called and then the method1 of super class would be called coz a call to method1 of super class is being made from within the method2 of super class.
therefor the ouptut would be
Parent's method2()
Parent's method1()
now lets take an example of references.
class Parent
{
String message = "Parent";
public void getMessage()
{
System.out.println("hi this is parent class");
}
}
class Child extends Parent
{
String message="Child";
public void getMessage() //overtriding the method getMessage()
{
System.out.println("hi this is child class");
}
public static void main(String[] args)
{
Child c = new Child();
Parent p = new Parent();
c.getMessage() // getMessage of class Child would be called
p.getMessage() // getMessage() of Parent Class would be called
Parent pp = c;
pp.getMessage() // getMessage of Child Class would be called because at runtime it would be checked that the object c is of Which class and not the reference.
// but in case of variables its different the class of the reference is checked therefore pp.message would be called of Parent Class.

Hope this solves reference funda in detail..
Thanks
Rajeev Sachdev
 
incandescent light gives off an efficient form of heat. You must be THIS smart to ride this ride. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic