简体   繁体   中英

How can I implement my own method when instantiating an interface?

I have an interface:

public interface ExampleInterface {

  public void exampleMethod();

  public void anotherExampleMethod();

}

And an example class:

public class ExampleClass implements ExampleInterface{


  public ExampleClass() {
    ........
  }

  public void exampleMethod() {
    ..............
  }

  public void anotherExampleMethod() {
    ..............
  }


  public void myOwnMethod() {
    ........
  }

}

I instantiate the class by using the interface like so:

ExampleInterface exampleClass = new ExampleClass();

Doing this is ok:

exampleClass.exampleMethod();

But this is not:

exampleClass.myOwnMethod();

However, this works:

ExampleClass exampleClass = new ExampleClass();
exampleClass.myOwnMethod();

Why is it when I instantiate with an interface and try to call a method that is not part of the interface it doesn't work? Is there anyway to call myOWnMethod() while still instantiating with the interface?

When you have a reference variable of type ExampleInterface , there is no knowledge that it's really an ExampleClass , so there is no guarantee that there is a myOwnMethod to call. You can create another class, SillyExampleClass that implements ExampleInterface , but you don't define a myOwnMethod method there. That's why you can't call myOwnMethod , it may not be there on the actual implementation.

If you want to call myOwnMethod() on a reference variable of the ExampleInterface interface, then you include myOwnMethod in the interface definition, because the interface definition specifies what must be in any implementation of the interface.

Let's say you have an interface Person and then you implement it in two classes: Boy and Girl . If you have the method bePregnant() in the Girl class, when you do:

Person person = new Boy();

calling bePregnant() method in that object won't make sense. However, there is one way to check if the person is a Girl before you call some method that doesn't belong to Person interface:

if (person instanceof Girl)
    ((Girl) person).bePregnant();

First, instanceof is used to check wheter an object is an instance of another class (this class can be also a superclass of the object). Then what (SomeClass) object does, is called casting . In your case you could do this:

ExampleInterface exampleClass = new ExampleClass();
((ExampleClass) exampleClass).myOwnMethod();

In both examples you create an instance of ExampleClass. But in the first one you reference it with a variable of type ExampleInterface (which has no method myOwnMethod) in the second example you reference it with a variable of type ExampleClass (which do has a method myOwnMethod)! If you want to call myOwnMethod on your ExampleInterface you could cast it to ExampleClass.

((ExampleClass) exampleInterface).myOwnMethod()

this is not save if you are not sure that the object which exampleInterface is referring to is actually of type ExampleClass. Otherwise you'll get a ClassCastException.

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