wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Complicated Question (Interfaces) pls. Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Complicated Question (Interfaces) pls. Help" Watch "Complicated Question (Interfaces) pls. Help" New topic
Author

Complicated Question (Interfaces) pls. Help

vishal avad
Ranch Hand

Joined: Nov 29, 2001
Posts: 45
i ve tried to write a class which is implementing two interfaces
both interfaces having one identical method. which is implemented in the class how can we come to know we ve implemented which interface's method.
i know it is meaningless to know but i m curious to know about that
or tell me what is happening internally.
thx in adv
vishal
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Vishal,
An interface is just a template! If two interfaces both define a method with the same signature and a class implements those interfaces, then the class just has to implement the method and the contract between the class and the interfaces is fulfilled.
JLS 8.1.4 gives some good explanations (towards the end of the secion 8.1.4)
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Rex Rock
Ranch Hand

Joined: Jun 25, 2001
Posts: 82
Hi,
Valentin Crettaz - that was a neat reply.
To add a few more points to what Valentin has said,
When both the interfaces have an identical method with same signature and return type and if your class is implementing both of those interfaces, then during the compilation process, the java compiler checks what interfaces you claim to implement in your class declaration statement, and makes sure that you have a method with such a signature in your class body. so, if you claim to implement two interfaces I1 and I2, it first checks what method interface I1 declares, and checks to see if your class body has such a method implementation, then it checks the method declarations in I2 and looks for corresponding implementation. If it doesn't find an appropriate implementation then it will raise an alarm saying your class should be declared abstract.
<code>
import java.lang.reflect.*;
interface ICar {
public void drive() ;
}
interface ITruck {
public void drive() ;
}

public class Test implements ICar, ITruck {
public static void main(String[] args) {
Test t = new Test();
t.drive();
Class[] c = t.getClass().getInterfaces();
for ( int i = 0 ; i < c.length;i++)
System.out.println(c[i].getName());
}
public void drive() {
System.out.println("am driving");
}
}
</code>
Output :
<code>
am driving
ICar
ITruck
</code>
I think the above code makes it a lil bit easier to understand what i said. Again Valentin's explanation says it all. I was just adding a lil bit more to it.
vishal avad
Ranch Hand

Joined: Nov 29, 2001
Posts: 45
thx valentin & rex
i read ur explanation. but my question is there no way to find out which interface is the parent of the implemented method.
consider rex's example how can we come to know method drive is the method from ICar or ITruck.
thx in adv
vishal
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Vishal,
Your question really makes no sense. An interface is just a signature template to a class. What that means is that any class that implements it must have the correct signature. It doesn't matter how many interfaces you implement that require the same signature.
Put in other words, think of an interface as a key that allows the implementing class to act as that interface to the outside world.
Need a real world example? Ok, how about Java Ranch? When you register you are implementing the JavaRancher interface. This allows you to read and write on these topic boards. It doesn't matter than you are also short/tall or white/black/foreign or thin/fat. All that matters is that you implement the JavaRancher interface.
If you don't understand the concept, you might want to read a few Object Oriented Programming books. If you can't get your arms around the idea you will probably have trouble in your java programming...
Regards,
Manfred.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Vishal,
As Manfred pointed out, "An interface is just a signature template". That means that you usually define an interface by listing the (signature of the) methods that should be implemented. A class implementing an interface must provide an implementation for each method listed in the interface, that means that the class provides a behavior for each method. Moreover, when a class implements an interface, the class acquire one more type, i.e. the one of the interface and can thus act as an object of the type of the interface. This is a very powerful feature because unrelated classes can be of the same type and thus referenced by the same variable or array of variable.
Think of the interface Runnable. That interface allows classes implementing it (i.e. providing an implementation for the run() method) pretend they can execute in their own Thread. And any class can implement Runnable and provide the run method implementation. Imagine you have an application with following classes: Car, Tree, Book, Animal, Human, ... You must admit that these classes are not really related except that a Human drives a Car, climbs a Tree, reads a Book and owns an Animal, but what I want to point out is that those classes have very different functionalities. But all these classes can implement Runnable to show how they evolve with the time. Your application may then want to keep a reference to all instances of those classes in a central repository (say an array). The good part is that with an array of Runnable you can reference all those instances without even caring about what type the object really is of. All you are interested in is that the Object is Runnable and can evolve with the time. Besides that all objects have their own proper type also, i.e. a Car is of type Runnable and Car, a Tree of type Runnable and Tree, and so on...
Do you start to understand what the interface concept is aiming at ?
Interface is a topic you MUST master.
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Valentin Crettaz (edited November 30, 2001).]
vishal avad
Ranch Hand

Joined: Nov 29, 2001
Posts: 45
thx Manfred Leonhardt & Valentin Crettaz
what ever u all answered that is my view. but this question is asked to me in a interview. that time i just cant think and get confused "what to answer" anyway now i m confident that whatever i know is correct so thx 4 ur suggestions
rgds
vishal
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Complicated Question (Interfaces) pls. Help
 
Similar Threads
Which of the above two interfaces would be invoked?
interfaces and abstract classes
why an Interface cannot have static method?
Where methods of ServletContext , HttpSession , HttpServletRequest are implemented ?
multiple inheritance