0

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.

Community
  • 1
  • 1
Marquake
  • 191
  • 1
  • 3
  • 10
  • 1
    What is the problem you are trying to solve? B and C should be expected to inherit the methods of A. Using getters/setter is to a strategy to achieve encapsulation. – Peter Lawrey Mar 08 '16 at 11:38
  • 1
    private properties of a class don't get inherited by child classes. Therefor you will have to use the getters and setters in the child classes obviously. if you want to directly access private variables like "id" from child classes, you need to declare them as protected. – Imesha Sudasingha Mar 08 '16 at 11:38

3 Answers3

2

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.

faizal vasaya
  • 517
  • 3
  • 12
1

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.

Saif
  • 6,804
  • 8
  • 40
  • 61
  • Ok, I understand. If I always use "protected" modifiers for methods getters and setter I always can use these methods from son classes and grandson classes. – Marquake Mar 08 '16 at 11:55
  • yah.. but it's better to make the setter/getter public unless you have any reason to make them protected. I i am talking about simple Java bean class – Saif Mar 08 '16 at 12:00
  • What you says, it make me thinking... And I have searched some documentation and I found this: [link]http://docstore.mik.ua/orelly/java-ent/jnut/ch06_02.htm[/link]
    6.2.7. Methods A beanbox can expose the methods of a bean to application designers. The only formal convention is that these methods must be declared public. The following guidelines are also useful, however:
    – Marquake Mar 08 '16 at 12:37
  • `The only formal convention` this is what I was talking about. I am happy you found it. – Saif Mar 08 '16 at 12:40
1

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.

SkyWalker
  • 28,384
  • 14
  • 74
  • 132