I am creating an interface say 'Car'
public interface Car {
public void drive(int Speed, int Gear ); // for cars which have gears
public void drive(int Speed); // for cars which do not have gears
}
Now i am creating my implimentation classes say SimpleCar and AdvanceCar where
Now when i write my implementation classes i am forced to code for both the methods even though i do not want them in my implementation classes
public class SimpleCar implements Car {
public void drive(int Speed, int Gear ){ ... }; // dont want this method in SimpleCar
public void drive(int Speed ){ ... };
}
can someone help me design my interface which has a method but the implementation classes have different signatures?
public interface Car {
public void drive(int Speed, int Gear); // for cars which have gears
public void drive(int Speed); // for cars which do not have gears
}
public class CarAdapter implements Car {
@Override
public void drive(int Speed, int Gear) {
// TODO Auto-generated method stub
}
@Override
public void drive(int Speed) {
// TODO Auto-generated method stub
}
}
public class AdvancedCar extends CarAdapter {
@Override
public void drive(int Speed) {
// TODO Auto-generated method stub
super.drive(Speed);
}
@Override
public void drive(int Speed, int Gear) {
// TODO Auto-generated method stub
super.drive(Speed, Gear);
}
}
public class SimpleCar extends CarAdapter {
@Override
public void drive(int Speed) {
// TODO Auto-generated method stub
super.drive(Speed);
}
}
You should have a Car
interface and another one named GearCar
interface which extends Car
interface.
This way you can either implement GearCar or Car
interface.
See following design. I've removed gear
from Car
interface cause based on your requirements it's not valid for all cars and hence can't be part of interface.
public interface Car
{
// public void drive(int Speed, int Gear ); // for cars which have gears
public void drive(int Speed); // for cars which do not have gears
}
public abstract class SimpleCar implements Car
{
public void drive(int speed) { ... }
public abstract void accelerate(); // you can move it to interface also
}
public abstract class AdvancedCar implements Car
{
protected int CURRENT_GEAR = 1;
public void drive(int speed) { ... }
public void changeGear(int gear) { ... }
public abstract void accelerate();
}
public class Reva extends SimpleCar
{
// provide implementation for accelerate
}
public class Ferrari extends AdvancedCar
{
// provide implementation for accelerate
}
Write an CarAdapter
providing empty implementations of all methods in the interface. Then let your SimpleCar
extend CarAdapter
(which by default implements Car
)
This is frequently seen in Swing applications.
Just remove the method declaration that needs Gear
because in your class you need to implement every method declared in the interface that you are implementing unless your class itself is abstract.
Also you should have your SimpleCar
and AdvancedCar
as abstract class if you don't want to implement any methods of the interface.
public interface Vehicle {
public void drive(int Speed);
}
public interface Car extends Vehicle{
public void drive(int Speed, int Gear );
}
public class SimpleCar implements Vehicle {
public void drive(int Speed ){ ... };
}
public class AdvancedCar implements Car {
public void drive(int Speed, int Gear ){ ... };
public void drive(int Speed ){ ... };
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.