开发者

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

开发者 https://www.devze.com 2023-02-01 20:49 出处:网络
Can i call a method inside an interface without implementing the interface in my class? package; import Contact;

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.

开发者_如何学Go}


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).


You can in this way :

Person p =  new IPerson{

   public void savePerson(Contact contact) {
       // some code
   }

}

Now call savePerson on p. You are implementing IPerson without creating a separate class.


You can't because the method doesn't contain a definition (method body). The purpose of a interface is to implement it somewhere so that you have an actual implementation of this method.


No you cant...bcoz all methods of the class interface are abstract..and you cant use them without implementing the class ...


No You can't call it, You will have to provide its implementation atleast

Further to call method you will need an object. you can't instantiate interface without its implementation provided


The interface is just a definition of methods without implementation. If you are not implementing the methods what will they do when you call them?

On the other hand, an interface is not necessarily implemented by the calling class. So in your case HulkHogan can call another object's savePerson() method, without implementing it itself. But some class has to implement it somewhere for it to do something.


Unless you implement the interface or have a member object of a class that implements the interface, you can not use the savePerson method, since by definition it has no implementation yet.


You'd have to call it on an object which is an instance of the interface, so either HulkHogan would have to implement IPerson or an instance of something which implements IPerson would have to be supplied to (or instantiated within, though that is not preferred) HulkHogan.

More to the point (based also on your previous questions), what do you expect to accomplish by calling a method which has no implementation?


The goal of an interface is to mandate a specific and well-known contract for a known type of object. For example, by tradition all Stacks have push(), pop(), and peek(). So by writing an interface, you're expressly writing a contract that anyone who writes a stack should follow.

To put in another way, it is a template that dictates that any class that calls itself a Stack must implement methods called push(), pop(), and peek() as my example here discusses.

public interface StackInterface {
    public void push(int x);
    public int pop();
    public void peek();
}

public class myStack implements StackInterface{

}

It's an object-oriented technique. At this point you will not get a compile because you haven't at least implemented methods for the interface methods. You will need to write methods matching the signatures of the Interface before you can fully use the interface.


when you say you are implementing an interface for a class, you actually mean you are abiding a contract (or a discipline) which is defined in the interface, that is you will give the definition for the method in interface, so just feel that interface is a container which contains a rule,

You said you want to call the method in the interface : that is not a method in itself that is just a rule defined so what is the point is calling a method which has no body (actually is its not a method in itself)

...enjoy


You can create a Proxy which does nothing to mock out calls to the interface. However this effectively creates an implementation dynamically. You cannot avoid providing an implementation of some kind.


public class HulkHogan {

private IPerson person;

public void setContactM(IPerson person) {
    this.person= person;
}

public void doSomething(Contact contact) {
    person.savePerson(contact); 
  }

}

Hope the savePerson method has some definition somewhere in other class which implements it. Through this way you can call the method inside your interface.


You can't call the method while it does not have an implemented body. The implementing class will give it the body that you need. In Java 8, you can implement the method body inside the interface itself, and using the static access modifier for it. In this case you just need to import the interface in your current class (in case it is in a different package) and then you can call the method directly.

0

精彩评论

暂无评论...
验证码 换一张
取 消