0% found this document useful (0 votes)
23 views

130 Slides OOP Part 1 Inheritance Method Overloading Vs Overriding Recap

Uploaded by

Phú Nguyễn
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

130 Slides OOP Part 1 Inheritance Method Overloading Vs Overriding Recap

Uploaded by

Phú Nguyễn
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Method Overriding vs Overloading

Let's review the main differences between method overriding and method
overloading.

COMPLETE JAVA MASTERCLASS


Method Overloading vs Overriding Recap
Method Overloading

Method overloading means providing two or more separate methods, in a class, with the
same name, but different parameters.
Method return type may or may not be different, and that allows us to reuse the same
method name.
Overloading is very handy, it reduces duplicated code, and we don't have to remember
multiple method names.
We can overload static, or instance methods.

COMPLETE JAVA MASTERCLASS


Method Overloading vs Overriding Recap
Method Overloading

To the code calling an overloaded method, it looks like a single method can be called, with
different sets of arguments.
In actuality, each call that's made with a different set of arguments, is calling a separate
method.
Java developers often refer to method overloading, as compile-time polymorphism.
This means the compiler is determining the right method to call, based on the method
name and argument list.

COMPLETE JAVA MASTERCLASS


Method Overloading vs Overriding Recap
Method Overloading

Usually overloading happens within a single class.


But methods can also be overloaded by subclasses.
That's because, a subclass inherits one version of the method from the parent class, and
then the subclass can have another overloaded version of that method.

COMPLETE JAVA MASTERCLASS


Method Overloading vs Overriding Recap
Method Overloading Rules

Methods will be considered overloaded if both methods follow the following rules:
• Methods must have the same method name.
• Methods must have different parameters.

If methods follow the rules above:


• They may or may not have different return types.
• They may or may not have different access modifiers.
• They may or may not throw different checked or unchecked exceptions.

COMPLETE JAVA MASTERCLASS


Method Overloading vs Overriding Recap
Method Overriding

Method overriding, means defining a method in a child class, that already exists in the
parent class, with the same signature (the same name, same arguments).
By extending the parent class, the child class gets all the methods defined in the parent
class (those methods are also known as derived methods).
Method overriding is also known as Runtime Polymorphism, or Dynamic Method
Dispatch, because the method that is going to be called, is decided at runtime, by the Java
virtual machine.

COMPLETE JAVA MASTERCLASS


Method Overloading vs Overriding Recap
Method Overriding

When we override a method, it's recommended to put @Override, immediately above the
method definition.
The @Override statement is not required, but it's a way to get the compiler to flag an error,
if you don't actually properly override this method.
We'll get an error, if we don't follow the overriding rules correctly.
We can't override static methods, only instance methods can be overridden.

COMPLETE JAVA MASTERCLASS


Method Overloading vs Overriding Recap
Method Overriding Rules

A method will be considered overridden, if we follow these rules.


• It must have the same name and same arguments.
• The return type can be a subclass of the return type in the parent class.
• It can't have a lower access modifier. In other words, it can't have more restrictive access
privileges.
• For example, if the parent's method is protected, then using private in the child's
overridden method is not allowed. However, using public for the child's method would
be allowed, in this example.

COMPLETE JAVA MASTERCLASS


Method Overloading vs Overriding Recap
Method Overriding Rules

There's also some important points about method overriding to keep in mind.
• Only inherited methods can be overridden, in other words, methods can be
overridden only in child classes.
• Constructors and private methods cannot be overridden.
• Methods that are final cannot be overridden.
• A subclass can use super.methodName() to call the superclass version of an
overridden method.

COMPLETE JAVA MASTERCLASS


Method Overloading vs Overriding Recap
Method Overriding vs. Overloading
OVERRIDING same name OVERLOADING same name
same parameters different parameters

COMPLETE JAVA MASTERCLASS


Method Overloading vs Overriding Recap
Method Overriding vs. Overloading

Method Overloading Method Overriding


Provides functionality to reuse a method Used to override a behavior which the class
name with different parameters. has inherited from the parent class.
Usually in a single class but may also be used Always in two classes that have a child-
in a child class. parent or IS-A relationship.
Must have different parameters. Must have the same parameters and same
name.
May have different return types. Must have the same return type or covariant
return type(child class).
May have different access modifiers(private, Must NOT have a lower modifier but may
protected, public). have a higher modifier.
May throw different exceptions. Must NOT throw a new or broader checked
exception.

COMPLETE JAVA MASTERCLASS


Method Overloading vs Overriding Recap
Covariant Return Type

The return type of an overridden method can be the same type as the parent method's
declaration.
But it can also be a subclass.
The term, covariant return type, is more appropriate.

COMPLETE JAVA MASTERCLASS


Method Overloading vs Overriding Recap
Covariant Return Type

We briefly mentioned, in a previous video, that there's a clone method on the class Object,
that all classes inherit from.
A simplified look at this declaration, for our purposes, is shown below.

And if you overrode this method, by using IntelliJ's code generation tools, it would generate
this code in your class:

COMPLETE JAVA MASTERCLASS


Method Overloading vs Overriding Recap
Covariant Return Type

But in general, when you're cloning an instance, you're going to want to return an Object,
that's the same type as the Object you are cloning.
Remember, we said all classes ultimately have Object as a base class, so every class can be
said to be a covariant of Object.

COMPLETE JAVA MASTERCLASS


Method Overloading vs Overriding Recap
Covariant Return Type
The clone method overridden in a Person class.
The clone method (generated by IntelliJ) override
(from Object)
This is a valid override of Object's clone method.

COMPLETE JAVA MASTERCLASS


Method Overloading vs Overriding Recap

You might also like