Choose Your Language

Tuesday 18 June 2013

Does subclasses inherit private fields of superclass in java?

Answer is YES.

public class MySubClass extends MySuperClass {
public static void main(String a[]){
MySubClass mySubClass=new MySubClass ();

System.out.println(
mySubClass.getPrivateVariable());

}
}

class
MySuperClass {

private int y=20;

public int getPrivateVariable(){
return y;
}

}


output is:
20

No comments:

Post a Comment