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

2
  • Possible duplicate of Do subclasses inherit private fields? Commented Apr 9, 2018 at 18:49
  • 3
    Who says private variables aren't inherited? Of course it is inherited. Only the access is restricted via the parent. Commented Apr 9, 2018 at 18:49

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Just wondering if it is right to say it is inherited (From the update to DigitalRoss's answer on that question)
@user7 , no we cannot say it is inherited however it is present in the object so created.
@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.
@BreakingBenjamin What is the difference between an inherited method and an inherited field then?
@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 : docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.2 Correct me If I am wrong.
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.