Re: Using Interfaces ?



On Feb 25, 9:28 am, "TonyB" <t...@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
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

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


.



Relevant Pages

  • Re: Using Interfaces ?
    ... "example1class" which implements the interface methods 1 for 1. ... 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? ... Dog "is a" Animal, Cat "is a" Animal, etc. ...
    (comp.lang.java.help)
  • Re: Using Interfaces ?
    ... TonyB wrote: ... "exampleinterface" declaring a list of methods. ... "example1class" which implements the interface methods 1 for 1. ...
    (comp.lang.java.help)
  • Using Interfaces ?
    ... I've started java recently and I'm having some problems using interfaces in ... Say I have an interface, "exampleinterface" declaring a list of methods. ... Now if I want to use an object of type "example1class" in my code, ...
    (comp.lang.java.help)
  • Re: implementing roles in OOP......
    ... > "An application implements a system of responsibilities. ... > single purpose) within a given level of abstraction. ... Note that since an interface defines the messages an object will ... Car not ComponentList). ...
    (comp.object)
  • Re: Speed of interfaces vs inheritance
    ... implementation of an interface. ... Another thing to note is the degree of actual polymorphism at the call site. ... But that kind of optimisation makes considering call sites difficult. ... abstract public void abstractCall; ...
    (comp.lang.java.programmer)