Use of private member variable of super class in subclass
What you will learn?
- How to access private member variable of parent class into child class.
- Hiding super class variable.
Some Important factors
- An inherited member is that member of base class that is freely accessible to the methods of the derived class.
- A base class private members are never inherited but they will still from part of derived class object.
Example
class ParentClass {
private int x;
public void setx(int a)
{
this.x=a;
}
public void showx()
{
System.out.println("Value of x is "+x);
}
}
class childclass extends ParentClass
{
private int y;
public void setxy(int a, int b)
{
setx(a);
y=b;
}
public void showxy()
{
showx();
System.out.println("Value of y is "+y);
}
}
public class MainClass {
public static void main(String[] args) {
// TODO Auto-generated method stub
childclass cobj=new childclass();
cobj.setxy(20, 39);
cobj.showxy();
}
}