Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

protected data in abstract class

My question involves specifically Java, abstract classes, and the use of protected data. I am being told that all the data should be private, and protected getters/setters used only.

Now, I understand we want to shield data from direct manipulation by casual users of the class, and that public data members in general are a questionable practice. I have looked at "Java protected fields vs public getters" ( Java protected fields vs public getters ), but I still am dubious that:

protected int i;  

is worse in an abstract class than:

private int i;  
protected int geti();  
protected void seti(int j); 

I am just not seeing the down side when the abstract class is there precisely to provide parent/common facility to the children classes, and the protected scope is meant to provide access to children, while protecting the data from casual users. I note in the question referenced above, that most of the answers seem to address the issue of why data in general should be private rather than public. I am trying to focus my question specifically on data existing in an abstract parent intended for use by the children. The sole reasonable comment I have heard to date is that using the parents protected data (e.g., int i above) leaves you with code in the child class that references a variable not declared in the child class. Less compelling is the argument (see Common protected data member in base class? ) that you may want to change the access some day, and now you have to honor your interface. This is an abstract class, and is intended to be extended 100% of the time.

Thanks! Specific Title/page# references to books are far more helpful that references to "..any basic Java programming text..."

========================================== 10-13-2010
This was as much a question about abstract classes as it is about protected data. I find it disappointing that the focus seems to have shifted in the responses to whether data hiding is a good thing in OOP (answer: yes). There's a lot of depth here involving the nature of the abstract class, and how it differs from a regular non-final class, and what possible advantages there might be for fixing the names and types of data-items in the abstract parent for use by the child classes. I think there is the possibility here for innovation and greater control being extended down from the abstract parent to the implementing child classes. I am concerned that general principles, such as the advantages of data-hiding, can become dogma, and inhibit innovation and the development of new patterns and ideas.

Thanks to all who contributed.

like image 895
cvsdave Avatar asked Aug 19 '10 20:08

cvsdave


People also ask

Can we use protected in abstract class?

Yes, you can declare an abstract method protected. If you do so you can access it from the classes in the same package or from its subclasses.

What is not allowed in abstract class?

Abstract methods cannot be declared private or static. Any class that extends an abstract class must implement all of its abstract methods; otherwise, that class needs to be declared abstract as well.

Can abstract class have private variables?

Abstract classes can have private methods. Interfaces can't. Abstract classes can have instance variables (these are inherited by child classes).


1 Answers

If the field is private and access is through getters and setters, you will be able to reimplement getters and setters (for instance, dropping the field and updating/reading the value from an external source), and thus change how the "field" works without touching any child classes.

Whether this is worth it, that's up to you.

like image 58
alex Avatar answered Sep 20 '22 09:09

alex