简体   繁体   中英

Implement multiple classes in the same interface in 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 (ie 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?

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.

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

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
    }

}

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?

You can provide an empty implementation for all the methods of an interface in other class called Adaptor class. And you can extend that adaptor class in ABC_FamilyGivenName class and ABC_DOBGender class.

class Adaptor implements common
{
public void ABC_GivenNames() {
 }

 public void ABC_FamilyNames() {
 }

 public void ABC_Gender() {
 }

 public void ABC_BirthDay() {
 }
}

IMPLEMENTATION CLASSES :

        public class ABC_FamilyGivenName extends Adaptor{

            public void ABC_GivenNames(){
                // Implementation code
            }

            public void ABC_FamilyNames(){
                // Implementation code
            }

        }

        public class ABC_DOBGender extends Adaptor {

            public void ABC_Gender(){
                // Implementation code
            }

            public void ABC_BirthDay(){
                // Implementation code
            }

        }
interface Icalculate{                 //interface
calculate(operand1:number,operand2:number):number
}

class Add implements Icalculate{     //addition
calculate(operand1: number, operand2: number): number{
 return (operand1 + operand2);       
    }
}

 class Sub implements Icalculate{       //subtraction
 calculate(operand1: number, operand2: number): number{
    return (operand1 - operand2);
  }
 }

class Mul implements Icalculate{           //multiplicationn
calculate(operand1: number, operand2: number): number{
    return(operand1*operand2);
 }
}

 class Div implements Icalculate{         //Division
  calculate(operand1: number, operand2: number): number{
    return(operand1/operand2);
   }
}  
  let a = new Add;
  let b = new Sub;
  let c = new Mul;
  let d = new Div;

  class Calculator {                   //main class
    operator: Icalculate;
    operand1: number;
    operand2: number;
    constructor(a: number, b: number, operator: Icalculate) {
    this.operand1 = a;
    this.operand2 = b;
    this.operator = operator;
    let op = this.operator;
    console.log(op.calculate(this.operand1, this.operand2));
    }
}   
const cal=new Calculator(1,1,a);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM