‘ final ‘ to avoid overriding                        http://improvejava.blogspot.in   1
Objective   :  On completion of this period, you would be able to  learn  • using final with inheritance  • using final to...
Recap In the previous class we have learnt• multi-level inheritance hierarchy                  http://improvejava.blogspot...
Final ClassFinal classes• Classes that cannot be extended• To declare final classes, we write,public final class ClassName...
final Methods • final Methods       • Methods that cannot be overridden       • To declare final methods, we write, public...
Example For final Method    public final String getName(){            return name;     }                           http://...
Uses of final KeywordThere are three uses of final keyword• It can be used to create the equivalent of named constant• To ...
Prevent method overriding• Method overriding is one of java’s most powerful features• There will be times when you want to...
Example program to prevent overridingclass A { final void meth() {System.out.println( “ This is a Final method.” );  }} cl...
Example Program To Prevent Overriding                                                      contd..• meth() is declared as ...
Prevent Inheritance• Sometimes you will want to prevent a class being inherited• To do this use the final keyword before t...
Example to prevent inheritance    final class A {       // ….    }    // The following class is illegal     class B extend...
Summary• In this class we have discussed • final keyword • Using final avoiding overriding • Using final avoiding inherita...
Frequently Asked Questions1. What is the use of a final key2. Explain with an example program using final to   prevent met...
Quiz1. To prevent overriding we will use  1. this  2. final  3. new  4. old             http://improvejava.blogspot.in   15
Quiz                        contd..2. Final is used to prevent the following   1. Multi level hierarchy   2. Abstraction  ...
Upcoming SlideShare
Loading in …5
×

‘ Final ‘ to avoid overriding

1,208 views

Published on

1 Comment
1 Like
Statistics
Notes
No Downloads
Views
Total views
1,208
On SlideShare
0
From Embeds
0
Number of Embeds
21
Actions
Shares
0
Downloads
16
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide

‘ Final ‘ to avoid overriding

  1. 1. ‘ final ‘ to avoid overriding http://improvejava.blogspot.in 1
  2. 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 2
  3. 3. Recap In the previous class we have learnt• multi-level inheritance hierarchy http://improvejava.blogspot.in 3
  4. 4. Final ClassFinal classes• Classes that cannot be extended• To declare final classes, we write,public final class ClassName{...} http://improvejava.blogspot.in 4
  5. 5. final Methods • final Methods • Methods that cannot be overridden • To declare final methods, we write, public final [return Type] [methodName] ([parameters]){ ... } • Static methods are automatically final http://improvejava.blogspot.in 5CM-602.40
  6. 6. Example For final Method public final String getName(){ return name; } http://improvejava.blogspot.in 6CM-602.40
  7. 7. Uses of final KeywordThere 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 7
  8. 8. Prevent method overriding• 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 http://improvejava.blogspot.in 8
  9. 9. Example program to prevent overridingclass 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! “); }} http://improvejava.blogspot.in 9
  10. 10. Example Program To Prevent Overriding contd..• 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 http://improvejava.blogspot.in 10
  11. 11. Prevent Inheritance• 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 http://improvejava.blogspot.in 11
  12. 12. Example to prevent inheritance final class A { // …. } // The following class is illegal class B extends A { //ERROR ! Can’t be subclass A // ….} http://improvejava.blogspot.in 12
  13. 13. Summary• In this class we have discussed • final keyword • Using final avoiding overriding • Using final avoiding inheritance http://improvejava.blogspot.in 13
  14. 14. Frequently Asked Questions1. What is the use of a final key2. Explain with an example program using final to prevent method overriding3. Explain with an example program using final to prevent inheritance http://improvejava.blogspot.in 14
  15. 15. Quiz1. To prevent overriding we will use 1. this 2. final 3. new 4. old http://improvejava.blogspot.in 15
  16. 16. Quiz contd..2. Final is used to prevent the following 1. Multi level hierarchy 2. Abstraction 3. Inheritance 4. Polymorphism http://improvejava.blogspot.in 16

×