Upload
myrajendra
View
1.173
Download
5
Embed Size (px)
Citation preview
http://improvejava.blogspot.in 1
‘ final ‘ to avoid overriding
http://improvejava.blogspot.in 2
Objective :On completion of this period, you would be able to learn
• using final with inheritance
• using final to prevent overriding
http://improvejava.blogspot.in 3
Recap
In the previous class we have learnt• multi-level inheritance hierarchy
http://improvejava.blogspot.in 4
Final Class
Final classes
• Classes that cannot be extended
• To declare final classes, we write,
public final class ClassName{
. . .
}
http://improvejava.blogspot.in 5
• final Methods• Methods that cannot be overridden
• To declare final methods, we write,
public final [return Type] [methodName]([parameters]){
. . .
}
• Static methods are automatically final
final Methods
CM-602.40
http://improvejava.blogspot.in 6
public final String getName(){
return name;
}
Example For final Method
CM-602.40
http://improvejava.blogspot.in 7
Uses of final Keyword
There are three uses of final keyword
• It can be used to create the equivalent of named constant
• To prevent overriding
• To prevent Inheritance
http://improvejava.blogspot.in 8
• Method overriding is one of java’s most powerful features
• There will be times when you want to prevent it from
occurring
• To disallow a method from being overridden use final
modifier at the start of the declaration
• Methods declares as final cannot be overridden
Prevent method overriding
http://improvejava.blogspot.in 9
class A { final void meth() {System.out.println( “ This is a Final method.” ); }} class B extends A { void meth() { // ERROR! Can’t overrideSystem.out.println( “ Illegal! “); }}
Example program to prevent overriding
http://improvejava.blogspot.in 10
• meth() is declared as final hence it cannot be overridden in B
• If you attempt to do so a compile time error will result • Methods declares as final sometimes provide
performance enhancement• Normally java resolves calls to methods dynamically at
run-time• This is call late binding• Since final methods cannot be overridden , a call to one
can be resolved at compile time• This is called early binding
Example Program To Prevent Overriding contd..
http://improvejava.blogspot.in 11
• Sometimes you will want to prevent a class being inherited
• To do this use the final keyword before the class declaration
• Declaring a class as final implicitly declares all of its methods as final, too
Prevent Inheritance
http://improvejava.blogspot.in 12
final class A { // ….
} // The following class is illegal
class B extends A { //ERROR ! Can’t be subclass A
// ….}
Example to prevent inheritance
http://improvejava.blogspot.in 13
Summary
• In this class we have discussed• final keyword• Using final avoiding overriding• Using final avoiding inheritance
http://improvejava.blogspot.in 14
Frequently Asked Questions
1. What is the use of a final key
2. Explain with an example program using final to prevent method overriding
3. Explain with an example program using final to prevent inheritance
http://improvejava.blogspot.in 15
1. To prevent overriding we will use
1. this2. final3.new4.old
Quiz
http://improvejava.blogspot.in 16
2. Final is used to prevent the following 1. Multi level hierarchy2. Abstraction3. Inheritance 4. Polymorphism
Quiz contd..