aspose file tools*
The moose likes Beginning Java and the fly likes Interface related Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interface related" Watch "Interface related" New topic
Author

Interface related

sukhdev satwalia
Greenhorn

Joined: Aug 19, 2006
Posts: 1
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

Joined: Aug 24, 2005
Posts: 518
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

Joined: Jan 10, 2002
Posts: 60053
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
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???


all events occur in real time
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Interface related
 
Similar Threads
Interface
About interface methods
Object Class' methods in Interface
regarding comparator interface
sample jsp page calling database access bean