Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interfaces

 
S.Hamid
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How Interface realize that which class use its methods.
 
Fei Ng
Ranch Hand
Posts: 1245
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
by Polymorphism?? no? maybe i don't understand your question.
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand the question correctly, an interface does not maintain state information, and therefore doesn't "know" when it's being used.

------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My sentiments exactly
A class is made to implement an interface. That just means that the class must have methods with the right signature and return type (the return type is not part of the signature) as dictated by the Interface.
As this is the beginner's forum, a class can only inherit from one class but it can implement many interfaces.
Poors man's "multiple inheritance"
 
Tony Jackson
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm also not sure of your question, but I will explain some things about Interfaces and Classes and Objects with the hope of clarifying things for you.
First, let's say you have an interface called "Drawable" that defines a method drawToScreen:
public void drawToScreen();
(this method presumably draws the object on the screen.)
Second, say you have two or more classes that implement Drawable, including "Square", "Circle", and "TextBlock".
Third, your program calls a static method "nextObject()" that gets a Drawable object and you then want to draw it on the screen.
You write the following code to get the object and draw it on the screen:
Drawable d = someClass.nextObject();
d.drawToScreen();
One key point is this: you are doing all this (getting an object and calling one of its methods) without ever knowing what kind of object it is. The object d might be a Square, or a Circle, or something else. Whatever it is, you know it implements the interface "Drawable" and thus that it has the method "drawToScreen()".
If you really wanted to know what kind of object d was, you could use this:
Class c = d.getClass();
String s = c.getName();
Tony Jackson
------------------
Tony Jackson
Trainer and Courseware Developer
tjackson@selectica.com
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic