1

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?

gagneet
  • 35,729
  • 29
  • 78
  • 113
  • no, you can´t. The proper way might be splitting your interface into two separate interfaces with the methods you do need. – SomeJavaGuy Nov 27 '17 at 06:32
  • If you are using java 8, Make all the methods as default method, and override the needed methods on the subclass. – Saran Nov 27 '17 at 06:41
  • @SomeJavaGuy I do not wish to have multiple interfaces, as that defeats the purpose and I would rather use the class directly. For me, I have ~200 such classes, and each "Application" type class will use ~20-30 of them each time. I wish to avoid the overhead of trying to import/declare statements for each class I use, and wanted to use a single one; which I thought would be possible by using an interface, but seems that is not the case. – gagneet Nov 27 '17 at 09:35
  • @Saran Could you please explain a bit more on this? – gagneet Nov 27 '17 at 09:35
  • A solution for this can also be found at: https://stackoverflow.com/questions/47523700/implement-a-single-java-class-interface-which-references-multiple-classes/47524603#47524603 – gagneet Nov 28 '17 at 06:33

6 Answers6

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.

Talgat
  • 135
  • 11
1

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

  • Okay, that is fine and I realize that. But my issue is that, I have multiple such class files implemented, which I need to reference/use in the "Application" class, and other such class files. I do not wish to every time create a new() object of each class in "Application", et al, just to use the methods of that class. – gagneet Nov 27 '17 at 09:20
  • you can use a concrete class named common instead of interface. then inherit the common class to all the class. then the common class can be used as a reference of every class – Shakhawat Hossain Nov 27 '17 at 09:57
  • Thank you for the suggestion. Possible to provide an example? – gagneet Nov 27 '17 at 10:02
  • here use your common interface as a concrete class. then write the basic declaration of all the methods. If any class override the method the instance call the overridden methods. Otherwise, it calls the base method. But here is a problem. You can't extend both base class and common class at a class. in this case, extend common class by base class then extend base class by ABC_FamilyGivenName class. hope it helps. – Shakhawat Hossain Nov 27 '17 at 10:09
1

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
    }

}
Saran
  • 146
  • 7
  • Thanks Saran, the above works. Only issue is that the called method is not instantiating. – gagneet Nov 27 '17 at 22:27
  • Could you please explain what instantiation means here? – Saran Nov 28 '17 at 02:25
  • If we use the method in our code, it is not getting referenced or the implementation being run for that method. We are trying to debug and figure out what the issue is, as there are no compile time issues. – gagneet Nov 28 '17 at 02:49
  • use this link to download the code, created using eclipse oxygen supporting java 8 [https://github.com/saravananshivaji/TestDefaultMethods]. There are 2 approaches 1. Using default in Interface, 2. Using adapter pattern (This has a package named adapter where you can use the Base class as adapter). – Saran Nov 28 '17 at 05:19
0

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?

Keval
  • 1,857
  • 16
  • 26
  • If I create an abstract class, then the use in "Application" class is not being permitted. Is there a work around that I implement them as abstract classes and can then use the methods via the interface in the "Application" class? Basically, how do I use the methods of ABC_* class in the "Application" class? – gagneet Nov 27 '17 at 09:18
0

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
            }

        }
niksvp
  • 5,545
  • 2
  • 24
  • 41
S Botalji
  • 1
  • 1
0
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);
Pooja
  • 21
  • 3
  • 1
    Please put your answer always in context instead of just pasting code. See [here](https://stackoverflow.com/help/how-to-answer) for more details. – gehbiszumeis Nov 13 '19 at 08:42