-1

i'm beginner and just learning abstract classes and interfaces but really struggeling to get it right.

MY Class structure.

  • Abstract Class Vehicle
  • Class Car (extends Vehicle, implements ITuning)
  • Class Motorcycle (extending Vehicle)
  • Interface ITuning

I want to have an abstract method doRace() that I can use in my Car class to compare the Car Objects (car.getHp()) and prints the winner. I try to implement a doRace(object o) into the Interface ITuning and a doRace(Car o) into my Car class but get the error. How can I implement that correctly ?

The type Car must implement the inherited abstract method ITuning.doRace(Object)

But if do chage it to Object, i can't use it in my Car class…

public interface ITuning
{
abstract void doRace(Object o1);
}
public Car extends Vehicle implements ITuning
{
 public void doRace(Car o1){
 if(this.getHp() > o1.getHp())
 };
}

Any Idea what i'm doing wrong? I assume its a comprehension error

  • 2
    Hint: `doRace(Object o1)` and `doRace(Car o1)`. Why are you using `Object` in your interface? – Marvin Nov 10 '22 at 13:41
  • Where is `getHp()` defined? In `Car` or in `Vehicle`? The basic idea would be to use that class in your `doRace()` method in the interface declaration. – Marvin Nov 10 '22 at 13:47
  • Tbh I tried "Object" as a placeholder in doRace(Obeject) as I wasn't sure what to use. Whatever i tried, it does not work. get(Hp) is an abstract method in Vehicle Class and implemented in Car & Motorcylce class, as I wanted to use it in both. –  Nov 10 '22 at 14:26

2 Answers2

1

You can make ITuning generic.

public interface ITuning<T> {
  
  void doRace(T other);
}

Implementation will be like this:

public class Car extends Vehicle implements ITuning<Car> {

  @Override
  public void doRace(Car other) {
    //do comparison
  }
}

Implementation in other classes will be quite similar, just change the generic parameter.

As a side note, i would rename the interface to something more fitting. Considering that tuning a vehicle is the act of modifying it to optimise its' performance, ITuning providing functionality to do the actual racing is counter intuitive.

Chaosfire
  • 4,818
  • 4
  • 8
  • 23
  • Thanks for your answer! Just for clarification does that mean if i want my "interface" to interact with multiple Objects (like comparing) I need to change it to generic ? –  Nov 10 '22 at 14:38
  • 1
    @EddiOne That depends. If you mean like `Comparator` and `Comparable`, which compare objects of the **same** type, yes, it has to be generic. If you want to race, let's say car vs motorcycle, **different** types, then generic is not necessary, although it can be done with generic as well, by providing bound to it. See [Marvin's answer](https://stackoverflow.com/a/74390783/17795888) for non generic solution. – Chaosfire Nov 10 '22 at 14:52
0

You can change your ITuning interface to

public interface ITuning
{
    void doRace(Vehicle other);
}

Since Vehicle defines the getHp() method this ensures that your actual implementation can access it.

The difference to Chaosfire's answer is that this will allow you to do a race between different types of vehicles, e.g. a Car and a Motorcycle, while the generic ITuning<T> class will be limited to races between equal types*.

Chaosfire's point regarding the interface name is still valid.

*This is a bit over-simplified to make my point clearer. Of course Car could implement ITuning<Car> and ITuning<Motorcycle> but this may not be what you want.

Marvin
  • 13,325
  • 3
  • 51
  • 57
  • Thanks Marvin, that is exactly what i was tying to do. However If I change this as you said where do I implement the actual method? If I do in the abstract Vehicles class, i don't need the Interface ITuning anymore… –  Nov 10 '22 at 14:55
  • Okay i think i found my Issue. My main class was following `public static void main(String[]args){. Car car1 = new Car("bmw", 90) Motorcycle moto1 = new Motorcyle("Ducati", 100) Vehicle moto2 = new Motorcycle("Yamaha", 96) } ` Thats why it wasn't working. Now i changed the last Object to Motorcylce moto = new Motorcycle("Yamaha", 96) and implemented the method doRace into Motorcylce.java and Car.java. Now I can race all kinds. Thank you –  Nov 10 '22 at 15:37
  • 1
    I'd argue that you probably indeed do not need the `ITuning` interface. If you are unsure about the differences between an interface and an abstract class, maybe a look at [Interface vs Abstract Class (general OO)](https://stackoverflow.com/q/761194/4616087) helps. – Marvin Nov 10 '22 at 16:10