Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • 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

public ...private....

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i came across the following question..

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();
}
}

according to my knowledge the o/p should be

Parent's method2()
Child's method1()

but the o/p is

Parent's method2()
Parent's method1()

and the explanation given for such a o/p is:

Explanation:
Because method1() of the Parent class is private, it will be invoked. Had method1() of the Parent class been public, protected, or friendly (default), the Child's method would have been called.

i am unable to understand....the explanation...pls help

thanking u
amal
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Private methods are private. They are only visible to the class in which they are declared. So your class Child doesn't know about the method1() in class Parent.

The method1() in class Child does not override the method in class Parent - it is just a completely new method.

So if you turn it around and look from the view of class Parent, the private method1() is not overridden by the method1() in class Child - so if you call method1() in class Parent, it just calls its own private method.

If you are using Java 5, you should always annotate methods that you intended as overridden versions of the super class with the @Override annotation. If you do that, the compiler will warn you for situations like this, where your method is not actually overriding a super class method:
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With other words, private methods aren't polymorphic.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic