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

9 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