Search code examples
javatostringsuperclass

Print toString from different classes


I've been trying to figure this out for a while now, I've searched through various questions but I don't think I'm geting any closer.

I have a superclass Vehicle that gives various details of a vehicle. I then have my Car class which inherits from Vehicle.

I am trying to print the toString from my Car class, which overides the Vehicle class.

Basically I want to print out the information about each car.

Here is my main

    public static void main(String []args)

    {

        Car Ford = new Car(Red, 4, true);

        //Ford = Ford.;   Tried various things here to no avail, kept getting nullfalse in output

        Car Honda = new Car(Blue, 4, false);


        System.out.println(Ford.vehicleDetails);

        System.out.println("Honda");

    }

}

This is my Vehicle class

public class Vehicle

{

// Variables

private String vehicleColour;

private int numberOfWheels;

String vehicleDetails;

// Constructor

public Vehicle(String vehicleColour, int numberOfWheels)

{

}

// Getters

public String getVehicleColour()

{
    return vehicleColour;
}

public int getNumberOfWheels()

{
    return numberOfWheels;
}



// Setters

public void setVehicleColour(String vehicleColour)

{
    this.vehicleColour = vehicleColour;
}


public void setNumberOfWheels(int numberOfWheels)

{
    this.numberOfWheels = numberOfWheels;
}


// Constructor

public Vehicle ()

{

}


// toString method super

public String toString() {

    String vehicleDetails = (getVehicleColour() + "" + getNumberOfWheels());

    return vehicleDetails;

}

}

And this is my Car class

    public class Car extends Vehicle

{

    // Variables

    private boolean convertible;

    Car vehicleDetails;

    // Getter

    public boolean getConvertible()

    {
        return convertible;
    }

    // Setter

    public void setConvertible(boolean convertible)

    {
        this.convertible = convertible;
    }

    // Constructor

    public Car(String vehicleColour, int numberOfWheels, boolean convertible) {


    }


        // toString method override

        @Override
        public String toString() {

        return super.toString() + convertible;

        }

    }

I want my output to be something like "The Ford is red, has 4 wheels and is a convertible", with bold text coming from Vehicle and Car classes respectively.

How do I get the bold text to show up in output? Right now I just get default values such as null, 0 and false.

I appreciate any input, I know I am probably doing something really stupid that is really obvious but I just can't see it.

Thanks.


Solution

  • I want my output to be something like "The Ford is red, has 4 wheels and is a convertible"

    For your above requirement simple solution:

    System.out.println("The Ford is "+ Ford.toString() );
    

    If you intend to get the whole output from Ford.toString then while creating Ford object you need to pass the car name too! Like shown below and make necessary changes in constructor for it!

    Car Ford = new Car("Ford","Red", 4, true);
    

    However constructors in Car and Vehicle class are made but no value is assigned to the local variables from them! Thus .toString() was not getting anything to show!

    Required changes are:

    In Main Class

    System.out.println("The Ford is "+ Ford.toString() );
    

    In Car Class

    //change in the constructor
    public Car(String vehicleColour, int numberOfWheels, boolean convertible) {
    
        setNumberOfWheels(numberOfWheels);
        setVehicleColour(vehicleColour);
        setConvertible(convertible);
    
    }
    

    I am trying to print the toString from my Car class, which overides the Vehicle class.

    //change in toString()
    
    
        @Override
        public String toString() {
            if(convertible){
                 return super.toString() +"and is a convertible" ;
            }else{
                 return super.toString() +"and is not a convertible" ;
            }
    
    
        }
    

    In Vehicle Class

    //change the constructor
    public Vehicle(String vehicleColour, int numberOfWheels)
    
    {
            setNumberOfWheels(this.numberOfWheels);
            setVehicleColour(this.vehicleColour);
    
    }
    

    Basically I want to print out the information about each car.

    //change the toString()
    public String toString() {
    
        String vehicleDetails = (getVehicleColour() + ", has "  +getNumberOfWheels()+" wheels");
    
        return vehicleDetails;
    
    }