1

Look at the code below:

class Rodent {
    private String hello = "hello" ;

    public void printHello() {
        System.out.println(hello);
    }
}

public class Mouse extends Rodent {

    public static void main(String[] args) {
        Mouse mouse = new Mouse();
        mouse.printHello();

    }
}

If we see , the method printHello() is inherited. This method is inside using a private variable. But this private variable is not inherited. Then how does this code works ?

Is it something similar like closure in JavaScript ?

If method printHello() is inherited and it needs some private variable , then this also has to be made available in Mouse as now I am calling printHello() in Mouse ?

Edit : For those who say that parent class method will be called , I have found that there is only object created and not separate objects one for sub class and one for parent class. Here

Number945
  • 4,631
  • 8
  • 45
  • 83
  • Possible duplicate of [Do subclasses inherit private fields?](https://stackoverflow.com/questions/4716040/do-subclasses-inherit-private-fields) – PM 77-1 Apr 09 '18 at 18:49
  • 3
    Who says private variables aren't inherited? Of course it is inherited. Only the access is restricted via the parent. – user unknown Apr 09 '18 at 18:49

2 Answers2

2

Please see:

Do subclasses inherit private fields?

The instance of subclass contains its superclass private fields. So, in a sense private String hello is inherited.

But as a private field it is not accessible outside of Rodent. But it's also not accessed outside of Rodent in your code.

lexicore
  • 42,748
  • 17
  • 132
  • 221
  • Just wondering if it is right to say it is *inherited* (From the update to DigitalRoss's answer on that question) – Thiyagu Apr 09 '18 at 18:57
  • @user7 , no we cannot say it is inherited however it is `present` in the object so created. – Number945 Apr 09 '18 at 19:02
  • @user7 My wording is "in a sense". In a sense that this private field of the superclass is actually contained in an object instance of subclass. Strictly speaking - you're probably correct. – lexicore Apr 09 '18 at 19:04
  • @BreakingBenjamin What is the difference between an inherited method and an inherited field then? – lexicore Apr 09 '18 at 19:06
  • @lexicore See every thing is `present` in the object only , in the end , but among what is `present` , we say `inherited` only for as JLS dictates : https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.2 Correct me If I am wrong. – Number945 Apr 09 '18 at 19:12
  • @BreakingBenjamin Strictly speaking, you are right, yes. Therefore the *in a sense* wording which is my only excuse here. – lexicore Apr 09 '18 at 19:14
1

mouse.printHello()

Calls the printHello method in class Rodent and not Mouse and hence it has access to the private variable hello. As you have said, it is inherited.

Think of it like this: (though the terms are not correct)

Class Rodent has the method printHello and private variable hello. But the method being public, it is visible from Mouse (but still it is in the Rodent class).

However, if you add a method to Mouse and try to access the variable hello, it will result in a compilation error.

Thiyagu
  • 17,362
  • 5
  • 42
  • 79