130 Slides OOP Part 1 Inheritance Method Overloading Vs Overriding Recap
130 Slides OOP Part 1 Inheritance Method Overloading Vs Overriding Recap
Let's review the main differences between method overriding and 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.
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.
Methods will be considered overloaded if both methods follow the following rules:
• Methods must have the same method name.
• Methods must have different parameters.
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.
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.
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.
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.
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:
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.