Marquake
Marquake

Reputation: 191

Correct hierarchy in Java with correct Class modifiers for getters and setters methods

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.

Upvotes: 0

Views: 653

Answers (3)

SkyWalker
SkyWalker

Reputation: 29170

In simple, You can declare variable as private. But related getter/setter must be public.

Hope it will help you.

Actually We use getters and setters for

  • Reusability
  • to perform validation in later stages of programming
  • Getter and setter methods are public interfaces to access private class members.

Encapsulation Procedure: The encapsulation procedure is to make fields private and methods public.

Getter Methods: We can get access to private variables.

Setter Methods: We can modify private fields.

Even though the getter and setter methods do not add new functionality, we can change our mind come back later to make that method safer and faster.

Upvotes: 1

faizal vasaya
faizal vasaya

Reputation: 492

There are various ways of achieving encapsulation.

  1. The best way is to make public methods in base class to access private data member. That is what you have applied.
  2. The other way is to make your base class members protected and public methods in subclass (that inherits them) to get and set them.

As per my knowledge and other JAVA books authors like Paul Deitel prefers the first method to achieve maximum encapsulation.

Upvotes: 2

Saif
Saif

Reputation: 7052

About getter/setter there is very simple rule :

If you declare variable in class A getter/setter should be provide in class A. If you need any mutation in child class then override.

This make code more readable and easy to debug.

BTW you can't write getter/setter of id anywhere other then class A because it's private. SO, also in this case the above theory complies.

Upvotes: 1

Related Questions