I've started java recently and I'm having some problems using interfaces in
real world cases.
Say I have an interface, "exampleinterface" declaring a list of methods. I
write a class, "example1class" which implements the interface methods 1 for
1. I can compile the class ok since all methods required by the interface
are implemented in the class. I could write another class which also
implements interface "exampleinterface" called "example2class".
Now if I want to use an object of type "example1class" in my code, I create
it using "exampleobject = new example1class" where exampleobject is declared
as type "example1class". Similarly I could use "example2class" also.
But I'm puzzled as to how to create an object that uses the interface
"interfaceexample", and how to specify which of the two possible classes
that implement the interface are actually used in my code ?
Thanks
Tony
Thomas Schodt - 25 Feb 2007 09:40 GMT
> I've started java recently and I'm having some problems using interfaces in
> real world cases.
[quoted text clipped - 11 lines]
> "interfaceexample", and how to specify which of the two possible classes
> that implement the interface are actually used in my code ?
That's the whole point - when your code deals with an interface it does
not care which actual implementation is used.
If you really want to know (say, for debugging), try
exampleobject.getClass().getName()
TonyB - 25 Feb 2007 09:52 GMT
> I've started java recently and I'm having some problems using interfaces
> in real world cases.
[quoted text clipped - 15 lines]
> Thanks
> Tony
I think I've answered my own question. An interface is really declaring a
type for a class's methods , so that when I use a class based on that
interface, it is type checked that it meets the requirements defined in the
interface ?
So I could say
exampleinterface object 1 ;
exampleinterface object 2 ;
...
object1 = new example1class();
object2 = new example2class();
If this compiles I know that both objects implement the required methods.
Do I understand this correctly ?
Tony
Lew - 25 Feb 2007 15:01 GMT
> I think I've answered my own question. An interface is really declaring a
> type for a class's methods , so that when I use a class based on that
[quoted text clipped - 9 lines]
> If this compiles I know that both objects implement the required methods.
> Do I understand this correctly ?
Yes.
One small note: there is a nearly universal convention in Java for laying out
source code and for nomenclature.
<http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html>
Class names should start with an upper-case letter, variable and method names
with a lower-case letter. Each subsequent word part within a name also begins
with an upper-case letter.
As a matter of engineering, it is bad practice to include things like "class"
in a class name, "object" in an object name, "interface" in an interface name.
You might refer a class to an interface or vice versa, and to have to change
the name is silly. All objects are objects, so saying "object" is unnecessary.
- Lew
Lew - 25 Feb 2007 15:15 GMT
> You might refer a class to an interface or vice versa,
You might /refactor/ ...
Joe Schlobotnick - 25 Feb 2007 19:39 GMT
>> I've started java recently and I'm having some problems using interfaces
>> in real world cases.
[quoted text clipped - 30 lines]
> Do I understand this correctly ?
> Tony
Yes, you do seem to understand correctly. Indeed, it's very accurate to
say you're declaring a "type" (although I wouldn't say "for a class's
methods...see below.) Any class that implements an interface is said to
have an "is a" relationship with that interface. Dog "is a" Animal, Cat
"is a" Animal, etc. (where Animal is an interface that both Dog and Cat
implement.)
One slight clarification. It's possible to have an empty interface, one
without method signatures. So while it's usually the case that
implementing an interface implies the existence of certain methods in
the implementing class, it's not necessarily so. Such interfaces are
often referred to as "tagging" interfaces; they simply "tag" a class as
being a member of a group.
..Joe
andrewmcdonagh - 25 Feb 2007 09:55 GMT
> I've started java recently and I'm having some problems using interfaces in
> real world cases.
[quoted text clipped - 14 lines]
> Thanks
> Tony
public interface Car {
void drive();
}
public class AutomaticCar implements Car {
public void drive() {
engageDrive();
accelerate();
}
}
public class ManualCar implements Car {
public void drive(){
changeIntoGear(1);
accelerate();
}
}
public class Test {
public void main(String[] args) {
List myCars = new ArrayList();
myCars.add( new ManualCar() );
myCars.add( new AutomaticCar() );
for (int index = 0; index < myCars.size(); index++ ){
Car aCar = (Car)myCars.get(index);
aCar.drive();
}
}
}
Here, the Test class only knows what specific type of car is, when
creating them, but after that it treats all of them the same, by using
only references of the interface type (Car).