Monday, November 11, 2013

final class, variable, and method

Java does allow you to extend a class, change the value of a variable, and override a method of a class. Sometimes, however, you can modify this behavior to disable extendibility, prevent value of the variable from being changed, and prevent the method from being overridden by using the final keyword.

When the final keyword is written before the name of a class, the class will become a final class. You cannot create a sub-class to extend the final class. You will get a compile error if you try to extend the final class. By placing the final keyword before the name of a variable, the variable will be a constant variable. You can assign a value to the constant variable only one time when it is defined. Further change to the value of the variable will also generate compile error. When the final keyword stays before the name of a method, the method will become a final method. The final method does not allow any code to override it.

In the example code below, the Reader class is a final class. The value of the path variable cannot be further changed by any code in the class. The readText method can be called from other classes. However, it cannot be overridden by any code outside the class.

import java.io.BufferedReader;
import java.io.FileReader;

final class Reader{ //final class
private final String path="d:/test.txt"; //constant variable

public final void readText(){ //final method
try {
FileReader fr=new FileReader(path);
BufferedReader br=new BufferedReader(fr);
String content="";
while((content=br.readLine())!=null){
System.out.println(content);
}
br.close();

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}


}

If you try to extend the Reader by creating another class and use the extends keyword, the compile error will display as shown in the picture below.

final keyword

27 comments:

  1. If you have enough knowelege to start job searching you can call for our linkedin resume writing service. We write resume. make LinkedIn profile to increase your chance to get really good job.

    ReplyDelete
  2. Very good, I think I found the knowledge I needed. I will see and refer some information in your post. thank you

    ReplyDelete
  3. Thanks for sharing such a good article and for informative things. Your articles are really amazing and I got a lot of information and guidance reading them.

    ReplyDelete
  4. I’ve been exploring for a bit for any high-quality articles or post posts on this sort of area . Exploring in Yahoo I at last stumbled upon this website. Reading this information So i am happy to convey that I've an incredibly good uncanny feeling I discovered just what I needed. I most certainly will make certain to don’t forget this website and give it a glance on a constant basis. google com sg

    ReplyDelete
  5. You made some decent points there. I looked on the internet for the subject matter and found most people will agree with your site.cool website designs

    ReplyDelete
  6. Thank you for the good writeup. It in fact was a amusement account it. Look advanced to more added agreeable from you! By the way, how could we communicate? Parc clematis launch date

    ReplyDelete
  7. It is the best time to make some plans for the future and it's time to be happy. I have learn this put up and if I may just I want to suggest you few fascinating issues or tips. Perhaps you can write next articles regarding this article. I wish to read even more things about it! concrete pump pipe

    ReplyDelete
  8. I do not even know how I ended up here, but I thought this post was good. I do not know who you are but definitely you're going to a famous blogger if you are not already ;) Cheers! dental filling cost

    ReplyDelete
  9. Hello there, You've done a great job. I will certainly digg it and personally suggest to my friends. I'm sure they'll be benefited from this website. tile regrouting cost

    ReplyDelete
  10. I’ve been exploring for a bit for any high-quality articles or blog posts on this sort of area . Exploring in Yahoo I at last stumbled upon this web site. Reading this info So i’m happy to convey that I have an incredibly good uncanny feeling I discovered just what I needed. I most certainly will make sure to do not forget this site and give it a glance on a constant basis. market website

    ReplyDelete
  11. Its like you read my mind! You seem to know so much about this, like you wrote the book in it or something. I think that you can do with a few pics to drive the message home a bit, but other than that, this is fantastic blog. A great read. I'll certainly be back.website design agency singapore

    ReplyDelete
  12. Thank you for the good writeup. It in fact was a amusement account it. Look advanced to far added agreeable from you! However, how can we communicate? website layout Singapore

    ReplyDelete
  13. Thank you for the auspicious writeup. It in fact was a amusement account it. Look advanced to far added agreeable from you! However, how can we communicate? engagement ring in Singapore

    ReplyDelete
  14. Good post. I study one thing tougher on completely different blogs everyday. It's going to all the time be stimulating to learn content from other writers and follow slightly something from their store. I’d want to make use of some with the content material on my weblog whether or not you don’t mind. Natually I’ll give you a link in your web blog. Thanks for sharing. aesthetic clinic in singapore

    ReplyDelete
  15. Very nice post. I just stumbled upon your blog and wished to say that I've really enjoyed surfing around your blog posts. After all I’ll be subscribing to your rss feed and I hope you write again very soon!asx stock

    ReplyDelete
  16. I appreciate, cause I found exactly what I was looking for. You've ended my 4 day long hunt! God Bless you man. Have a nice day. Bye fence staining chesterfield

    ReplyDelete
  17. I and also my guys have already been examining the great pointers on your website and at once I got a terrible feeling I had not thanked you for those tips. All of the ladies are actually for that reason very interested to see them and have in effect sincerely been making the most of these things. movers geneva il

    ReplyDelete
  18. This is the appropriate weblog for anyone who needs to search out out about this topic. You understand so much its almost arduous to argue with you (not that I actually would want…HaHa). You definitely put a new spin on a topic thats been written about for years. Nice stuff, just great!hgh for sale online

    ReplyDelete
  19. I think that is among the most significant info for me. And i'm glad studying your article. But want to statement on some general things, The web site style is wonderful, the articles is in point of fact great : D. Just right task, cheersAsbestos Encapsulation

    ReplyDelete
  20. I loved as much as you'll receive carried out right here. The sketch is tasteful, your authored subject matter stylish. nonetheless, you command get got an impatience over that you wish be delivering the following. unwell unquestionably come more formerly again since exactly the same nearly a lot often inside case you shield this hike.
    pest control brooklyn ny

    ReplyDelete
  21. Hi, i think that i saw you visited my web site so i came to “return the favor”.I'm trying to find things to enhance my web site!I suppose its ok to use a few of your ideas!! web designer

    ReplyDelete
  22. Hmm is anyone else experiencing problems with the pictures on this blog loading? I'm trying to find out if its a problem on my end or if it's the blog. Any suggestions would be greatly appreciated. online business ideas in singapore

    ReplyDelete
  23. Wow! This could be one particular of the most useful posts We've ever arrive across on this subject. Basically Excellent. I am also a specialist in this topic therefore I can understand your effort. cheapest private jet

    ReplyDelete
  24. Great write-up, I am regular visitor of one’s site, maintain up the nice operate, and It's going to be a regular visitor for a lengthy time.wayanad hotels and resorts

    ReplyDelete
  25. Thank you for the auspicious writeup. It in fact was a amusement account it. Look advanced to far added agreeable from you! By the way, how can we communicate? SEO for lead generation

    ReplyDelete
  26. Hey There. I found your blog using msn. This is an extremely well written article. I’ll make sure to bookmark it and return to read more of your useful info. Thanks for the post. I’ll definitely comeback.
    seo company singapore

    ReplyDelete
  27. Pretty nice post. I just stumbled upon your blog and wished to say that I've really enjoyed browsing your blog posts. After all I will be subscribing to your rss feed and I hope you write again very soon!
    singapore web design company

    ReplyDelete