Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call method not defined in interface from implemented interface in Java

Tags:

java

interface

I have the following scenario in Java. Let's say I have an interface, and two classes that implement this interface. As follows:

public interface myInterface {

    public String printStuff();

}

public class A implements myInterface {

    @Override
    public String printStuff(){
        return "Stuff";
    }
}


public class B implements myInterface {

    @Override
    public String printStuff(){
        return "Stuff";
    }

    public String printOtherStuff(){
        return "Other Stuff";
    }
}

How do I call the printOtherStuff method above if I define it as follows:

public static void main(String... args) {
 
     myInterface myinterface = new B();
     String str = myinterface.printOtherStuff(); // ? This does not work
}

The above calling code does not seem work. Any ideas?

like image 985
rameezk Avatar asked Jan 15 '14 11:01

rameezk


People also ask

What do we call a method which is defined with an implementation in an interface?

JavaObject Oriented ProgrammingProgramming. In order to call an interface method from a java program, the program must instantiate the interface implementation program. A method can then be called using the implementation object.

Which method is a method that is declared but not implemented in the code?

abstract. An abstract method has no implementation and must be a member of an abstract class.

Can we have interfaces with no defined methods in Java?

Yes, you can write an interface without any methods. These are known as marking interfaces or, tagging interfaces. A marker interface i.e. it does not contain any methods or fields by implementing these interfaces a class will exhibit a special behavior with respect to the interface implemented.

Why we Cannot define a method body inside an interface?

Because by default all methods are abstract inside the interface. So this example states that we can not have final methods inside the interfaces. Hence, this example also shows that we can have abstract methods only inside the interface.


2 Answers

myInterface myinterface = new B();

The reference type of myinterface is myInterface. That means you can only access the methods defined in the interface. You can cast it to type B in order to make the method call.

NOTE: From here on out I'll be using the proper naming conventions.

Example

MyInterface myInterface = new B();

String str = ((B)myInterface).printOtherStuff();

Just a note on this

If you need to do this, then you need to have a look at your class design. The idea of using an interface in this way is to abstract away from the details of the object's concrete implementation. If you're having to perform an explicit cast like this, then you might want to look into either changing your interface to accommodate the necessary methods, or change your class so that the method is moved into a global location (like a util file or something).

Extra Reading

You should read about reference types here, and you should have a look at casting here. My answer is a combination of the understanding of both of these things.

As an added note, take a look at the Java Naming Conventions. This is a vital piece of information for any Java developer to make understandable code.

like image 53
christopher Avatar answered Nov 15 '22 17:11

christopher


Surely this wouldn't work because you have reference type of Interface MyInterface. At the time of method binding compiler would try to find this method in your Interface MyInterface which is not available. So you need to cast it to your class like this.

    MyInterface myInterface = new B();
    B newB=(B) myInterface ;//casting to class
    newB.printOtherStuff();// would work fine
like image 29
Saurabh Sharma Avatar answered Nov 15 '22 17:11

Saurabh Sharma