You are on page 1of 3

CSCI 1302: Computer Science II Fall 2018

Overriding Methods
Overloading vs. Overriding
In method overloading, we wrote variations of a method, passing different arguments to each. For example:

1 public static double computeAvg(int x, int y){
2 return (x + y) / 2.0;
3 }
4
5 public static double computeAvg(int x, int y, int z){
6 return (x + y + z) / 3.0;
7 }
8
9 public static double computeAvg(double x, double y){
10 return (x + y) / 2.0;
11 }


These three methods have the same name, computeAvg, but the number of the parameters is different (the first
has 2, the second has 3) or the data type of the parameters is different (the first and second have int parameters,
while the third has double).

In inheritance, we may need a variation of a superclass in our subclasses to make it more specialized. For
example, if we had the following Pet class:

1 public class Pet{
2 public void speak(){
3 System.out.println("Hello.");
4 }
5 public void exercise(){
6 System.out.println("Doing some activity");
7 }
8 }


and we had two subclasses, Dog and Fish, they would exercise differently, right? It wouldn’t make sense,
though, to make methods in each class with different names, it’s still exercise, it’s just implemented differently by
the different subclass objects, so we should just override it.

An overridden method, unlike an overloaded one, MUST have the same name AND the same number and or
type of parameters. Consider the Dog and Fish classes below.

-1-

1 public class Dog extends Pet{
2 public void exercise(){
3 System.out.println("My owner is taking me for a walk!");
4 }
5 }



1 public class Fish extends Pet{
2 public void exercise(){
3 System.out.println("Swimming around in my bowl!");
4 }
5 }


Notice that both methods are named exercise and that neither has a parameter, just like the one in the Pet
superclass, thereby overriding the method in the superclass. NOte that method overriding can only occur during
inheritance.

If a subclass overrides a superclass method, when doing that method call, the subclass version is used, otherwise,
the superclass’s version is used. Here’s an example.

1 public class OverridingDemo{
2 public static void main(String[] args){
3 Dog d = new Dog();
4 Fish f = new Fish();
5 d.speak();
6 d.exercise();
7 f.speak();
8 f.exercise();
9 }
10 }


Here’s the output:

Hello
My owner is taking me for a walk!
Hello
Swimming around in my bowl!

Because neither Dog nor Fish have a speak method, the one from the Pet class is executed (lines 5 and 7),
but because Fish and Dog have overridden the method, their versions of the exercise method are executed
rather than the one written in Pet. This means that the JVM checks the subclass first to see if it implemented a
version of the method, if not, the one in the superclass is executed.

-2-
The Object Class
Every class in Java without an extends clause, automatically inherits from the Object class. That means that
it is, directly or indirectly, the superclass of every class in Java. So what does that mean exactly?

The Object class has some general methods (some that you’ve seen before!) that, because every class inherits
from Object, every class we write has access to, to use, or to override:

toString(): returns a string representation of the object. returns a String


equals(Object obj): indicates whether some other object is equal to this one. returns a boolean
getClass(): returns the runtime class of this Object

Remember the toString method that we’ve been writing for our classes so that when they’re passed as an
argument to print statements, the object can be displayed as a string rather than what appears to be gibberish?

What we’ve actually been doing is overriding the toString method from the Object class, so now instead of
printing out the name of the class an @ symbol and a hash code like it says to do in the Object class, we’re
equipping our classes with their own version so that the string representation is more meaningful to the class.

We’ve also been “overriding” the equals method, but we’ve actually not been doing it properly.

First of all, remember that overriding means that the name of the method and the data type and number of its
parameters must be the same. The parameter for our equals methods have always been the same as the class
where we wrote the method, but according to the equals method in the Object class, the parameter should
be of type Object. That is:

-3-

You might also like