12/24/2015
source: http://www.itrelease.com/2011/05/difference-between-static-final-and-abstract-class-in-java/
final, static and abstract:
final declares a variable to be constant. Which means once you set its value, it never changes and trying to change it gives an error. An example of using final is:
final int i = 0;
i is equal to 0, it will always be equal to 0 and can never be changed.
In addition to Jason's excellent post, here is some more information about final and static.
The keyword final is used to prevent a method from being overridden, or to prevent a class from being inherited. It can also be used to create variables whose values can't be changed, as Jason points out.
12/24/2015
Difference between static, final and abstract class in java
Let me discuss static, final and abstract class one by one.
Abstract class
An abstract class is that which must be extended. If you use abstract method in a class then that means the class is abstract also so you have to declare that class as abstract. Abstract class behaves as a template. Abstract class can contain static data. Abstract class can not be instantiated.
Static class
Static class is that whose methods and variables can be called without creating the instance of the class. Static means which is initialized once for example our main method is initialized only once.
We cannot override the static method in child class by non static. So the child class should be static as well.
Final class
Final class is that which cannot be extended and the final variable is constant that means it cannot be changed.
Try final keyword on variable, method and class. Define functionality when applied on in each these.
ReplyDelete
Delete- final ; -- Means variable cannot accept change in value.
- final ; --- Means method cannot be overriden(no overriding)
- final ; ---- Means class cannot be extended