• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interface related

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67748
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
reply
    Bookmark Topic Watch Topic
  • New Topic