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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:19:11+00:00 2026-05-22T23:19:11+00:00

I have 3 classes A, B, and C. These extend another class D. Class

  • 0

I have 3 classes A, B, and C. These extend another class D.

Class D has a method that is used in all classes A, B, and C.

Now the problem is that classes A, B, and C should extend different classes and use just the same method from class D.

I can’t believe that I should copy and paste the method in all my classes. Is there something like an include for function in C?

By the way I’m working on an Android app. Class D extends Activity and has a method for managing a common menu of Android activities A, B, and C (this is the official approach as reported in Android’s documentation). However I need that these activities extend different classes such as ActivityList and not just the Activity class.

  • 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-22T23:19:12+00:00Added an answer on May 22, 2026 at 11:19 pm

    If your method doesn’t need to access private state, add a static method in class D, and call the static method from A, B, C.

    If your method does need to access private state, see if you can factor out the need to use private state by adding a package-private getter to each class, and then use the method in A.

    Otherwise, try factoring out some of the logic to a common interface rather than superclass.

    Otherwise, try delegating to a helper class. (e.g. composition rather than inheritance as @Marcelo indicated)

    Otherwise, repeat the methods in each class A, B, C.


    as an example of the common interface approach, combined with a static method in D:

    interface MyThing
    {
       public void doMyThing(String subject);
       public List<String> getThingNames();
    }
    
    class D
    {
       static void doSomethingComplicatedWithMyThing(MyThing thing)
       {
          for (String name : thing.getThingNames())
          {
             boolean useThing = /* complicated logic */
             if (useThing)
               thing.doMyThing(name);
          }
       }
    }
    
    class A extends SomeClass implements MyThing
    {
       /* implement methods of MyThing */
    
       void doSomethingComplicated()
       {
          D.doSomethingComplicatedWithMyThing(this);
       }
    }
    
    class B extends SomeOtherClass implements MyThing
    {
       /* implement methods of MyThing */
    
       void doSomethingComplicated()
       {
          D.doSomethingComplicatedWithMyThing(this);
       }
    }
    
    class C extends YetAnotherClass implements MyThing
    {
       /* implement methods of MyThing */
    
       void doSomethingComplicated()
       {
          D.doSomethingComplicatedWithMyThing(this);
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a List of objects that extend another class: List<? extends Fruit> arguments;
I wish to have a single class which all of my Activity classes extend.
I have an interface called ICommand from which various classes inherit. These classes all
I have created custom MembershipUser, MembershipProvider and RolePrivoder classes. These all work and I
Let's say I have one class Foo that has a bunch of logic in
I have an ABC with several derived classes. To create these derived classes I
I have a few classes, such as those that outline database table structure or
Let's say I have a Base class and several Derived classes. Is there any
I have an application that receives information from a database, and is used to
I have two Java class hierarchies that share a common ancestor and implement a

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.