Skip to content
Advertisement

Java toString in class

I am getting an error of cannot find symbol in my code posted below. I am trying to initalise a sedan using it’s class and have the toString function right after but nothing is working for me. Please help

class cars {
    String make;
    String model;
    String color;
    int year;
    double price;

    public String sedancClass(String initMake, String initModel, String initColor, int initYear, double initPrice) {
        make = initMake;
        model = initModel;
        color = initColor;
        year = initYear;
        price = initPrice;
        String name = "Sedan";
        String main = (color + " " + make + " " + model + " " + name + " " + year + " costs $" + price);
        return main;

    }





}

public class autoPark {
    public static void main(String args[]) {
        cars sedan1;
        sedan1 = new sedanClass("Ford" , "Model-1" , "white" , 2015, 20000);
    }
}

Advertisement

Answer

According to what you provided, I think this is what you are trying to do

class cars {
    String make;
    String model;
    String color;
    int year;
    double price;

    // parametised constructor
    public cars (String initMake, String initModel, String initColor, int initYear, double initPrice) {
        make = initMake;
        model = initModel;
        color = initColor;
        year = initYear;
        price = initPrice;
    }

    @Override
    public String toString() {
        String name = "Sedan";
        String main = (color + " " + make + " " + model + " " + name + " " + year + " costs $" + price);
        return main;
    }

}

public class autoPark {
    public static void main(String args[]) {
        cars sedan1; // declaring cars object by name sedan1
        sedan1 = new cars("Ford" , "Model-1" , "white" , 2015, 20000); // initialising sedan1 using cars constructor
        System.out.println(sedan1); // printing sedan1 for invoking toString() method
    }
}

Why we use @Override annotation source

Using @Override annotation while overriding a method is considered as a best practice for coding in java because of the following two advantages:

  1. If programmer makes any mistake such as wrong method name, wrong parameter types while overriding, you would get a compile time error. As by using this annotation you instruct compiler that you are overriding this method. If you don’t use the annotation then the sub class method would behave as a new method (not the overriding method) in sub class.

  2. It improves the readability of the code. So if you change the signature of overridden method then all the sub classes that overrides the particular method would throw a compilation error, which would eventually help you to change the signature in the sub classes. If you have lots of classes in your application then this annotation would really help you to identify the classes that require changes when you change the signature of a method.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement