-1
class Weather{
    int humidity;
    //default value is 0;
}

class Rainy extends Weather{



    void changeHumidity(){
        humidity = 10;
        System.out.println("The humidity is " + super.humidity);
    }


}

public class Test{
    public static void main(String[] args){
        new Rainy().changeHumidity();
        System.out.println(new Weather().humidity);
    }
}

Here the output is : The humidity is 10 and 0

why super.humidity returns 10.I know that instance variable are not inherited but they can be accessed in sub class.If they can be accessed in sub class then does that means that they are shared between super class and subclass or both super class and subclass have different copy.Now coming to question why super.humidity returns 10 but in next line it returns 0.Kindly make my concept clear please.

1
  • 1
    The problem has nothing to do with inheritance. You create two distinct objects, one Rainy which is immediately discarded, and one Weather which is used to print humidity and also immediately discarded. Commented Mar 4, 2017 at 5:50

3 Answers 3

2

In Rainy class doesn't exist field humidity so this class use parent class field for initilize

if you decleare humidity field in child class change be there but print parent class field

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

Comments

2

Each instance of Weather and Rainy has its own copy of humidity.

Within your changeHumidity() method, you reference both humidity and super.humidity. These are referring to the same instance variable. When you create a new Rainy, it's inheriting everything from Weather, which is why you can use humidity in the first place.

However, when you create a new Weather, that has absolutely nothing to do with your instance of Rainy. Lets assume we have two objects here:

Rainy rainy = new Rainy();
Weather weather = new Weather();

Each one of the objects above has its own copy of humidity. Changing the humidity on the instance rainy does not change the humidity on the instance weather.

rainy.humidity = 20;

System.out.println(weather.humidity); //-> 0

4 Comments

in Rainy class i changed humidity to 10 then why super.humidity gives 10 as a output as super.humidity points to Weather class humidity variable whose value is 0 bu deafult
also why super.humidity changed to 10 when i changed humidity in sub class
if i created Rainy class object then it will have access to both humidity and super.humidity so does that means that humidity and super.humidity refers to same value and change in one will reflect in other also
humidity and super.humidity in Rainy are literally the exact same variable. They are a single location in memory, referenced in different ways. In fact, when you say humidity, the compiler treats it as super.humidity under the hood. It's just syntax.
1

humidity is an instance variable.

Instance Variables :

These variables belong to the instance of a class, thus an object. And every instance of that class (object) has it's own copy of that variable. Changes made to the variable don't reflect in other instances of that class.

new Weather() will create a new instance

public class Test{

   int x = 5;

 }

Test t1 = new Test();   
Test t2 = new Test();

t1.x=10 will not make any changes to t2.x. t2.x will still be 5

2 Comments

does that means that instance variable is shared between both classes or does it means that both classes have diff copy of humidity
Different copies of instance variables in different instances of that class. The example should make it clear

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.