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 8092577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:18:56+00:00 2026-06-05T20:18:56+00:00

Lets say I have a class A that is fairly simple like this –

  • 0

Lets say I have a class A that is fairly simple like this –

public class A{
  private int randomField = 0;
  protected int key;
  protected double dmg;
}

Now I want to write a number of sub-classes that inherit the protected fields and only differ based on the initial values that are assigned to those fields – for example, if I wrote two subclasses B and C, the only difference between those two sub-classes would be that the values key and dmg would have different values. They would share a method, set, which would be exactly the same, in that it would affect the same variable.

I find when I’m writing these sub-classes I’m repeating myself, as I just change the constructor to set different initial values to key and dmg, and simply copy and paste the set method.

Is there a ‘good’ way to do this?

  • 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-06-05T20:18:57+00:00Added an answer on June 5, 2026 at 8:18 pm

    This is not a place where you should be sub-classing and in fact is a gross over-use of inheritance. If the only difference between “subclasses” are initial states of the objects, then use only one class for all, and instead simply set the states in a constructor or factory to create instances of the desired state.

    e.g.,

    public class A{
      private int randomField = 0;
      private int key;
      private double dmg;
    
      public A(int key, double dmg) {
        this.key = key;
        this.dmg = dmg;
      }
    
      // getters and possibly setters...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a very simple PrototypeJS class that looks like this: var
ok, lets say I have this: class A: { Public: int dd; } Ok,
Lets say I have a controller class like this. public class InStorePickupController : Controller
Lets say I have a set of model classes like this: public class Person
Lets say I have a class like this: public class Foo { public Foo
Lets say I have a class that is doing something like: public class Foo
Lets say that I have the following code: public class Shelter<A extends Animal, B
Let's say I have a fairly large IList<foo> where foo looks like this: public
So lets say I have this interface: public interface IBox { public void setSize(int
Lets say you have a class MyClass that has a constructor public Myclass(SomeObject o)

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.