Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good design: How use fields of superclass [duplicate]

Tags:

java

oop

Possible Duplicate:
Java protected fields vs public getters

If i have that class B extends A and in A i have some fields that i use also in B, it's better make this fields protected and call them from class B or write getter methods for this fields and so use this method from class B ? (this fields are setted in constuctor of A)

like image 505
ech0s7r Avatar asked Mar 19 '11 12:03

ech0s7r


1 Answers

If you prefer getters to public member data in your public interface (as you should), then use protected getters for the same reason. Subclasses, like public clients, are classes outside your control which don't need unfettered access to your internals.

like image 190
fizzer Avatar answered Sep 23 '22 13:09

fizzer