Before nothing,
my problem is simmilar to these: get super class value in java Getting the name of a sub-class from within a super-class
but not solve my problem. So, I'm going to explain it. I expect no repeat a topic...
I'm creating a hierarchy. This hierarchy is only for Object with getter and setter. Just for show information.
Well, I want to do correctly my hierarchy but without do something ilogical with modifiers. This is a simplified example of my Classes, is not exactly Java is pseudocode:
Class A
private id;
Class B extends A
private dataB;
Class C extends A
private dataC;
Variable "id" is common for Class B and Class C. Because that B and C extends A. I'm thinking that never I going to use Class A for show data, just B and C, like if A were an Abstract Class.
My problem is:
I don't know if is correct put methods getter and setter in class A with modifiers public and use this methods from Classes B and C, because the hierarchy is correct, is logical but the data "id" is not correctly encapsulated.
Class A
private id;
private name;
private age;
public getId(){...}
public getName(){...}
public getAge(){...}
Class B extends A
private dataB;
public getDataB(){...}
Class C extends A
private dataC;
public getDataC(){...}
To access to my object I want to do this:
B.getDataB();
B.getId();
B.getName();
B.getAge();
C.getDataC();
C.getId();
C.getName();
C.getAge();
This works but all method of Class A must be public, the variables aren't correctly encapsulated.
Are there other ways to do this? Is this the best/worst way? Getters and setters could be an exception to "jump" the logical of the modifiers?
I excpect you could understand my example and my "English".
Thank you in advanced.