DaveM
DaveM

Reputation: 734

How do objects of interfaces being returned have their methods declared

This feels like a silly question, but it aggravates me as I don't understand where the methods are declared. In my code I can do things like (obviously this is a bit pseudo code)

ResultSet rs = DB.getConnection.sendSQL(select * from [table])

I understand that the sending of the SQL returns a 'result set' and although a 'ResultSet' is only an interface the returned object implements that interface.

My question is how does the returned object implement that interface? From what I read interface can only define a method signature, not a complete function.

So where are the functions defined? Obviously not in the ResultSet interface! Am I correct in understanding that this the job of the database Driver? (I think I may have just answered my own question).

Can I write an interface with a fully functioning method? Will any implementing classes then automatically implement that method, or do I just overwrite it with a call to 'super' or something?

I just found this other question on SO How does abstract method of predefined interface like Connection, Statement etc. perform some task without having body? which is a perfect duplicate, but much bette formulated. And a great answer

Upvotes: 0

Views: 225

Answers (3)

Averroes
Averroes

Reputation: 4228

Let's take the following scenario:

We have an interface:

public interface Expendable(){
  public void expend();
}

And a class that implements it:

public class MyClass implements Expendable(){
  public void expend(){
//Do something
}
}

And another class returns in a method an object that implements our interface:

public class OtherClass(){
  public Expendable myMethod(){
   return new MyClass();
}
}

OtherClass myMethod returns an object (we don't know what Class it is just by looking at the method return type) that implements Expendable interface. The current Expend method is defined in the actual class myMethod is returning. In this case, the method who is invoked if you do

new OtherClass.myMethod().expend();

is MyClass expend method.

Upvotes: 1

Anthony Grist
Anthony Grist

Reputation: 38345

Classes that implement an interface are responsible for declaring all of the methods in the interface, and where appropriate implementing the internal logic for those methods.

The interface is essentially saying "You have to provide functionality for this, but I don't care how you do that. You just have to take inputs of these types and provide output of this type." The class is where you implement how you get from the inputs to the outputs.

Upvotes: 1

user207421
user207421

Reputation: 310909

My question is how does the returned object implement that interface?

class ResultSetImpl implements ResultSet { ... }

From what I read interface can only define a method signature, not a complete function. So where are the functions defined? obviously not in the ResultSet interface!

In the returned object. In the dots ;-)

This is very basic Java.

Upvotes: 1

Related Questions