• Like
‘ Final ‘  to avoid overriding
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

‘ Final ‘ to avoid overriding

  • 466 views
Published

 

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
  • thanks
    Are you sure you want to
    Your message goes here
    Be the first to like this
No Downloads

Views

Total Views
466
On SlideShare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
5
Comments
1
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. ‘ final ‘ to avoid overriding http://improvejava.blogspot.in 1
  • 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. Recap In the previous class we have learnt• multi-level inheritance hierarchy http://improvejava.blogspot.in 3
  • 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. 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. Example For final Method public final String getName(){ return name; } http://improvejava.blogspot.in 6CM-602.40
  • 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. 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. 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. 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. 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. 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. Summary• In this class we have discussed • final keyword • Using final avoiding overriding • Using final avoiding inheritance http://improvejava.blogspot.in 13
  • 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. Quiz1. To prevent overriding we will use 1. this 2. final 3. new 4. old http://improvejava.blogspot.in 15
  • 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