Computer Applications
With the help of an example explain how you can access a private member of a base class.
Answer
Private member of a base class can be accessed through a public method of the base class which returns the private member. This is illustrated 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());
}
}
Related Questions
What is the significance of using protected declaration during inheritance? Show with the help of an example.
Main class by default inherits another class. Explain the given statement.
Why is the main method in Java so special?
What is meant by scope of variables? Show its application with the help of an example.