Setzer22
Setzer22

Reputation: 1689

Visibility issue in Java

I come from C++, and actually I haven't really used class inheritance at college yet, so I'm learning by myself. I'll put my doubt in the form of an example:

Let's say I have a class called Entity. This entity class holds two integers representing some X and Y coordinates (this is not relevant, it could be any other field). Then I have my class Player, extending Entity. As it might seem usual, I want Player to use as well the x and y fields, and some code in the Entity class refers to those fields as well.

Now comes my doubt. Are x and y public, private or protected? I feel like they should be protected, as public is not an option (Other classes should'nt be accessing it) and private doesn't allow me to use the x and y fields from the extended (Player) class. But I read elsewhere that class fields should never be protected, only methods.

Other sollutions to this other than setting the variable to protected that come into mind would be using getters and setters from the super class (which in my opinion is not only a bad practice but completely redundant and unnecesary), or re-declaring those attributes in the new class, which let's me thinking if I'm getting any reward from inheritance if I'll have to type all the field declarations twice. I actually don't even know if this last thing might work as I don't know Java that well to know how it handles this visibility issues.

Any help on the topic would be much appreciated.

Thank you.

Upvotes: 1

Views: 342

Answers (4)

Hurda
Hurda

Reputation: 4715

It really depends on purpose of the Entity class. If its just holder of x and y - because many of your classess will use them then it's perfectly fine to make them protected.

If there is any logic bound to those coordinates - especialy validation, some event upon change. Then you need to use getters/setters to encapsulate this logic inside Entity class.

Nothing is set in the stone and you can allways reworke this - as long as other developers does not depend on that. If you have other dependent on your code - you have to design more safely. But don't overdesign.

Upvotes: 1

scottysseus
scottysseus

Reputation: 2020

Couldn't you use no access specifier in this case? Using protected (unless I am mistaken) allows all subclasses, including those from different packages, to have access. In most (but not all) cases in which you are just making a little program for yourself, if you want a subclass to have acces to a superclass's variable, you could just have no access specifier.

This picture should give you information about all of the different access specifiers. The 'Package' column refers to subclasses within the same package, and the 'Subclass' column refers to subclasses regardless of package. enter image description here

Upvotes: 0

Ankit
Ankit

Reputation: 6622

if you want variables to be accessible through subclass, you need to make them protected. but still those will be accessible from other classes but in same package. however those will be protected from classes outside of the package, except those who inherit your class where the variables are defined.

And yes, instead of making variables themselves as private, you should define setters to access them. It is not at all bad practice, in fact its best practice in all object oriented programming.

Upvotes: 2

arshajii
arshajii

Reputation: 129587

[Using getters and setters]... which in my opinion is not only a bad practice but completely redundant and unnecessary

Actually no, this is often considered the best approach. There are a variety of reasons to favor it, ranging from maintainability to security. You would make the fields private, and declare methods in the base class to both access and alter them.

Upvotes: 4

Related Questions