Frage

Is it possible to overload constructors of a subclass because I have been racking my brain and I really can't figure it out. Below is the whole of my code so far and the question.

It is school work but its not graded as we have a concepts exam in a few weeks so I'm trying to get through as many exercises as possible.

Exercise 4 – Overload constructors:

We have noticed that the majority of the computer labs have a capacity of 20.

Write a new constructor for the ComputerLab class that only has one parameter, the room number, and initialise the capacity to 20. We will have now overloaded the constructor for the ComputerLab class, so that there are two ways to create instances of the class.

Week7.java:

package week7;


public class Week7 {
    /*
     * @param args the command line arguments
     */

    public static void main(String[] args) {
        Room nonTech = new Room("t12", 25, true);
        System.out.println("nonTech");
        System.out.println(nonTech.getRoomNumber());
        System.out.println(nonTech.getCapacity());
        System.out.println(nonTech.hasProjector());

        ComputerLab tech = new ComputerLab("r12", 10, true, "win7");
        System.out.println();
        System.out.println("tech");
        System.out.println(tech.getRoomNumber());
        System.out.println(tech.getCapacity());
        System.out.println(tech.hasProjector());
        System.out.println(tech.getOS());

        // hasProjector == false however it will also return true due 
        //to the override method lower down
        LectureRoom mainhall = new LectureRoom("somthing", 100, false);
        System.out.println();
        System.out.println("mainhall");
        System.out.println(tech.hasProjector());

    }
}

class Room {

    String roomNumber;
    int capacity;
    boolean projection;

    public Room(String rm, int n, boolean p) {
        roomNumber = rm;
        capacity = n;
        projection = p;
    }

    public String getRoomNumber() {
        return roomNumber;
    }

    public int getCapacity() {
        return capacity;
    }

    public boolean hasProjector() {
        return projection;
    }
}

class ComputerLab extends Room {
    // RoomNumber, capacity, projection inherited

    private String os;

    public ComputerLab(String rm, int n, boolean p, String os) {
        super(rm, n, p);
        this.os = os;
    }

    public String getOS() {
        return os;
    }

    public void setOS(String update) {
        os = update;
    }

}

class LectureRoom extends Room {
    // RoomNumber, capacity, projection inherited

    public LectureRoom(String rm, int n, boolean p) {
        super(rm, n, p);
    }

    // Overrides Superclass hasProjector
    public boolean hasProjector() {
        return true;
    }
}
War es hilfreich?

Lösung

You need to add a constructor for the ComputerLab class that sets the capacity to 20:

public ComputerLab(String roomNumber) {
    super(roomNumber, 20, false); // is false the correct default value?
    this.os = null; // what is the correct default value?
}

super here invokes the superclass constructor (Room in this case) so you pass to it the argument you want. In this specific case, your ComputerLab instance is a Room with the room number roomNumber (the argument of the new ComputerLab constructor), a capacity set to 20 and a projection set to false.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top