enter image description here

Here I have created 2 classes , $A$ is the super class of $B$ , i.e , $B$ inherits the features of class $A$.

Now I don't know exactly what statement (1) does but I am speculating as follows ,kindly correct me if I am wrong ,

An object of $B$ is created whose address has been assigned to the variable $ob$ of class $A$.

Now when statement (2) is called the $add$ function of the class $B$ is executed , but when statement (3) is executed it gives an error saying $e$ doesn't belong to $A$. Why is that ?

With my understanding , I thought statement (1) creates an object of class $B$ via its parent class $A$. Then why its object can't access its data member ? Can anyone explain ?

  • $\begingroup$ We discourage images as the main content (relevant parts should be transcribed into text). Strictly programming questions are off-topic here. Java tag is not used here in the context of the code debugging. $\endgroup$
    – Evil
    Oct 25, 2016 at 16:03
  • $\begingroup$ Sorry. A new user here. Will keep that in mind. Thanks ! @Evil $\endgroup$
    – User9523
    Oct 25, 2016 at 18:48

1 Answer 1


The variable $ob$ is an object reference variable of type $A$. As such, it can only reference those attributes and methods defined in $A$. The attribute $e$ is defined in $B$, not $A$, so $ob$ cannot reference $e$. It is okay to reference $add$ because it is defined in $A$, even though it is overridden in $B$.

  • $\begingroup$ Just read about it , Method overriding takes place here. Thanks anyway :) $\endgroup$
    – User9523
    Oct 25, 2016 at 14:50
  • $\begingroup$ This is method overriding. Method overloading is another topic. $\endgroup$
    – tjhighley
    Oct 25, 2016 at 14:50
  • $\begingroup$ By mistake.. :P $\endgroup$
    – User9523
    Oct 25, 2016 at 14:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.