Sunday, June 15, 2014

What is Function Over-Riding and Over-Loading in Java?

Snippet below should explain things better.

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.");
}
}

No comments:

Post a Comment