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.
}
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With