Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can i call a method which is inside an interface without implementing the interface?

Tags:

java

Can i call a method inside an interface without implementing the interface in my class?

package;

import Contact;

public interface IPerson{

    public void savePerson(Contact contact);

}

Now some class here...

public class HulkHogan { 

//Calling the method savePerson here
//I dont want to implement the Interface in all.

}
like image 661
theJava Avatar asked Dec 29 '10 13:12

theJava


1 Answers

Statically, you can declare it to be called. You don't necessarily have to implement the interface inside the calling class. For example:

public class HulkHogan { 
  private IPerson person;

  public HulkHogan(IPerson person){
    this.person = person;
  }

  public void doSomething(Contant contact){
    //call your method here
    person.savePerson(contact);
  }
}

But in order to execute it, you will need to implement it somewhere, because interface just describes how something behaves but does not provide the actual behaviour (i.e. does not implement it).

like image 102
rodion Avatar answered Oct 04 '22 04:10

rodion