Can I call a method that is inside an interface without implementing an interface?

Is it possible to call a method inside an interface without implementing an 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. } 
+8
java
source share
13 answers

Statically, you can declare it callable. You do not 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 something, because the interface simply describes how something behaves, but does not provide the actual behavior (i.e. does not implement it).

+6
source share

You can as follows:

 Person p = new IPerson{ public void savePerson(Contact contact) { // some code } } 

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

+5
source share

You cannot, because the method does not contain a definition (the body of the method). The purpose of the interface is to implement it somewhere so that you have the actual implementation of this method.

+4
source share

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

+3
source share

No. You cannot name it, you will have to provide its implementation at least

In addition to the invocation method, you will need an object. you cannot create an interface without implementing it

+2
source share

An interface is simply a definition of methods without implementation. If you do not implement methods, what will they do when you name them?

On the other hand, an interface is not necessarily implemented by the calling class. Therefore, in your case, HulkHogan may call another savePerson () method without implementing it itself. But some class must implement it somewhere in order to do something.

+2
source share

If you do not implement interface or do not have a member object of a class that implements the interface, you cannot use the savePerson method, because by definition it is not yet implemented.

+1
source share

You will need to call it an object that is an instance of the interface, so either HulkHogan would have to implement IPerson , or an instance of something that implements IPerson would have to be provided (or instantiated internally, although this is not preferred) by HulkHogan .

To a greater extent (based on your previous questions), what do you expect to achieve by calling a method that has no implementation?

+1
source share

The purpose of the interface is to entrust a specific and well-known contract for a known type of object. For example, by tradition, all stacks have push (), pop (), and peek (). Therefore, by writing an interface, you are directly writing a contract in which everyone who writes the stack should follow.

In other words, this is a template that dictates that any class that calls itself a stack should implement methods called push (), pop (), and peek (), as my example discusses here.

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

This is an object oriented technique. At this point you will not get compilation because you at least have not implemented methods for interface methods. You will need to write methods that match the interface signatures before you can fully use the interface.

+1
source share

when you say that you implement an interface for a class, you actually mean that you are executing the contract (or discipline) that is defined in the interface, that is, you will give a definition for the method in the interface, so that just that the interface is a container containing a rule ,

You said that you want to call a method in the interface: this is not the method itself, which is just a specific rule, so the point is a method call that does not have a body (in fact, this is not the method itself)

... enjoy

+1
source share

You can create a proxy server that does nothing to mock interface calls. However, this effectively creates an implementation dynamically. You cannot avoid providing any kind of implementation.

+1
source share
 public class HulkHogan { private IPerson person; public void setContactM(IPerson person) { this.person= person; } public void doSomething(Contact contact) { person.savePerson(contact); } } 

We hope that the savePerson method has some definition somewhere in another class that implements it. In this way, you can call the method inside your interface.

0
source share

You cannot call a method until it has an implemented body. The performing class will provide you with the organ you need. In Java 8, you can implement the method body inside the interface itself and use the static access modifier for it. In this case, you just need to import the interface into the current class (in case it is in another package), and then you can directly call the method.

0
source share

Source: https://habr.com/ru/post/650523/


All Articles