1

I am trying to fulfil the requirements of the homework... "Include at least one interface that contains at least one method that implementing classes must implement."

When I try to instantiate the interface it says it cannot instantiate the interface. I am not sure what I am doing wrong.

I have tried several ways to make it work.

//main class
public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {

        Ford mustang = new Ford("Mustang", 135, 125);
        Chevrolet camero = new Chevrolet("Camero", 202, 100);
        Dodge challenger = new Dodge("Challenger", 203, 75);        

        Nitrous nitro = new Nitrous();//problem code

        mustang.start();
        camero.start();
        challenger.start();
    }

}

//Abstract class
public abstract class Vehicle extends Thread implements Nitrous {

    private String model;
    private int speed;
    private int boost;

    public Vehicle(String model, int speed, int boost) {
        this.model = model;
        this.speed = speed;
        this.boost = boost;
    }

    public String getmodel() {
        return model;
    }

    public void setmodel(String model) {
        this.model = model;
    }

    public int getspeed() {
        return speed;
    }

    public void setspeed(int speed) {
        this.speed = speed;
    }

    public int getboost() {
        return boost;
    }

    public void setboost(int boost) {
        this.boost = boost;
    }


    @Override
    public void run() {
        try {
            go();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void go() throws InterruptedException {
        int trackLength = 5000;
        int checkPointPassed = 0;
        for (int i = 0; i < trackLength; i += (speed + boost)) {
            if (checkPointPassed * 1000 < i) {
                checkPointPassed++;
                System.out.println("The " + this.model + " has passed check point " + checkPointPassed);
//              System.out.println(nos);
                Thread.sleep(10);
            }
        }
    }

}

//subclass one of three
public class Ford extends Vehicle {

    public Ford (String model, int speed, int boost) {

        super(model, speed, boost); 

    }

    @Override
    public void nos() {
        // TODO Auto-generated method stub
        System.out.println("The cars have Nitro!");
    }

}

public class Chevrolet extends Vehicle{

    public Chevrolet(String model, int speed, int boost) {
        // TODO Auto-generated constructor stub.
        super(model, speed, boost);
    }

    @Override
    public void nos() {
        // TODO Auto-generated method stub
        System.out.println("The cars have Nitro!");
    }

}

public class Dodge extends Vehicle{

    public Dodge(String model, int speed, int boost) {
        // TODO Auto-generated constructor stub
        super(model, speed, boost);
    }

    @Override
    public void nos() {
        // TODO Auto-generated method stub
        System.out.println("The cars have Nitro!");
    }

}

//Interface

public interface Nitrous {

    public void nos();
}

It is a race with three vehicles that have a nitrous boost. I have chosen to make the Nitrous the interface. You can see in my code where I have tried different ways to make it work and none have been successful. I don't even know if I am close or way off with how to do this.

  • There's no reason to instantiate the interface; all three vehicles implement (override) the interface so that is all you need. –  Oct 18 '19 at 19:01
  • You may want to make the `nos` function more interesting by defining it as `boolean hasNitro`, have each vehicle subclass decide what it returns and use the `hasNitro` in the Vehicle `go`. –  Oct 18 '19 at 19:11
  • Ohhh, nifty idea Andy...I might try that. – Mordsith_T Oct 18 '19 at 19:21

1 Answers1

0

An interface only provides a blueprint on what a specific class can do that implements it. To give you a concrete example, consider the interface Fruit.

public interface Fruit {

  void eat();

}

Now, it does not make sense to instantiate an interface, since it is only an abstraction. However, it would make sense to actually create an object of the class Banana or Apple. And since these are all fruits, you can eat them.

for (Fruit f : fruits) {
  f.eat();
}

Since your Vehicle class implements Nitrous, all of its subclasses can be assigned to that specific type as well, and they fulfill its contract, which is nos().

Ford mustang = new Ford("Mustang", 135, 125);

Nitrous asNitrous = mustang; // works
mustang.nos(); // works

Related: What is the definition of an interface in object oriented programming?

Glains
  • 2,773
  • 3
  • 16
  • 30