Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Implementing Interfaces

I am working on a homework assignment for my into to programming class that involves implementing interfaces. The problem here is that I really just flat out don't understand interfaces or what they are used for (the professor was not very good about explaining it).

The assignment was to make a "Vehicle" superclass, and than three subclasses, something like "Truck" or "Jeep" that would each have a couple traits of their own. The "Vehicle" class must implement the comparable interface, which I think I have figured out (I have the compareTo() method written comparing the number of doors on vehicles), and one other class must also implement the "Hybrid" class (I have no idea what this means). We then have to implement the toString(), equals(Object o), and compareTo(Object o) methods.

I think I have the compareTo() down, but with the equals() I have no idea. The last thing we have to do is write a Main to test, this involves making an array of Vehicle objects, printing them out, sorting them, and then re printing them. It should also iterate through the array and print out the price premium for hybrids, and use the equals(Object o) method to compare 2 Vehicles.

Here is the code for my "Vehicle" superclass

package vehicle;

abstract public class Vehicle implements Comparable {
    private String color;
    private int numberOfDoors;

    // Constructor
    /**
     * Creates a vehicle with a color and number of doors
     * @param aColor The color of the vehicle
     * @param aNumberOfDoors The number of doors
     */
    public Vehicle(String aColor, int aNumberOfDoors) {
        this.color = aColor;
        this.numberOfDoors = aNumberOfDoors;
    }

    // Getters
    /**
     * Gets the color of the vehicle
     * @return The color of the vehicle
     */
    public String getColor() {return(this.color);}
    /**
     * Gets the number of doors the vehicle has
     * @return The number of doors the vehicle has
     */
    public int getNumberOfDoors() {return(this.numberOfDoors);}

    // Setters
    /**
     * Sets the color of the vehicle
     * @param colorSet The color of the vehicle
     */
    public void setColor(String colorSet) {this.color = colorSet;}
    /**
     * Sets the number of doors for the vehicle
     * @param numberOfDoorsSet The number of doors to be set to the vehicle
     */
    public void setNumberOfDoors(int numberOfDoorsSet) {this.numberOfDoors = numberOfDoorsSet;}

    @Override
    public int compareTo(Object o) {
        if (o instanceof Vehicle) {
            Vehicle v = (Vehicle)o;
            return this.numberOfDoors - v.getNumberOfDoors();
        }
        else {
            return 0;
        }
    }

    /**
     * Returns a short string describing the vehicle
     * @return a description of the vehicle
     */
    @Override
    public String toString() {
        String answer = "The car's color is "+this.color
                +". The number of doors is"+this.numberOfDoors;
        return answer;
    }
}

And I will also post one of my subclasses

package vehicle;

abstract public class Convertible extends Vehicle {
    private int topSpeed;

    // Constructor
    /**
     * Creates a convertible with a color, number of doors, and top speed
     * @param aColor The color of the convertible
     * @param aNumberOfDoors The number of doors of the convertible
     * @param aTopSpeed The top speed of the convertible
     */
    public Convertible (String aColor, int aNumberOfDoors, int aTopSpeed) {
        super(aColor, aNumberOfDoors);
        this.topSpeed = aTopSpeed;
    }

    // Getters
    /**
     * Gets the top speed of the convertible
     * @return The top speed of the convertible
     */
    public int getTopSpeed() {return(this.topSpeed);}

    // Setters
    /**
     * Sets the top speed of the convertible
     * @param topSpeedSet The top speed to set to the convertible
     */
    public void setTopSpeed(int topSpeedSet) {this.topSpeed = topSpeedSet;}

    /**
     * Returns a short description of the convertible
     * @return a short description of the convertible
     */
    @Override
    public String toString() {
        String answer = "The car's color is "+super.getColor()
                +", the number of doors is "+super.getNumberOfDoors()
                +", and the top speed is "+this.topSpeed+" mph.";
        return answer;
    }
}

I am definitely not asking anyone to do the work for me, but if someone could shed a bit more light on how interfaces work and what this homework is really asking that would be great.

All help is very much appreciated

Thanks!

like image 351
salxander Avatar asked Apr 16 '12 17:04

salxander


People also ask

What is implementing interfaces in Java?

The implements keyword is used to implement an interface . The interface keyword is used to declare a special type of class that only contains abstract methods. To access the interface methods, the interface must be "implemented" (kinda like inherited) by another class with the implements keyword (instead of extends ).

CAN interface in Java have implementation?

All methods of an Interface do not contain implementation (method bodies) as of all versions below Java 8. Starting with Java 8, default and static methods may have implementation in the interface definition.

Can we implement 2 interfaces in Java?

Yes, we can do it. An interface can extend multiple interfaces in Java.


1 Answers

Rather than doing your particular example I will run through why interfaces are useful and how to use them in a more general case.

What is an Interface?

When I first started programming I also found the concept of an interface to be confusing so I like to think of it as a standard "rule book". Every class which implements this rule book has a list of "rules" it must follow. For example, consider the following interface:

interface Bounceable{
  public void setBounce(int bounce);
  public int getBounce();
}

This above rule book declares an interface for something that bounces. It states that anything that is bounceable must set its bounce and also get its bounce. Any class which implements this interface must follow the rule book.

Why would this rule book be useful?

Well, what if you want to code up a playground, where kids play with all sorts of bouncy things. You might make the following types of bouncy things..

public class FootBall implements Bounceable{
private int bounce;

public void setBounce(int bounce){
   this.bounce = bounce;
}

public int getBounce(){
  return this.bounce;
}

}

public class BaseBall implements Bounceable{
private int bounce;

public void setBounce(int bounce){
   this.bounce = bounce;
}

public int getBounce(){
  return this.bounce;
}

}

The above classes define a type of bouncy ball. You would then make your playground class and could define methods around the abstract Bounceable interface. For example, what if a basketball hoop was a method in your class? what if it could accept any bouncy thing as an argument? This would mean that you could pass any kind of ball as long as it implements bounceable. If you didn't have interfaces or similar functionality you could see how messy your code would get the more balls you implement.

EDIT: I've included a small practical example..

A practical example of this would be..

public void slamDunk(Bounceable bouncyThing){
  System.out.println("You scored three points!");
}

Both of the following calls to slamDunk are valid...

slamDunk(new BaseBall());

slamDunk(new FootBall());

Now your slameDunk function can score points with any bounceable object.

like image 141
Aidanc Avatar answered Sep 24 '22 19:09

Aidanc