• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

practise question : guess output

 
Vishy Karl
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • 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();
}
}
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • 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.
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • 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 ]
 
Sudhakar Krishnamurthy
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • 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
 
Rajeev Sachdev
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic