File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes practise question : guess output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "practise question : guess output" Watch "practise question : guess output" New topic

practise question : guess output

Vishy Karl
Ranch Hand

Joined: Sep 08, 2003
Posts: 116
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()");

class Child extends Parent
private void method1()
System.out.println("Child's method1()");
public static void main(String args[])
Child c = new Child();

"The man who can drive himself further once the effort gets painful is the man who will win." <br />Roger Bannister
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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.

SCJP Tipline, etc.
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
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.
[ October 09, 2003: Message edited by: Barkat Mardhani ]
Sudhakar Krishnamurthy
Ranch Hand

Joined: Jun 02, 2003
Posts: 76
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.
Rajeev Sachdev

Joined: Jul 01, 2003
Posts: 10
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..
Rajeev Sachdev

Rajeev Sachdev<br />(Sun Certified Java Programmer 1.4)<p>"Research is to see what everyone sees and thinking what no one has thought of"
I agree. Here's the link:
subject: practise question : guess output
It's not a secret anymore!