Consider the following classes.
public class GraphicObject {
int id;
public GraphicObject(int id) {
this.id = id;
}
public void print() {
System.out.println("Printing object " + id);
}
}
public class Rectangle extends GraphicObject {
public Rectangle(int id) {
super(id);
}
@Override
public void print() {
super.print();
System.out.println("Printing something extra for rectangle " + id);
}
}
Suppose that you wanted to add another print()
method on Rectangle
.
public void print(String value) {
System.out.println( "Printing rectangle " + id + " with parameter \"" + value + "\"");
}
The modified Rectangle
class now has two print
methods.
void print()
void print(String value)
The second method does not override print()
, but
overloads it!
Overriding
Overloading:
Here is a concrete comparison:
What will be the output of the following program?
class X {
void method(int a) {
System.out.println("ONE");
}
void method(double d) {
System.out.println("TWO");
}
}
class Y extends X {
@Override
void method(double d) {
System.out.println("THREE");
}
}
public class Quiz1 {
public static void main(String[] args) {
new Y().method(100);
}
}
ONE
, TWO
or THREE
?
What will be the output of the following program?
class A { }
class B extends A { }
class C { }
public class Quiz2 {
static void overloadedMethod(A a) {
System.out.println("ONE");
}
static void overloadedMethod(B b) {
System.out.println("TWO");
}
static void overloadedMethod(Object obj) {
System.out.println("THREE");
}
public static void main(String[] args) {
C c = new C();
overloadedMethod(c);
}
}
ONE
, TWO
or THREE
?
What will be the output of the following program?
class J { }
class K extends J { }
class L extends K { }
public class Quiz3 {
static void overloadedMethod(J j) {
System.out.println("ONE");
}
static void overloadedMethod(K k) {
System.out.println("TWO");
}
static void overloadedMethod(Object obj) {
System.out.println("THREE");
}
public static void main(String[] args) {
L l = new L();
overloadedMethod(l);
}
}
ONE
, TWO
or THREE
?
What will be the output of the following program?
class M {}
class N extends M {}
class O extends N {}
public class Quiz4 {
static void overloadedMethod(M m) {
System.out.println("ONE");
}
static void overloadedMethod(N n) {
System.out.println("TWO");
}
static void overloadedMethod(O o) {
System.out.println("THREE");
}
public static void main(String[] args) {
M m = new O();
N n = new O();
O o = new O();
overloadedMethod(m);
overloadedMethod(n);
overloadedMethod(o);
}
}
ONE
, TWO
or THREE
?
ONE
, TWO
or THREE
?
ONE
, TWO
or THREE
?
Write a Calculator
class that has a method to calculate
displacement during uniform acceleration double calculateDisplacement(double time, double acceleration, double initialVelocity)
Remember that d = (v0 * t) + (a * t^2)/2
where:
d
: distancev0
: initial velocitya
: accelerationt
: timeOverload calculateDisplacement
with two new versions:
double calculateDisplacement(double time, double acceleration)
assuming initialVelocity = 1
double calculateDisplacement(double time)
assuming also acceleration = 1
public class Calculator {
// Add methods here
public static void main(String[] args) {
System.out.println("Distance: " + calculateDistance(10, 1, 1));
System.out.println("Distance: " + calculateDistance(10, 1));
System.out.println("Distance: " + calculateDistance(10));
}
}
You can find the solution to this exercise here.