12f0af0
12f0af0

Reputation: 105

How to inherits/uses methods from multiple classes

Current I have a base class that contains project init and teardown method, and some large number of common methods, and these methods are inherited/used by its subclasses.

Because the based class is getting huge, so I am trying to move these common methods into newly created classes to improve the modularity of the project. And the original base class inherits the methods from those newly created classes. (multiple inheritances?)

Is there any suggestion to refactor this? also need to minimize the impact to the subclasses.. minimal code changes will be perfect..

Base class 
-method1()
-method2()
-method3()
-method4()
-method5()
-method6()--------
    |            |
    |            |
subclasse1     subclass2 

Upvotes: 0

Views: 55

Answers (2)

fabian
fabian

Reputation: 82451

First of all, there is no multi inheritance of classes (only multi inheritance of interfaces) in java.

However in java 8 you can write default implementations of interface methods (see "Default Methods" on docs.oracle.com). But there are some restrictions:

  • You still can't declare fields in a interface
  • If you have 2 default implementations of the same method in different interfaces, you still need to override them.

You may be better off using the adapter pattern, i.e. refractor your code to something like this:

public class BaseClass {
    private final Adaptee1 adaptee1;
    private final Adaptee2 adaptee2;
    // ...

    public BaseClass(Adaptee1 adaptee1, Adaptee2 adaptee2 /* , ...*/) {
        this.adaptee1 = adaptee1;
        this.adaptee2 = adaptee2;
        //...
    }

    public BaseClass() {
        this(new ConcreteAdaptee1(), new ConcreteAdaptee2() /* , ...*/)
    }

    public void method1() {
         adaptee1.method1();
    }

    public void method2() {
         adaptee1.method2();
    }

    public void method3() {
         adaptee2.method3();
    }

    public void method4() {
         adaptee2.method4();
    }
    // ...
}

public interface Adaptee1 {
    void method1();
    void method2();
}

public interface Adaptee2 {
    void method3();
    void method4();
    // ...
}
//...

public class ConcreteAdaptee1 implements Adaptee1 {
    //...
}

public class ConcreteAdaptee2 implements Adaptee2 {
    //...
}
//...

You could even pass the adapter to the adaptees in the method calls, if you need access to methods in other adaptees as well for example.


But you should check first, if the class can be split into multiple classes in a good way (i.e. the adaptees should be independent). Don't split the class into parts at all cost. If you can't make the adaptees independent, you should keep it as a single class and rely on your IDE instead to navigate the code easier (Code folds, bookmarks, ...)

Upvotes: 1

user1706538
user1706538

Reputation: 641

In Java, one class cannot extend multiple other classes, however it can implement as many interfaces as you'd like. Unfortunately, implementing an interface will not provide the methods you need.

As far as I could think this out, you have the following options:

1) If you don't want to change all your references to the Base class, and the Base class has some methods that are really broad and may apply to many classes, you could put those into one class and have your base class extend that one.

example:

public class SuperBaseClass{
method1()
method2()
method3()
}

public class BaseClass extends SuperBaseClass{
method4()
method5()
method6()
}

You could even create a SuperDuperBaseClass which SuperBaseClass extends to continue the chain as far as you'd like. The problem with this technique is it forces a hierarchical structure to your project.

2) You could split the Base class into multiple other base classes (I would recommend this one). Sort out your methods into categories and put them in appropriately named base classes. For example, throw all your methods which crunch numbers into one class named Algorithms. Put all your methods which tie other classes and functionalities from your project into one class called Tools. Do this until your whole project is sorted. It will clean everything up as much as possible and keep things manageable in the future. The problem with this is you will have to change everywhere the Base class used to be referenced.

3) You could just leave the Base class as it is. Since it's only a supporting file, you shouldn't really have to be digging through that code very often, so it won't hold you back too much that this file gets a little messy.

Upvotes: 0

Related Questions