• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Why private method of parent is called?

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I forgot to write "Thanks in advance".
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Kalyani Marathe
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you give more detail explaination on private access. I am still confused.

Thanks.
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Soni Prasad
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok I got it actually in the first code method was not overridden thats why parent's method is called.

Thanx anyway...
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all. I understood the point.
 
Hey, sticks and stones baby. And maybe a wee mention of my stuff:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic