Hiding of Base Class member variable from sub class in java
- If a super class and sub class have members with the same name then super class members are still inherited but they are hidden by the member of sub class.
- We can access them by qualifying with keyword super.
Example
class ParentClass {
int x;
public void setx(int a)
{
x=a;
}
public void show()
{
System.out.println("Value of x in ParentClass "+x);
}
}
class childclass extends ParentClass
{
int x;
public void set(int a,int b)
{
super.x=a;
x=b;
}
public void show()
{
super.show();
System.out.println("Value of X in child class "+x);
}
}
public class MainClass {
public static void main(String[] args) {
// TODO Auto-generated method stub
childclass obj=new childclass();
obj.set(3, 4);
obj.show();
}
}