Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3871134
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T21:47:40+00:00 2026-05-19T21:47:40+00:00

I am writing an abstract class and I have a variable that should be

  • 0

I am writing an abstract class and I have a variable that should be defined by the sub-class (in this case it is an int which is used in the super-class). I can’t decide whether to define a protected variable with a default value and let the sub-class change the value either in the constructor or through a setter method. Or to define an abstract method in the parent class such that all sub-classes must implement it and return the value they want to be used (then access the method in the super-class using the abstract method). Can anyone tell me if there are any great reasons why one way should be preferred over the other?

Abstract Method:

public abstract int getValue();

public int getValue() {
    return 5;
}
  • Pros:

Forces sub-class to think about this value and choose what they want it to be.

  • Cons:

No default value, so even if most of the sub-classes just want to use the same value they still have to implement the method. (The method can be made non-abstract and return the default value, but then you lose the advantage of forcing sub-classes to think about the value)

Protected Variable:

protected int value;

public SubClassImpl() {
    value = 5;
}
  • Pros:

Can define a default value and sub-classes can just ignore it if they don’t care.

  • Cons:

Authors of the sub-class aren’t made aware of the existence of the variable, so they might be surprised by the default behaviour.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T21:47:41+00:00Added an answer on May 19, 2026 at 9:47 pm

    Well, it depends…

    • Does the value have to be able to change during the lifetime of the object? If not, you might want to make it a protected constructor parameter, and make it a final private variable in your base class.

    • Might subclasses have reason to compute the variable based on other mutable state? If so, an abstract getter would be appropriate.

    • Otherwise, I’d probably use a private variable and a protected setter. I’m generally not a fan of non-private variables. This could allow you to react to changes in the value in your base class, such as changing other computer fields. To avoid the presence of a default value you could combine this with a constructor parameter to force an initial value which the subclass could change later via the setter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When writing an abstract class, or a class that doesn't get instantiated directly... do
I'm writing a series of math based class that each inherit from an abstract
I have an abstract class defining a pure virtual method in c++: class Base
Right now I have classes like: abstract class Record { // Required fields val
(Please no advise that I should abstract X more and add another method to
Design -- in a perfect world I have one abstract base class A with
Scenario: I'm currently writing a layer to abstract 3 similar webservices into one useable
Writing a JSP page, what exactly does the <c:out> do? I've noticed that the
Writing something like this using the loki library , typedef Functor<void> BitButtonPushHandler; throws a
I'm writing an application that do some task and inform user upon successful completion

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.