Question

So I have these 3 classes, found them somewhere, where it said that it will make me understand what overriding is. Thing is that when I run this, I get Car@697eb767 and Ferrari@7e3b014c. Also, I made some changes for the program to run, on that site where I saw it the program was like this :

public class Car {
    public static void main (String [] args) {
        Car a = new Car();
        Car b = new Ferrari(); //Car ref, but a Ferrari object
        a.start(); // Runs the Car version of start()
        b.start(); // Runs the Ferrari version of start()
    }
}

class Car {
    public void start() {
        System.out.println("This is a Generic start to any Car");
    }
}

class Ferrari extends Car {
     public void start() {
         System.out.println("Lets start the Ferrari and go out for a cool Party.");
    }
}

But in this state, it doesn't run. So what should I do to get the program to actually show me these messages, not the Car@697eb767 and Ferrari@7e3b014 stuff ?

public class Car {
    public void start() {
        System.out.println("This is a Generic start to any Car");
    }
}

public class Ferrari extends Car {
    public void start()  {
        System.out.println("Lets start the Ferrari and go out for a cool Party.");
    }
}


public class Main {
    public static void main (String [] args) {
        Car a = new Car();
        Car b = new Ferrari(); //Car ref, but a Ferrari object
        System.out.println(a);
        System.out.println(b);
    }
}

Also I would really appreciate another example of java program which will get me to understand overriding better.

Was it helpful?

Solution

Overriding a method is when you take the method of a superclass and modify it in the sub class. Right now, your Car method extends the Object class. The reason you are getting Car@697eb767 is that you have not overridden the toString Method of object in you car.

In your Car class, Override the Object classes toString method by using the Override tag and creating the toString method. Inside that method, define what you want the car object to return.

@Override
public String toString(){
   String text = ""; <-------define what you want to return here
   return text;
 }

Similarly, you need to do this to the Ferrari class if you want a different toString output form the Car object since Ferrari extends Car.

OTHER TIPS

If you invoke start() methods of both classes like in the first sample, the output will be:

This is a Generic start to any Car

Lets start the Ferrari and go out for a cool Party.

The call b.start() will use Ferraris method, because you created it as Ferrari object and it doesn't matter whether you assigned object to generic class-declared variable or of concrete one.

The issue with Car@697eb767-like output when you output a and b objects is caused by default implementation of toString() method. In order to avoid this, you need to override it like this:

@Override
public String toString() {
    return "Hi, I'm Ferrari"; //or "I'm a car"
}

For further reading to understand inheritance, I recommend you to read these:

http://en.wikipedia.org/wiki/Inheritance_%28object-oriented_programming%29

http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

This is what I did I split into two files and is working for me

Car.java

public class Car {
    public static void main (String [] args) {
        Car a = new Car();
        Car b = new Ferrari(); //Car ref, but a Ferrari object
        a.start(); // Runs the Car version of start()
        b.start(); // Runs the Ferrari version of start()
    }


    public void start() {
        System.out.println("This is a Generic start to any Car");

    }
}


Ferrari.java


public  class Ferrari extends Car {

     @Override
     public void start() {
     System.out.println("Lets start the Ferrari and go out for a cool Party.");
}
}

Hope it helps you

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top