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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:24:36+00:00 2026-05-22T22:24:36+00:00

I have one main interface and an abstract class implementing all derivable methods (that

  • 0

I have one main interface and an abstract class implementing all “derivable” methods (that can be written using only abstract methods) of it:

public interface Main {
    public void main1(int x);
    public void main2();
}

public abstract class MainAbstract implements Main {
    public void main2() { main1(42); }
}

This functionality can be extended in different “directions”:

public interface SubA extends Main {
     public void subA1(int x);
     public void subA2();
}

public interface SubB extends Main {
     public void subB1(int x);
     public void subB2();
}

Now I could have abstract classes SubAAbstract and SubBAbstract implementing all the “derivable” methods (like main2 in Main). The problem is that I may have concrete implementations who want to implement both SubA and SubB, so I could use only one of the abstract classes. In reality the problem is worse, because I have more than 2 sub-interfaces.

I’m aware that there is no way around single inheritance in Java, but I would like to know if there is a good way to minimize code duplication in that case.

[Edit]

Would it be an abomination to put all functionality in the MainAbstract class (note that it only implements Main), e.g.

public abstract class MainAbstract implements Main {

    public void main2() {
        main1(42);
    }

    public void subA1(int x) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void subA2() {
        subA1(4711);
    }

    public void subB1(int x) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void subB2() {
        subB1(-1);
    }

}

Then a concrete class needs just to implement the needed interfaces, and to implement the needed methods:

public class MainConcrete extends MainAbstract implements Main, SubA {

    public void main1(int x) {
        System.out.println("main " + x);
    }

    public void subA1(int x) {
        System.out.println("subA" + x);
    }

}

Of course this would only make sense if the hierarchy is rather stable (that means, the sub interfaces are known exactly before), but this would be the case for me. Having the superfluous methods visible isn’t nice either, but I don’t seee a way to change that.

  • 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-22T22:24:37+00:00Added an answer on May 22, 2026 at 10:24 pm

    Externalize the work to utility classes, builders etc.

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

Sidebar

Related Questions

I have a base class and a dozen derived classes. All but one derived
I have 2 classes. One will be a basic instructions screen and on that
In my iOS app I wan't to play sounds using OpenAL. I have AudioManager
I have simple test project which creates a frame and adds one JTextPane to
I have: $(#option1).live(click, function(){ $(.active).removeClass('active'); $(this).addClass('active'); $(#main).load(option1.php, function(){ }); }); and the script inside
I've used the conditional comments technique for my IE6/7/8 CSS hacks, so that the
Hey everyone! I am having trouble with understanding modules -- I have two files,
I'm completely new to Linux and have been trying to get my (Windows built)
I'm using Add-in-Express to develop a prototype of our plugin for Outlook and what
So what I need is simple: each time we perform our shader (meaning on

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.