This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inheritance  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inheritance  " Watch "Inheritance  " New topic
Author

Inheritance

Johnson David
Greenhorn

Joined: May 22, 2005
Posts: 27
I have a question for all, regarding inheritance

take a look at the fallowing code

class Parent
{
public 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[])
{
Child p = new Child();
p.method2();
}
}

the output of the above code is

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

this is ok ,

but i will make a small change in the above code , i replace

"private" in place of "public" in method1() of parent class , output of

the code after the changes are

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

private means, it should not be accessible from the subclass,

but why the output is comming like this, can anybody explain me?
Nischal Tanna
Ranch Hand

Joined: Aug 19, 2003
Posts: 182
private member is not accessible to a child class !!!... its only accessible within the class.. Also, if u change from public to private, the method method1() cannot be overriden since it is not available to the child... It can be just hidden...Hope this helps


Thnx
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance