3

I am aware that if you subclass a class (superclass) which has for example an instance variable, it is going to be inherited in the subclass.

i.e. :

class Weather {
    int humidity;
}

public class Rainy extends Weather {
    void changeHumidity() {
        humidity = 10;
        System.out.println(super.humidity);
    }

    public static void main(String[] args) {
        new Rainy().changeHumidity();
    }
}

Bottom line: I would like to know why the instance variable "humidity" gets shared between subclass and class.

I am aware that if I shadowed that, it would not be shared but still why another instance (even though is the superclass) should share a variable with the subclass in the inheritance logic.

Thanks in advance.

6
  • 2
    Remember that every Rainy is also a Weather. It can be used wherever a Weather is expected, so it needs to have the attributes of a Weather, so that Weather methods will work. Commented Feb 4, 2013 at 21:32
  • 1
    The humidity field isn't visible to Rainy because it's a subclass of Weather, but because the two classes are in the same package. Subclasses of Weather that are in different packages would not be able to see the field. If you made it protected then it would be visible in all subclasses regardless of package (as well as all other classes in the same package, subclass or not, there's no visibility modifier in Java that can make members visible to subclasses but hidden from the rest of the package). Commented Feb 4, 2013 at 21:40
  • @PatriciaShanahan Hi Patricia, yes I am aware of what you are saying, however I do not understand, let's say we have a house and a villa and I want to change only the value of the Villa (int square meters;). Let's say that by mistake I forgot to shadow that instance variable. I am going to change also the value of The instance House even though I want to change only the isntance Villa. I get it and i will shadow the variable in the future, though whereas it seems logic to inherit and share a static variable, it does not seem so logic to share an instance variable between super and sub-class Commented Feb 4, 2013 at 22:14
  • @Rollerball You have a house that happens to be a villa. Surely it should have the same size in square meters regardless of whether you are currently thinking of the building as a house or a villa. Commented Feb 5, 2013 at 0:09
  • First, WHY is this asker repeating the same question under a different avatar as this question: stackoverflow.com/questions/42592348/… ? Commented Oct 20, 2017 at 16:57

2 Answers 2

8

There were two decisions the creators of Java took that guided how they designed Java. One was that performance was a priority, it must not be judged unusable due to being too slow. The other decision was that they would target C and C++ developers, and make Java similar to what they were used to in order to make adoption easier.

So Java got public, private, and protected access like C++. Where package-private came in and why it is the default is not clear. They might have assumed developers would want to access variables directly in order to save on method call overhead. A long time ago I was working on a project with professional services people from Marimba, and we had the opportunity to investigate some of the implementation code, which was written with a lot of package-private members. When I asked why the explanation was it was for performance. This was before optimizations like JIT, so there may have been a concern that using accessor methods to get to frequently used superclass members might be too slow. On the other hand it might have been a style preference or they were using a freer approach as part of an evolving design, and maybe the idea behind making package-private the default was that this kind of freedom should be expected. (It's not something that has caught on judging by any other code I've read.) It was code written by Jonathon Payne and Arthur Van Hoff, so the professional service people may not have been in on the real reasons.

There's an interview where James Gosling talks about his reasons:

... one of the things that I had wanted to do early on was formalize setting and getting something in the language. You know how in JavaBeans there's this sort of convention that you write setter and getter methods? But I did a bunch of surveys of developers at the time, about whether or not they would like this to be there. And the average person went, "Oh my God!"

And so I didn't do it. But I think in retrospect, I should never have listened to them. I should have just done it. Because, I mean Beans basically layered on this facility that I had wanted to do anyway, but Beans did it as kind of an afterthought.

And because it's layered on as a naming convention, there's some things that don't fit together real well. And so, for instance, it would've made a lot of sense for the default protection for an instance variable to be private. And then the getters and setters, which the system would've known about at a much deeper level, to make them either public or package.

The direction in which the style has evolved has been to prefer making instance variables private, and expose them through getters if required. You can specify the private access modifier, like this:

private int humidity;

and then the variable won't be visible to the subclass.

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

5 Comments

Do you have any actual evidence for this claim?
@EJP: tried to improve the explanation, thank you for the feedback.
A bit off-topic here, but how does scoping in JavaScript not make sense?
@Mattias: the language appears to have block-scope but doesn't. also using this in an anonymous function resolves to the global window object in some cases (sorry, my js is rusty, see the function chapter of crockford's goodparts book).
@NathanHughes You just need to remember that JS has function-scope and that the this context is determined by the way you call the function (not where it's defined). I agree that it should not refer to the global scope though, luckily strict mode "fixes" that.
3

I would like to know why the instance variable "humidity" gets shared between subclass and class.

That is what default/package local members do.

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.