aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why private method of parent is called? 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 "Why private method of parent is called?" Watch "Why private method of parent is called?" New topic
Author

Why private method of parent is called?

Kalyani Marathe
Ranch Hand

Joined: Apr 06, 2005
Posts: 72
Hi,

I would like to know why the private mthd method1() is called? I want to know how exactly the code works. I know static and private mtd cannot be overriden but still I am confused for private call.

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
{
public void method1()
{
System.out.println("Child's method1()");
}

public static void main(String args[])
{
Parent p = new Child();
p.method2();
}
}
Kalyani Marathe
Ranch Hand

Joined: Apr 06, 2005
Posts: 72
Sorry, I forgot to write "Thanks in advance".
Jay Vasudeva
Greenhorn

Joined: Sep 22, 2004
Posts: 21
Hi,

I just did this question myself and will tell you exactly what the system told me and it made sense to me.

When you call a method, it calls the method of the object type not the reference type. So, 'p' should look for 'method2' in class 'Child'. Now, since we know that there isn't any 'method2' in class 'Child', it will look for 'method2' in the superclass since superclass members are also accessible by the subclass object.

Now, if the 'method2' was private then I would say the compiler would give an error since subclass object can not see private members of its superclass.

this question highlights overriding rules....

hope it makes sense...

I am giving the exam on thursday and I can understand what everybody here is going through...

goodluck!

Jay


SCJP 1.4<br />SCWCD 1.4 ( next... )
Kalyani Marathe
Ranch Hand

Joined: Apr 06, 2005
Posts: 72
Can you give more detail explaination on private access. I am still confused.

Thanks.
levani dvalishvili
Ranch Hand

Joined: Mar 01, 2005
Posts: 99
In your parent class you have two methods : method1() and method2()
method1() is private(that means only members of that class can see it)
method2() is public that means anybody and everybody in the program can se it and call it
yuo can also see that method2() is calling method1() inside its body which is allowed due to the fact that both belong to same class .
now
in your child class you also have method1() that is not overriding parent method, because parent method is private, and child can not see or access it; so method1() in parent and method1() in child have nothing in common but the name and it is allowed in this situation. so when you create reference p which is type "Parent" and point it to object type "child" which is extending parent in this situation you are just calling a parents method2() since child does not have method2(). method2() is public and in turn calls method1(),and since method2() belongs to parent it will not look for method1() in child .
this is how I think program handles this situation


SCJP 1.5(Done) SCJA 1.0(Done)<br />SCWCD(in Progress...)
Soni Prasad
Ranch Hand

Joined: Mar 09, 2005
Posts: 97
Ok I understand what you want to say but then tell me what is happening in this code

class Phone {
String device = "Phone.device";
void showDevice() {
System.out.println("Phone.showDevice," + device + " ");
}
Phone() {
System.out.println("Phone construtor");
showDevice();
System.out.println("Phone construtor end");
}
}

class Mobile extends Phone {
String device = "Mobile.device";
void showDevice() {
System.out.println("Mobile.showDevice," + device + " ");
}
Mobile() {
showDevice();
}
public static void main(String[] args) {
Phone n = new Mobile();
n.showDevice();
}
}

output is :

Phone construtor
Mobile.showDevice,null
Phone construtor end
Mobile.showDevice,Mobile.device
Mobile.showDevice,Mobile.device


SCJP 1.4, SCBCD 1.3
Soni Prasad
Ranch Hand

Joined: Mar 09, 2005
Posts: 97
Ok I got it actually in the first code method was not overridden thats why parent's method is called.

Thanx anyway...
Nimai Etheridge
Greenhorn

Joined: Feb 16, 2005
Posts: 16
For your second piece of code, a call to super() is inserted by the compiler into Mobile's constructor, which invokes Phone()'s constructor, hence the first line of output. Phone's constructor then invokes the overridden showDevice method in Mobile, since Mobile is the declared type vs a reference type of Phone.

However, String device in the mobile object is null until after its super classes constructor has finished running, so you get the following line of output: Mobile.showDevice,null.

Phone's constructor then completes and the rest of the program is executed straightforwardly. At least that's my take on it.

If you're having trouble following the execution of code like this, I find it useful to step through it using a debug tool, like in Eclipse or something similar.

Hope this helps

nimai
[ April 12, 2005: Message edited by: Nimai Etheridge ]
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hi can i say

1) whenever an inheritance is involved the and calling a method, the JVM will look for the method in a child class if that doesn't exists then the JVM look into the parent class.

2) When it come to overridding the JVM will look in the object type.

3) when it is to overridding the static method the reference type is considered

4)is there anything when it comes to overloading
Kalyani Marathe
Ranch Hand

Joined: Apr 06, 2005
Posts: 72
Thanks to all. I understood the point.
 
wood burning stoves
 
subject: Why private method of parent is called?