Arun Kumar Choudhary
Arun Kumar Choudhary

Reputation: 13

Why Private data member inherited in child class

I have this piece of code

public class Base {
    private int x=10;
     void show(){
         System.out.println(x);
     }

}


public class Child extends Base {

    public static void main(String[] args) {

        Child c1=new Child();
        c1.show();

    }

}

This piece of code is working fine and output is 10.Can anyone please Elaborate how this private data member is access in child class..

Upvotes: 1

Views: 809

Answers (1)

Maarten Bodewes
Maarten Bodewes

Reputation: 93958

It isn't. The show() method is accessed. That method of the parent then accesses the field x. The show() method has default access, which includes access by the Child as it is in the same package.

Upvotes: 8

Related Questions