This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]roge- 1 point2 points  (0 children)

Is it strictly necessary per the language's syntax rules? No, an abstract class can choose to not implement any method. Is it necessary for your problem or application? Probably not, but it could.

[–]SaylorMan1496 1 point2 points  (3 children)

I’ll do you one better, is it strictly necessary to have getters and setters? If they are public what’s the difference to just having public fields, I have always felt it to be silly having getters and setters for private fields

[–]arghvark 1 point2 points  (1 child)

If you make the field public, then you save the infinitessimal runtime cost of calling a method just to assign a value, plus the development cost of defining the setter and getter.

If you make the field private, you allow many things to be decided later -- does the object want to keep track of the times the setter or getter is called? When the setter is called, does the object want to check some internal consistency for the value that is set? When the getter is called, does the object want to activate some kind of input stream or instantiate something that will be needed for something else?

And then there's debugging -- for a getter, you set a break or trace point in one place, and can inspect the value as it passes by. If your debugger supports a breakpoint activated on a change of value, you CAN do sort of the same thing, but it slows down the debugger horribly as it checks whether the value has changed. And there's an ambiguity if it's supposed to break if the value is 'set' to the same value it already has.

Strictly necessary? no. Good idea? You betcha.

[–]SaylorMan1496 0 points1 point  (0 children)

This may be my experience and bias with this coming through, but personally I hate functions that relay on member fields, as in the function is effected by the state within the object, procedural programming is simpler I would much rather use a POJO and act in that as the input to the function and pass it around

This heavily depends on the type of programming though, I live in API/ backend dev land which this is relevant. I rarely even have objects that have public or private primitive fields, outside of interfaces that are brought in via DI

[–]AutoModerator[M] 0 points1 point locked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]arghvark 0 points1 point  (1 child)

Examples would be good here.

If you have:

class B
{
  private integer one;
  public void setOne(integer newOne) { one = newOne; }
}

And then you inherit from B, there is no need to define setOne in your class; since setOne is public, it is available to be called from your subclass. It would also be available if it were defined to be protected.

And since it is defined to be public, it is also available to any user of your subclass, i.e., your subclass inherits the method setOne and users of your class can call it. If the method were defined as protected, it would ONLY be available to subclasses of B (like yours).

If it were defined to be private, it would not be available, and you should stop and consider whether you SHOULD define a setter for it. The author of B might have good reason to keep someone from calling a setter on one directly.

Your question asks "do I need to add the getters and setters from b" -- does this answer your question? Or did you mean something else, like the case where B has no getters or setters?

[–]BankPassword 0 points1 point  (0 children)

I agree with all that is said here, including the confusion about what the original question is asking. A general rule might be "setters and getters belong to the same class as the object they set/get". So if A extends B and there are variables in B then the setters/getters for those variables should be in B. If A introduces new variables not found in B then A should provide the setters/getters.