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 Inheritance  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inheritance  " Watch "Inheritance  " New topic


Johnson David

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()");
class Child extends Parent
public void method1()
System.out.println("Child's method1()");
public static void main(String args[])
Child p = new Child();

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

I agree. Here's the link:
subject: Inheritance
It's not a secret anymore!