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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:39:20+00:00 2026-06-04T07:39:20+00:00

Say I have the following scenario: abstract class A { abstract method1(); abstract method2();

  • 0

Say I have the following scenario:

    abstract class A {
        abstract method1();
        abstract method2();
    }

    class B extends A {
        method1() {
            // implementation 1
        }
        method2() {
            // implementation 3
        }
    }

    class C extends A {
        method1() {
            // implementation 1
        }
        method2() {
            // implementation 4
        }
    }

    class D extends A {
        method1() {
            // implementation 2
        }
        method2() {
            // implementation 4
        }
    }

Bottom line: several subclasses of the same class, using different method implementations for different methods.

The problem is that I have the same implementation in different subclasses. My question is what to do with these common implementations?
The logical way to do it would be to create another level of abstraction but that only works if I have one method with common implementations (since Java doesn’t have multiple inheritence). I could also put the implementation most commonly used in the superclass and only implement it differently in the other classes. However, I wouldn’t do that because in time the most common implementation might change with the addition of other classes, and besides, I’d like to keep the methods abstract in the superclass.

Is there some pattern that would solve my problem in an “elegant manner”?

  • 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-04T07:39:21+00:00Added an answer on June 4, 2026 at 7:39 am

    You’re running into the classic OOP issue with inheritance heirarchies becoming unwieldy 🙂

    My suggested solutions:

    • Prefer composition to inheritance – factor out the implementations into separate objects that can be “plugged in” to provide the required behaviour. The methods in your base class just call the implementation plugin via its defined interface. This is basically the Strategy Pattern. It’s a very solid approach, though can sometimes become prone to a lot of over-engineering / boilerplate when you have a lot of different strategy types.
    • Adopt a prototype-based model – use prototype-based objects rather than traditional OOP. This solves all the problems – you escape the inherent restrictions of inheritance hierarchies and can just copy the implementation you want to whichever objects need it at runtime. This is the most radical option, but can bring some big benefits.
    • Factor implementations out into static functions – this way you can share the implementations – it’s a bit messy since you still need to do all the overrides, but they just become one-liners delegating the call to the right implementation. This is often the least intrusive option.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say i have the following scenario: public class A { public String createString(final
Let's say we have following this: <p class=first>This is paragraph 1.</p> <p class=second>This is
Let's say I have the following scenario: ComObjectClass firstCOMObject = new ComObjectClass(); ComObjectClass secondCOMObject
Referring to this question, let's say we have the following scenario - A model
Say we have the following (overly simple) scenario: We have a screen to view
I have following scenario: Lets say we have two different webparts operating on the
Let's say I have the following code: public class Foo { private int x;
Let's say in some abstract ViewModel base-class I have a plain-old property as follows:
Let's say I have the following scenario: <form action=/something.php method=GET>Click me</div> <script type=text/javascript><!-- $('form').submit(function(e)
I have the following scenario where I have a page say parentPage.html, which contains

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.