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

  • Home
  • SEARCH
  • 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 8196527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:30:28+00:00 2026-06-07T05:30:28+00:00

I want to design class hierarchy. Super class has an abstract method, sub class

  • 0

I want to design class hierarchy. Super class has an abstract method, sub class has to override this method, but my problem is: this method use data of super class (instance variable).

I’ve designed some style to solve this problem:

Style 1: Use private instance variable and create other instance variable in sub class

class SuperClass {
    private T data;

    public SuperClass(T data) { /* ... */}

    public abstract void doSomething();
}

class SubClass extends SuperClass {
    private T data; // use another variable

    public SubClass(T data) {
        super(data);
        this.data = data;
    }

    public void doSomething() {
        // use 'data'
        int length = data.getLength(); // for example...
    }
}

Style 2: Use protected instance variable:

class SuperClass {
    protected T data;

    public SuperClass(T data) { /* ... */}

    public abstract void doSomething();
}

class SubClass extends SuperClass {
    public void doSomething() {
        // use 'data'
        int length = data.getLength(); // for example...
    }
}

Style 3: Use private instance variable and protected method:

class SuperClass {
    private T data;

    public SuperClass(T data) { /* ... */}

    public abstract void doSomething();

    protected T getData() {
        return data;
    }
}

class SubClass extends SuperClass {
    public void doSomething() {
        // use 'data'
        int length = getData.getLength(); // for example...
    }
}

Which of them is better? Or have more solutions to solve my problem better?

  • 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-07T05:30:29+00:00Added an answer on June 7, 2026 at 5:30 am

    The third approach is usually preferred and, if you design for public subclassing, is a must. A superclass wants to maintain as much control as possible over its internals.

    If, on the other hand, you are developing a closed hierarchy that is all in the same package, or at least all in the same project, you can choose to expose the var as protected, if there’s any benefit to be had.

    Duplicating a private var in the subclass is a failure on all counts, however. I can’t think of any scenario where that would help anything.

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

Sidebar

Related Questions

Is this good OO Design assuming you want every inheriting class to be a
I want design my startup screen with progress bar. But I don't how to
I want to design a page in django that has a search bar in
I want to design a music player with ListBox has 2 rows to show
I have an abstract class A and several implementations of it. I expect this
I have a question regarding class design. I want to have a class that
I want to have an abstract class Employee, and 2 derived classes; Staff and
I have a naive question about OOP design patterns. This is over-architecture, but I
Let's consider the following simplified Resource hierarchy: public abstract class Resource { static public
I have an abstract class, AbsClass that implements an interface, IClass . IClass has

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.