This video is about method overriding
versus overloading, specifically let's
talk about the main differences between
method overriding and method overloading.
So method overloading it means providing
two or more separate methods in a class
with the same name but with different
parameters now the yeah method return
type may or may not be different and
that allows us to reuse the same method
name. Overloading is very handy because
it reduces duplicated code and we don't
have to remember multiple method names.
Now overloading doesn't have anything to
do with polymorphism but Java developers
often refer to overloading as
compile-time polymorphism, so in other
words the compiler decided which method
is going to be called based on the
method name return type an argument list.
Now we can override static in instant
methods and more about the differences
between static and instant methods later
in the course.
Now usually overloading happens inside a
single class, but a method can also be
treated as overloaded in the subclass of
that class and 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, there's some rules about
method overloading methods will be
considered overloaded if both follow the
following rules the methods must have
the same method name and methods must
have different parameters.
Now if methods follow the rules above
then they may or may not have different
return types that may or may not have
different access modifiers and then may
or may not throw different checked or
unchecked exceptions and more on
exceptions than what they are on how to
use them later in the course, so that was
method overloading let's discuss a
little bit more now about method
overriding, so a method overriding well
that means to finding a method in the
child car or in a child class that
already exists in the parent class with
the same signature in other words the
same name and same arguments, so by
extending the parent class the child
class gets all the methods defined in
the parent class and those methods are
know on has derived methods.
Method overriding is also known as
runtime polymorphism and 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 at override immediately above the
method definition and an override is an
annotation and the compiler will read
and will read an annotation and in this
case the at override tells the compiler
to show us an error if we aren't
following the overriding rules correctly
for that method, then we can override
static methods only instance methods can
be overridden
so some rules about method overriding a
method will be considered overridden if
we follow these rules firstly 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 so for
example if the parent method is
protected than using private in the
child is not allowed,
however using public in the child would
be allowed in this example now there's
also some important points about method
overriding to keep in mind firstly only
inherited methods can be overridden in
other words methods cannot be overridden
only in child classes constructors and
other private methods cannot be
overridden and methods that are final
also cannot be overridden now a subclass
can use super dot method name
parenthesis to call the subclass version
of an overridden method. So let's see an
example now of method overloading and
method overriding so on the Left we've
got a simple overriding example, as you
can see we've got two classes dog and
German Shepherd, now the class German
shepherd extends dog and overrides
method bark, so as you can see with
overriding we've got the same name and
same parameters for the method in this
case specifically we don't have any
parameters but they would need to be the
same in the case of overriding on the
right-hand side you can see simple
example with method overloading, so here
we've got the class dog with two methods
that have the nice same name bark but in
this case they've got different
parameters, the second method bark has an
int parameter
while the firsts our method for bark
doesn't have any parameters, so hopefully
now you can easily see the difference
between overriding and overloading when
comparing these two examples side by
side, so let's recap now the most
important rules about overloading and
overriding methods, so in the following
table you can see the main differences
between overloading and overriding now
some parts like exceptions are probably
still unknown you may be wondering what
exceptions are you may not know those at
this point but we'll cover exceptions
later in the course, but on the left hand
side we've got overloading and on the
right hand side we've got method
overriding. Now with method overloading
we've got functionality to reuse the
method name with different parameters
overloading usually happens in a single
class however it can be used in a child
class because the child class inherits
methods from the parent class now with
overloading methods they must have
different parameters but they might have
different return types access modifiers
and/or different exceptions you know
with overriding we're reusing behavior
which the class has inherited from the
parent class, so overriding can be in the
child class or grandchild class it's
always in two classes that have there is
a relationship so methods that are
overridden must have the same parameters
and they must have the same return type
or covariant return type now covariant
return type is just a return type that
is in the parent-child relationship so
in other words that has a is a
relationship overridden methods I can't
have a lower access modifier, so in other
words if the parent method is protected
then the child method can't be private
but it could have a higher modifier in
other words in a scenario the parent
method being protected the child method
could be public. Now covariant type you
might be a bit confused by that term so
let's see a quick example of a covariant
return type with method overriding so on
the left hand side we've got two classes
burger and healthy burger
the fuels and methods have just been
admitted for simplicity so that create
any additional confusion and on the
right hand side we can see the classes
burger factory and it's got one method
create burger that method just returns a
new burger instance there's also a
healthy burger factory class that
extends the burger factory and overrides
the create burger method so notice here
how the return types are different and
that's known as a covariant return type
so in other words the burger class is
the parent class and healthy burger is a
child class, so the method create burger
in the class healthy burger factory can
return any child type of burger in this
case we've just got one child class
healthy burger. Alright, so hopefully we've
now aware that they'll make sense about
covariant return types and that's giving
you a good recap of a method overloading
vs. method overriding. I'll see you in
the next video.