简体   繁体   中英

Inheritance implementation

How to implement inheritance in this exercise?

A car dealer wants a computer system to manage the data of their vehicles and classify them by type.

  • All cars have the following data

    1. Engine serial number
    2. Brand
    3. Year
    4. Price
  • The vehicles are classified into compact cars , luxury cars , trucks and wagons .

  • For cars and wagons, it is also important to store the number of passengers ; While for trucks the load capacity should be controlled in kgs. And the number of axes and rounds .

  • Model this system and instantiate each of the classes, assigning data through their respective properties. Add a parameterized constructor to each class to initialize its data and invoke the base class constructor from the constructor of each derived class (do not use default constructors).

  • Implement the override of the toString() method to display the data for each type of auto. NOTE: Duplicate components are not allowed in classes or empty classes (no elements).

What I've tried: (I don't know if inheritance is correct)

interface Passegers {

    int CantidadPasajeros(int n);

    int Pasajeros();
}

class Vehicle {}

class Auto extends Vehicle implements Passegers {

    private int numSerieMotor;
    private String marca;
    private int año;
    private double precio;

    public int CantidadPasajeros(int n) {
        return n;
    }
}

class CompactAutos extends Auto {
    public int CantidadPasajeros(int n) {
        return n;
    }
}

class LuxuryAutos extends Auto {
    public int CantidadPasajeros(int n) {
        return n;
    }
}

class Truck extends Auto {
    private int capacity;
    private int axes;
    private int rounds;
}

class Wagon extends Vehicle implements Passegers {
    public int CantidadPasajeros(int n) {
        return n;
    }
}

public class Tarea22ago {
    public static void main(String[] args) {
        Vehicle x[] = new Vehicle[3];
        x[0] = new Auto();
        x[1] = new Wagon();
        x[2] = new Truck();
    }
}

However, the class Vehicle would be empty. I still have to write the constructors in each class, but I just want to be sure if this usage of inheritance is correct.

I'll just go through your code and comment top to bottom. A general note is to follow Java code convention.

Your interface is fine as such, but naming and the convention that a setter doesn't return a value makes it

interface Passengers {
    void setPassengers(int count);
    int getPassengers();
}

You might as well throw in an interface to vehicles with load.

interface LoadVehicle {
    int getCapacity();
    int getAxes();
    int getRounds(); // what is this anyway?
}

Note that there are no setters in this interface; these items are not going to change (cfg GhostCat's comment below).

The Vehicle class might as well be a marker interface ("marker" saying that it just denotes it is a vehicle, but doesn't define any methods).

interface Vehicle {}

(This could make the other two interfaces extend it, as in interface Passengers extends Vehicle and interface LoadVehicle extends Vehicle ).

The Auto class doesn't have the constructor that was required. Not all Autos seem to have Passengers, so there's no need to implement that interface.

class Car implements Vehicle {
    private int motorNumber, makeYear;
    private String brand;
    // add getter/setters for the price? This is something that can change.
    private double price;

    // constructor. This needs to be called from all subclasses as well
    Car(String brand, int year, int engNum, double price) {
        // assign parameters to variables
    }            
}

I don't see a point in creating a CompactCar and LuxuryCar class, since they don't seem to have a distinction as such. A general class should do it.

class PassengerCar extends Car implements Passengers {
    // add getter and setter for this to implement the interface
    private int passengers;
    PassengerCar(String brand, int year, int engNum, double price) {
        super(brand, year, engNum, price);
    }
}

You can just create LuxuryCars or Compacts as instances of this.

In the same vein, you can now create a Car for trucks.

class LoadCar extends Car implements LoadVehicle {
    // again, getters for these for the interface
    private int capacity, axes, rounds;
    // and constructor
    LoadCar(String brand, int year, int engNum, double price, 
        int cap, int axCount, int roundCount) {
        super(brand, year, engNum, price);
        // assigs the other parameters to variables
    }
}

The truck can just be created as a LoadCar if there is no other distinction.

Now for the wagon, your Wagon class does not have the Load-Functions that are required. You have to decide whether to subclass LoadCar or PassengerCar; after all, it is both. You'll need to implement the other missing interface in either case. I'd go with this because the Passengers interface is simpler:

class Wagon extends LoadCar implements Passengers {
    // again, getter and setter for this for the interface
    private int passengers;
    Wagon(String brand, int year, int engNum, double price, 
        int cap, int axCount, int roundCount) {
        super(brand, year, engNum, price, cap, axCount, roundCount);
}

It doesn't really matter which to extend because in the system in which you'd use these classes, you'd only be working with the interfaces.

EDIT: So here's how you would create your objects:

// create a luxury car
PassengerCar luxuryCar = new PassengerCar("BMW", 2017, 322131, 49_999.99);
// create a compact
PassengerCar compactCar = new PassengerCar("Smart", 2014, 55231, 25_999.99);
// create a truck
LoadCar truck = new LoadCar("Mercedes", 2014, 2113321, 99_999.99, 10_000, 6, 4);
// create a wagon
Wagon wagon = new Wagon("Dodge", 2005, 551223, 19_999.99, 3_000, 6, 4);

And to maybe show the effects of this inheritance:

List<Vehicles> vehicles = new ArrayList<>();
List<Passenger> passengerVehicles = new ArrayList<>();
List<LoadCar> loadVehicles = new ArrayList<>();

// everything is a vehicle:
vehicles.add(luxuryCar); // fine
vehicles.add(compactCar); // fine
vehicles.add(truck); // fine
vehicles.add(wagon); // fine

// you can only add passenger vehicles the the passenger car list:
passengerVehicles.add(luxuryCar); // fine
passengerVehicles.add(compactCar); // fine
passengerVehicles.add(wagon); // fine
passengerVehicles.add(truck); // NO! a truck is not a passenger car.

// you can only add load cars to the load car list:
loadVehicles.add(luxuryCar); // NO! a PassengerCar is not a LoadCar.
loadVehicles.add(compactCar); // NO, same reason
loadVehicles.add(wagon); // fine
loadVehicles.add(truck); // fine

Incidentally, here's another list:

List<Car> cars = new ArrayList<>();
// everything is a car:
cars.add(luxuryCar); // fine
cars.add(compactCar); // fine
cars.add(truck); // fine
cars.add(wagon); // fine

So what's the point in the distinction between Vehicle and Car? Well let's say you start selling boats...

class Boat implements Vehicle, Passenger {
    // add getter/setter
    private int passengers;
    public Boat(int keelDepth) {
    }
}

This would mean:

Boat boat = new Boat(15);
vehicles.add(boat); // fine
cars.add(boat); // NO! it's not a car.
passengerVehicles.add(boat); // well yes that works.

This conception might help you better:

public interface IVehicle{
  int getNumberOfWheels();
}

public interface IPassenger{
  void getGender();
  void getType(); // Driver or customer
}

public class Vehicle implements IVehicle{

  protected ArrayList<Passenger> _passengers;

  public Vehicle(int numberOfPassengers){
    _passengers = new ArrayList<Passenger>(numberOfPassengers);
  }

}

public class Passenger implements IPassenger{
}

public interface IVehicleAuto implements IVehicle{
}

public abstract class VehicleAuto extends Vehicle implements IVehicleAuto{
}

public class Car extends Vehicle {
}

public class CarAuto extends Car implements IVehicleAuto {
}

ditch the passengers interface, instead have the vehicle class have an instance variable called passengers. make vehicles abstract. subclass as you are doing. refactor any common instance variables into the vehicle class. add the constructors for each class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM