天天看點

java一個接口多個實作類,在Java中的同一個接口中實作多個類?

java一個接口多個實作類,在Java中的同一個接口中實作多個類?

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();

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?

解決方案

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

}

}