Implement multiple classes in the same interface i

2019-08-18 23:30发布

问题:

I have got multiple classes which each implement multiple different methods within each. Now the problem statement is that I wish to use the methods from all these (maybe around ~200 such different class files/methods) in another class file which all different methods from the above class files.

I thought that if I implement an interface which has all these various methods listed, then I just call/import/reference that single interface and can use all the methods? But I am stuck, as this solution does not seem to work.

The opposite of the above works (i.e. single class implements 2 interfaces: http://tutorials.jenkov.com/java/interfaces.html). Wish to check if the single interface can use multiple classes, without the overhead of declaring all the methods in each class that is being referenced inside the Interface?

As an example: Is there any way in which I can implement 2 different classes in the same interface, without each having the abstract class for each? As if the class is abstract, then I am unable to use the methods from it in the below example "Application" class:

Common commonClass = new ABC_FamilyGivenName();

The above is not allowed, if the ABC_FamilyGivenName class is an abstract class.

INTERFACE:

public interface Common {
    void ABC_GivenNames();
    void ABC_FamilyNames();
    void ABC_Gender();
    void ABC_BirthDay();
}



IMPLEMENTATION CLASSES:

public class ABC_FamilyGivenName extends Base implements Common {

    public void ABC_GivenNames(){
        // Implementation code
    }

    public void ABC_FamilyNames(){
        // Implementation code
    }
}

public class ABC_DOBGender extends Base implements Common {

    public void ABC_Gender(){
        // Implementation code
    }

    public void ABC_BirthDay(){
        // Implementation code
    }
}



USE IMPLEMENTED CLASS:
public class Application extends Base {

    Common commonClass = new ABC_FamilyGivenName();
    /* DO I NEED THIS? I THINK I DO, BUT CODE/JAVA SAYS I DO NOT
     * Common commonClass = new ABC_DOBGender();
     */

    public void ELP_C0050_PassportDetails(){
        commonClass.ABC_GivenNames();
        commonClass.ABC_FamilyNames();
        commonClass.ABC_DOB();
        commonClass.ABC_Gender();
    }
}

I have 2 classes called ABC_FamilyGivenName & ABC_DOBGender. I have created an interface Common.

I want to use the methods in both the above classes in another class called Application.

With the current implementation, Java wants me to add an @Override to both the ABC_FamilyGivenName & ABC_DOBGender:

IMPLEMENTATION CLASSES:

public class ABC_FamilyGivenName extends Base implements Common {

    public void ABC_GivenNames(){
        // Implementation code
    }

    public void ABC_FamilyNames(){
        // Implementation code
    }

    @Override
    public void ABC_BirthDay() {}

    @Override
    public void ABC_Gender() {} 
}

public class ABC_DOBGender extends Base implements Common {

    public void ABC_Gender(){
        // Implementation code
    }

    public void ABC_BirthDay(){
        // Implementation code
    }

    @Override
    public void ABC_GivenName() { }     

    @Override
    public void ABC_FamilyName() { }        
}

Can I avoid the above @Override and just use the classes without these as given in the first example?

回答1:

Object-oriented programming in Java requires to "override" all methods, if you are implementing a method, otherwise you may use inheritance, so not all methods must be overriden.

In your case you may put all four methods to parent class Base and then inherit them. Then the interface class is not needed or make two different interfaces.



回答2:

To implement Java interface, You should override all the abstract methods are declared into the interface. It is a basic concept of interface. Here interface Common all four methods are abstract, So you should override them. Otherwise, Java compiler will throw a compilation error. So better way can be splitting the interface into 2 parts. It is a contractual nature of an interface the subclass who implement the interface should have all the activities of the interface. It is the main purpose of using an interface.

If you don't wanna override all the method of interface but you need to use the interface as a reference of every class, then you can use a concrete class instead of interface and inherit the concrete class to every class



回答3:

To implement the below code change please make sure you use java8

public interface Common {

 default public void ABC_GivenNames() {
 }

 default public void ABC_FamilyNames() {
 }

 default public void ABC_Gender() {
 }

 default public void ABC_BirthDay() {
 }

}

IMPLEMENTATION CLASSES:

public class ABC_FamilyGivenName extends Base implements Common {

    public void ABC_GivenNames(){
        // Implementation code
    }

    public void ABC_FamilyNames(){
        // Implementation code
    }

}

public class ABC_DOBGender extends Base implements Common {

    public void ABC_Gender(){
        // Implementation code
    }

    public void ABC_BirthDay(){
        // Implementation code
    }

}


回答4:

Can I avoid the above @Override and just use the classes without these as given in the first example?

No, in java you have to implement all methods of interface unless its abstract class

as suggestion you can create two separate interfaces,

for more detail see : not implementing all of the methods of interface. is it possible?