Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interface related

 
sukhdev satwalia
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I know we cnn't call the method of interface, But I have seen many times that if some method returns object of an interface say ob. then using ob we can csll its methods.
eg
Resultset rs= st.executequery();
rs.next();
rs.getString(2);
Since Resultset is an interfce but we can call its methods next(),getString() How it is possible
 
Scott Johnson
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An interface is a contract or specification. It describes methods but does not implement them. An interface is implicitly abstract so it can't be instantiated.

A class that implements the interface is promising to provide the methods described by the interface. You can call those methods on an instance of that object.

This Wikipedia article has a more detailed explanation.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64712
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sukhdev, welcome to the Ranch.

Please reserve this forum for advanced Java question. I've moved this topic to the beginner's forum for you.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sukhdev satwalia:
As I know we cnn't call the method of interface


Well, your knowledge about this is simply wrong, I fear.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Resultset rs= st.executequery();


This line returns an instance of some class that implements ResultSet. Because it implements ResultSet the compiler makes sure it has all the methods defined for ResultSet and then you can call them.

It's pretty cool that you don't have to know exactly what that class is. In the case of ResultSet I think every database vendor has to implement their own version of it. Or maybe Sun provides one. See, I don't know and don't care! This gives whoever the freedom to swap in a new implementation and your code will still work.

Polymorphism can be fun.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sukhdev satwalia recently wrote:
As I know we can't call the method of interface, But I have seen many times that if some method returns object of an interface say ob. then using ob we can csll its methods.
eg

Since Resultset is an interfce but we can call its methods next(),getString() How it is possible


Polymorphism!

You wrote that "... some method returns object of an interface..." that's only one half the truth. In your example, the reference type of rs is Resultset. The object type must be of a class that implements Resultset. The object type cannot be Resultset, as you can not instantiate interfaces. What the object type of rs really is depends on the method "executequery".
Since it's clear, that all classes implementing Resultset must provide the interface's methods (otherwise they wouldn't compile) it's also clear, that you can call all these methods on objects with the reference type Resultset.

As I know nothing of your Resultset and executequery stuff, I post a simple example below. In the Main class there is a "getInstance" method that will always return an object of the reference type of the Interface "Swimmable". But the Object type is randomly either Fish or Boat.
The line "swimmer.swim();" will call the method of the object type respectively.

Hope that helps,
Bu.

Example:


BTW, swimmable written with one M or two???
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic