Ronan H
Ronan H

Reputation: 151

Java - Making inherited variables PRIVATE

I need to make my inherited instance variables private; is this possible?

IE, Superclass "Entity" has an int instance variable "health".

How can subclass "Zombie" (Extends "Entity") inherit the health variable from Entity, and have it private? I don't want other classes to be able to directly access the health variable, I want set and get methods for it.

Tell me if I wasn't specific enough; any help appreciated.

Upvotes: 0

Views: 302

Answers (4)

Rohit Jain
Rohit Jain

Reputation: 213223

You cannot decrease the visibility of any instance variable or method of the super class in your subclass..

Suppost you have super class with a public method.. And, suppose you were allowed to decrease the visibility to private in sub class..

Then see what's happens when you create object like this, and access that method of super class: -

SuperClass obj = new SubClass();
obj.pubMethod();

Now, at compile time, compiler sees that method pubMethod() is public in SuperClass, it will allow the access.. Note it does not check for the instance type on the RHS..

Now, at runtime, when JVM checks that the instance is of SubClass, then the actual method it would try to invoke will be searched in the SubClass..

But wait.. Did you see what happened when JVM went to search for pubMethod in SubClass that you made private.. BOoooooMMM -- A Crash..

That's why it is not allowed..

So, you cannot make it private..

From JLS Section - 8.4.8.3: -

The access modifier (§6.6) of an overriding or hiding method must provide at least as much access as the overridden or hidden method, or a compile-time error occurs. In more detail:

  • If the overridden or hidden method is public, then the overriding or hiding method must be public; otherwise, a compile-time error occurs.

  • If the overridden or hidden method is protected, then the overriding or hiding method must be protected or public; otherwise, a compile-time error occurs.

  • If the overridden or hidden method has default (package) access, then the overriding or hiding method must not be private; otherwise, a compile-time error occurs.

Upvotes: 2

Stefan
Stefan

Reputation: 4705

You cannot add restrictions to an instance variable in a subclass, i.e. of health is protected in Enitity it cannot be private in Zombie (you can make it public).

However, you can make health private in Entity and define a protected getter and setter there. Subclasses can use these methods.

Upvotes: 1

Marko Topolnik
Marko Topolnik

Reputation: 200148

Make the variable private in the superclass and let all subclasses use accessor methods.

A second choice would be to make it package-private and arrange for those classes to which you want to deny access to be in another package. This would make sense if your Zombie is especially close to Entity (shares many internals) whereas other classes are more loosely coupled to their superclass.

Upvotes: 2

Denys Séguret
Denys Séguret

Reputation: 382102

Simply : you can't.

This would break the contract of the superclass. Your class, being an Entity, exposes, like its superclass, a field named health. If you had the ability to make it private, all code using this field in instances of Entity (including instances of subclasses of Entity) would break with your class...

If you can, change the superclass (ie Entity) to make the field private. That's the common practice.

Upvotes: 3

Related Questions