16
http:// improvejava.blogspot.in 1 ‘ final ‘ to avoid overriding

‘ Final ‘ to avoid overriding

Embed Size (px)

Citation preview

Page 1: ‘ Final ‘  to avoid overriding

http://improvejava.blogspot.in 1

‘ final ‘ to avoid overriding

Page 2: ‘ 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

Page 3: ‘ Final ‘  to avoid overriding

http://improvejava.blogspot.in 3

Recap

In the previous class we have learnt• multi-level inheritance hierarchy

Page 4: ‘ Final ‘  to avoid overriding

http://improvejava.blogspot.in 4

Final Class

Final classes

• Classes that cannot be extended

• To declare final classes, we write,

public final class ClassName{

. . .

}

Page 5: ‘ Final ‘  to avoid overriding

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

Page 6: ‘ Final ‘  to avoid overriding

http://improvejava.blogspot.in 6

public final String getName(){

return name;

}

Example For final Method

CM-602.40

Page 7: ‘ Final ‘  to avoid overriding

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

Page 8: ‘ Final ‘  to avoid overriding

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

Page 9: ‘ Final ‘  to avoid 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

Page 10: ‘ Final ‘  to avoid 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..

Page 11: ‘ Final ‘  to avoid overriding

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

Page 12: ‘ Final ‘  to avoid overriding

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

Page 13: ‘ Final ‘  to avoid overriding

http://improvejava.blogspot.in 13

Summary

• In this class we have discussed• final keyword• Using final avoiding overriding• Using final avoiding inheritance

Page 14: ‘ Final ‘  to avoid overriding

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

Page 15: ‘ Final ‘  to avoid overriding

http://improvejava.blogspot.in 15

1. To prevent overriding we will use

1. this2. final3.new4.old

Quiz

Page 16: ‘ Final ‘  to avoid overriding

http://improvejava.blogspot.in 16

2. Final is used to prevent the following 1. Multi level hierarchy2. Abstraction3. Inheritance 4. Polymorphism

Quiz contd..