0
public class Base
{
    private int num;
    Base(int n)
    {
        num=n;
    }
    public void display()
    {
        System.out.println(num);
    }

}

class Sub extends Base
{
    Sub()
    {
        super(20);
    }   
}

class Main
{
    public static void main(String[] args)
    {
        Sub s=new Sub();
        Base b=new Base(10);
        s.display();
        b.display();
    }
}

Output:

20
10

In above program code, 2 values are stored in the memory 20 and 10, that means while creating object for child class memory allocation for data member num has done. Whether that means private data member num gets inherited..? These are my views. But in java clearly stated that private members are not inherited..

Someone please provide me with the true logic

Imran Ali
  • 2,223
  • 2
  • 28
  • 41
noyal_asok
  • 374
  • 1
  • 13
  • I don't think you have access to `this.num` from Sub class – OneCricketeer Aug 04 '16 at 04:03
  • 1
    No, they don't. And please note: you are expected to do prior research. Meaning: you are a beginner. Probably **everything** you can image to ask here ... has been asked here before. Many times. And most like got good answers! So please take some time ... and try to find your answers without repeating what others have asked before. – GhostCat Aug 04 '16 at 04:03
  • Where's that stated? – acdcjunior Aug 04 '16 at 04:04
  • "But in java clearly stated that private members are not inherited.." - where is this mentioned? – James Jithin Aug 04 '16 at 04:04
  • @acdcjunior OK, to be precise: of course the fields "exist" when you instantiate an object of a derived class. But: there is **no** way to access them. And that is perfectly fine. Private stuff means "implementation details"; and no other class (even derived classes) should know about implementation details. – GhostCat Aug 04 '16 at 04:05
  • @JamesJithin - See here. *A subclass does not inherit the private members of its parent class* - https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html – OneCricketeer Aug 04 '16 at 04:07
  • @GhostCat yes, of course, I agree with you. My question was actually meant for the OP (but I could have been more specify, I'd say). – acdcjunior Aug 04 '16 at 04:09

0 Answers0