apex2022
apex2022

Reputation: 855

Java - incorrect creation of subclasses and super classes

First, I think the title of this post could be better, so if you want to edit it feel free to do so (or let me know how you think I should edit it).

I am going over practice problems for Java interviews. I am not interviewing right now, but I think this is the best way for me to find all my weak spots with Java. And before you say it, yes, I am finding I am VERY weak in many areas of Java and that I will need to do lots or review before interviewing.

I have some questions about the following code:

public class VehicleApp {

    public static void main(String[] args) {
        Ford myFord = new Ford();
        System.out.println(myFord.countWheels());
    
        Kawasaki myKawasaki = new Kawasaki(1985, "Eliminator");
        System.out.println(myKawasaki.countWheels());
    }
}

class Vehicle {

    protected String make;
    protected int numWheels;

    public Vehicle() { }

    public String countWheels() {
        return "The number of wheels this " + make + " has is " + numWheels + ".";
    }
}

class Ford extends Vehicle {
    public Ford() {
        make = "Ford";
        numWheels = 4;
    }
}

class Kawasaki extends Vehicle {
    private String model;
    private int year;

    public Kawasaki(int year, String model) {
        make = "Kawasaki";
        numWheels = 2;
        this.model = model;
        this.year = year;
    }

    public String countWheels() {
        return "The number of wheels this " + year + " " + make + " " + model + " has is " + numWheels + ".";
    }
}

First, I notice that there are no references to super() in the code. I thought that when you are dealing with super classes and subclasses, it was required that the subclass constructor include a reference to the super class constructor in the form of super(); (and including parameters if the super class constructor has them). Yet this code seems to work without them. Am I wrong about this requirement? Am I missing something else in this picture?

Second, the Kawasaki class doesn't include the decoration @Override for the countWheels() method. Since this method has the same name (albeit different parameters) as the super class' countWheels() method, wouldn't it be required to have an @Override decoration? Or is that only if the parameters are the same type and same order?

Thanks!

Upvotes: 0

Views: 113

Answers (1)

Booboo
Booboo

Reputation: 44188

If you do not explicitly call super() in your derived class, the Java compiler will automatically generate a call to super() for you. But this, of course, only works if the base class constructor takes no arguments. This can be demonstrated by adding a System.out.println("Constructor called."); statement to your otherwise empty Vehicle constructor.

The @Override decorator, as you have found out but have not convinced yourself of, is optional. But it is considered a "best practice" to use this when overriding a method for catching errors if you change the method signature.

The one, hopefully constructive, comment I would make is that since a Vehicle must have attributes make and numWheels, I personally would require that these be specified in the Vehicle constructor. Now there is no possibility of having a derived class with these attributes undefined.

public class VehicleApp {

    public static void main(String[] args) {
        Ford myFord = new Ford();
        System.out.println(myFord.countWheels());

        Kawasaki myKawasaki = new Kawasaki(1985, "Eliminator");
        System.out.println(myKawasaki.countWheels());
    }
}

class Vehicle {

    protected String make;
    protected int numWheels;

    public Vehicle(String make, int numWheels) {
        this.make = make;
        this.numWheels = numWheels;
    }

    public String countWheels() {
        return "The number of wheels this " + make + " has is " + numWheels + ".";
    }
}

class Ford extends Vehicle {
    public Ford() {
        super("Ford", 4);
    }
}

class Kawasaki extends Vehicle {
    private String model;
    private int year;

    public Kawasaki(int year, String model) {
        super("Kawasaki", 2);
        this.model = model;
        this.year = year;
    }

    @Override
    public String countWheels() {
        return "The number of wheels this " + year + " " + make + " " + model + " has is " + numWheels + ".";
    }
}

Upvotes: 1

Related Questions