[go: up one dir, main page]

0% found this document useful (0 votes)
7 views14 pages

Inheritance 2

Uploaded by

divinekaisa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views14 pages

Inheritance 2

Uploaded by

divinekaisa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Inhritance2

AGENDA

01 Review 02 Override

03 Final
01
Override
Overriding : -
Overriding is a feature that allows a subclass or child class to provide a specific
implementation of a method that is already provided by one of its super-classes or
parent classes.

When a method in a subclass has the same name, same parameters or signature, and
same return type(or sub-type) as a method in its super-class, then the method in the
subclass is said to override the method in the super-class.
Acheiving method overriding:

1. same name of super class method.


2. same return type.
3. same parameter list.
4. different in implementation(method body).
5. can be different in access modefire.
• Example
• Rules for method overriding:

1. Overriding and Access-Modifiers : The access modifier for an overriding


method can allow more, but not less, access than the overridden method. For
example, a protected instance method in the super-class can be made public,
but not private, in the subclass.
2. Final methods can not be overridden : If we don’t want a method to be
overridden, we declare it as final.
3. Private methods can not be overridden : Private methods cannot be
overridden as they are bonded during compile time. Therefore we can’t even
override private methods in a subclass.
4. Invoking overridden method from sub-class : We can call parent class
method in overriding method using super keyword.
• Example
5. Static methods can not be overridden(Method Overriding vs Method Hiding) :
When you define a static method with same signature as a static method in base
class, it is known as method hiding.
6. Final methods can not be overridden : If we don’t want a method to be
overridden, we declare it as final.
02
Final Keyword
Final Keyword In Java

The keyword in java is used to restrict the user.

The java final keyword can be used in many context. Final can be:
1. variable
2. method
3. class
1) Java final variable :
If you make any variable as final, you cannot change
the value of final variable(It will be constant).
2) Java final method :
If you make any method as final, you cannot override it.

3) Java final class :


If you make any class as final, you
cannot extend it.
THANKS
Any questions?

You might also like