Hi, I'm trying to learn inheritance. Here is the code of my business tier package class so far:
/** * This is the Parent class for all vehicle objects. * Child classes will be SUV, Truck, and Sedan. * An override of a model could be something like Toyota Corolla LE and Toyota Corolla S. */ package vehicle.business; import java.lang.Object; public class Vehicle { //instance variables private String make; private int year; private String model;//override possibly needed private double price;//override possibly needed //constructor public Vehicle() { make = ""; year = 0; model = ""; price = 0.00; } public void setMake() { this.make = make; } public String getMake() { return make; } public void setYear() { this.year = year; } public int getYear() { return year; } public void setModel() { this.model = model; } public String getModel() { return model; } public void setPrice() { this.price = price; } public double getPrice() { return price; } //Since the model name and price might vary depending on the vehicle type (this is where //the child classes come in, i.e. SUV, Truck, Sedan), we should provide override methods //just in case: @Override public String toString() { return model; } @Override public String toString() { return price; } }
The Object class is the superclass for all Java classes; the methods of the Object class are available from every object. I assumed that meant the Object class was a special class whose methods could be used as needed, but I got the "Duplicate method toString() in type Vehicle" error on lines 57 and 62 when I tried to provide the toString overriding method for more than one instance variable.
Am I only limited to one toString method? What if I needed my program to provide overriding for more than one string type attribute of an object?