-2

I have the following Issue. Where I have to cast inside of the makeLeftTurnMethod... this looks very ugly to me.. Is there a way where I don't have to do this?

public interface Car(){
   public void turnRight();
   public void turnLeft();
   public void go();
}
public class LuxuryCare implements Car(){
   public void systemCheckRightSideClear();
   public void systemCheckLeftSideClear();
}
public class UsedCar implements Car(){
   public void personCheckRightSideClear();
   public void personCheckLeftSideClear();
}

public interface Run{
  public void makeLeftTurn();
}
public class RunLuxuryCar implements Run{
  public void makeLeftTurn(Car car){
     car = (LuxuryCar)car;
     car.systemCheckLeftSideClear();
     car.turnLeft();
  }
}

public class RunUsedCar implements Run{
   public void makeLeftTurn(Car car){
       car = (UsedCar)car;
       car.personCheckLeftSideClear();
       car.turnLeft();
   }
}

public void tester(){
    //Could return either used or luxuy car 
    Run run = RunFactory.getInstance(UsedCar);
    Car car = CarFacory.getInstance(usedCar);
    run.makeLeftTurn(car);
}
Iswanto San
  • 18,263
  • 13
  • 58
  • 79
  • I don't think this is what you intended. `Run#makeLeftTurn()` takes no arguments. `RunLuxuryCar#makeLeftTurn(Car car)` does not implement the interface's method, it creates a new method with one argument. – Jim Garrison Feb 13 '13 at 07:39
  • Also, this is nowhere near valid Java. `public interface Car()`: the `()` is spurious. `LuxuryCar` and `UsedCar` are declared to implement `Car` but do not implement `Car`'s methods... there's _lots_ more that won't compile. Please edit your post and provide compilable (or at least syntactically valid) Java. – Jim Garrison Feb 13 '13 at 07:43
  • This type of question should instead be asked on [codereview.se] – CharlesB Feb 14 '13 at 07:52

2 Answers2

1

Here's my best guess at what you intended, with the syntax errors fixed and using generics to eliminate the casts:

public static interface Car{
    public void turnRight();
    public void turnLeft();
    public void go();
 }

 public abstract class LuxuryCar implements Car{
    public void systemCheckRightSideClear(){}
    public void systemCheckLeftSideClear(){}
 }

 public abstract class UsedCar implements Car{
    public void personCheckRightSideClear(){};
    public void personCheckLeftSideClear(){};
 }

 public interface Run<T extends Car> {
   public void makeLeftTurn(T car);
 }

 public class RunLuxuryCar implements Run<LuxuryCar>{
   @Override
   public void makeLeftTurn(LuxuryCar car){
      car.systemCheckLeftSideClear();
      car.turnLeft();
   }
 }

 public class RunUsedCar implements Run<UsedCar>{
    @Override
    public void makeLeftTurn(UsedCar car){
        car.personCheckLeftSideClear();
        car.turnLeft();
    }
 }
Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • How should my declaration of `Run` be if the class Car itself is a generic? `Run >` does not seem to work... – zapstar Sep 14 '15 at 14:49
0

The simple answer is that your interfaces should always express all operations that can be performed. If it doesn't make sense for some implementations to implement some methods, then your abstraction is wrong. In this case, your problem suggests that your Car interface needs both isLuxuryCar() and isUsedCar() methods so that you can tell the difference, in addition to the appropriate methods that need to be invoked for each one.

Going a step further, this isn't what inheritance is really for. What you've shown is something I'd expect to find in a textbook or a Java primer, and it's a wrong view of what inheritance is for. Inheritance and polymorphism are all about code reuse, and I find that inheritance relationships are discovered much more often than they're planned. You discover them when you find that you're writing similar code in related objects, at which point you extract a common superclass from them to consolidate the code into one place.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199