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
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
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.
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
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.