Advertisements
Advertisements
Question
With the help of an example explain how you can access a private member of a base class.
Short Answer
Solution
A private member of a base class can be accessed by calling the base class's public method that returns the private member. This is shown in the example below.
class A {
private int x;
public A() {
x = 10;
}
public int getX() {
return x;
}
}
class B extends A {
public void showX() {
System.out.println("Value of x = " + getX());
}
}
shaalaa.com
Is there an error in this question or solution?