English

With the help of an example explain how you can access a private member of a base class. - Computer Applications

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?
Chapter 8: Encapsulation and Inheritance - EXERCISES [Page 446]

APPEARS IN

Avichal Computer Applications [English] Class 10 ICSE
Chapter 8 Encapsulation and Inheritance
EXERCISES | Q V. 10. | Page 446
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×