Chapter 11 - Part03
Chapter 11 - Part03
1
11.4 Overriding Methods
A subclass inherits methods from a superclass. Sometimes it is necessary
for the subclass to modify the implementation of a method defined in the
superclass. This is referred to as method overriding.
public class Circle extends GeometricObject {
// Other methods are omitted
// This method Overrides the toString method defined in GeometricObject
// This means that this method modifies the code of the toString in GeometricObject
public String toString() {
return super.toString() + "\nradius is " + radius;
}
}
public class GeometricObject {
// Other methods are omitted
public String toString() {
String s = "The color is " + color;
if (filled)
s += ".\nThe geometric object is filled";
else
s += ".\nThe geometric object is not filled";
return s;
}
} 2
11.4 Overriding Methods
To avoid mistakes, you can use a special Java syntax, called override
annotation, to place @Override before the method in the subclass as in the
below example.
public class Circle extends GeometricObject {
// Other methods are omitted
@Override
public String toString() { // overrides the toString method in GeometricObject
return super.toString() + "\nradius is " + radius;
}
}
4
5.8 Overloading Methods
Overloading methods enables you to define the methods with
the same name as long as their signatures (parameter lists) are
different.
The max method below works only with the double data type.
public static double max(double num1, double num2) {
if (num1 > num2)
return num1;
else
return num2;
}
But what if you need to determine which of two integer
numbers has the maximum value? The solution is to create
another method with the same name but different parameters, as
shown in the next slide.
5
5.8 Overloading Methods
public class TestMethodOverloading {
public static void main(String[] args) {
// Invoke the max method with the double parameters
System.out.println("The maximum of 3 and 4 is " + max(3, 4));
// Invoke the max method with the double parameters
System.out.println("The maximum of 3.0 and 5.4 is " + max(3.0, 5.4));
}
// Find the max of two int values
public static int max(int num1, int num2) {
if (num1 > num2)
return num1;
return num2;
}
// Find the max of two double values
public static double max(double num1, double num2) {
if (num1 > num2)
return num1;
return num2; Output
} The maximum of 3 and 4 is 4
}
The maximum of 3.0 and 5.4 is 5.4
6
5.8 Overloading Methods
If you call max with int parameters, the max method that expects
int parameters will be invoked; if you call max with double
parameters, the max method that expects double parameters will
be invoked.
8
5.8 Overloading Methods
Overloading methods can make programs clearer and more readable.
Methods that perform the same function with different types of
parameters should be given the same name.
Overloaded methods must have different parameter lists. You cannot
overload methods based on different modifiers or return types. For
example, the below is not overloading and will cause a syntax error
since although the return types are the different, the methods
signatures (parameter lists) are the same.
public static int max(int num1, int num2) {
// body
}
public static double max(int num1, int num2) {
// body
}
9
11.5 Overriding vs. Overloading
Let us use an example to show the differences between overriding and
overloading. In (a) below, the method p(double i) in class A overrides the same
method defined in class B. In (b), however, the class A has two overloaded
methods: p(double i) and p(int i). The method p(double i) is inherited from B.
public class Test { public class Test {
public static void main(String[] args) { public static void main(String[] args) {
A a = new A(); A a = new A();
a.p(10); a.p(10);
a.p(10.0); a.p(10.0);
} }
} }
class B { class B {
public void p(double i) { public void p(double i) {
System.out.println(i * 2); System.out.println(i * 2);
} }
} }
Overloading means to define multiple methods with the same name but
different signatures. Overriding means to provide a new implementation for
a method in the subclass.
12
11.6 The Object Class and its toString() Method
It is important to be familiar with the methods provided
by the Object class so that you can use them in your
classes.
13
11.6 The Object Class and its toString() Method
As you know from chapter 8, the toString( ) method is used to
return a string representation of the object. If no toString( ) is
defined in a class then a call to toString( ) will actually call the
toString( ) of its superclass Object. The toString( ) of the class
Object returns a string consisting of a class name of which the
object is an instance, the at sign (@), and a number representing
this object. Assume that the class Loan does not contain a
toString( ) method, and assume we wrote the following two
statements:
Loan loan = new Loan();
System.out.println(loan.toString());
15
Class Circle with a toString() method:
public class Circle {
private double radius;
public Circle(double r) {
radius = r;
}
@Override
public String toString() { // Overrides the toString of Object
return "The radius is " + radius;
}
}
public class Application {
public static void main(String [] args) {
Circle c1 = new Circle(1.0);
Output
System.out.println(c1.toString());
The radius is 1.0
}
}
16