Core Java - [final/abstract]

♠ Posted by Unknown in at 23:47

final/abstract


Java’s final keyword has slightly different meanings depending on the context, but in general it says “This cannot be changed.” You might want to prevent changes for two reasons: design or efficiency. Because these two reasons are quite different, it’s possible to misuse the final keyword.

There are three places, where final can be used: for data, methods, and classes.

1.      Final Data :

Many programming languages have a way to tell the compiler that a piece of data is “constant”. A constant is useful for two reasons:
I.                    It can be compile-time constant that won’t ever change.
II.                  It can be a value initialized at run-time that you don’t want changed.

In Java, these sorts of constants must be primitives and are expressed using the final keyword. A value must be given at the time of definition of such a constant.

A field that is both static and final has only one piece of storage that cannot be changed.

2.      Final Methods :

There are two reasons for final methods. The first is to put a “lock” on the method to prevent any inheriting class from changing its meaning. This is done for design reasons when  you want to make sure that a method’s behavior is retained inheritance and cannot be overridden.

The second reason for final methods is efficiency. If you make a method final, you are allowing the compiler to turn any calls to that method into inline calls. When the compiler to turn any calls to that method into inline calls. When the compiler sees a final method call it can skip the normal approach of inserting code to perform the method call mechanism and instead replace the method call with a copy of the actual code in the method body.

Any private method in a class are implicitly final. Because you can’t access a private method, you can’t override it.

3.      Final Classes :

When you say that an entire class is final (by preceding its definition with the final keyword), you state that you don’t want to inherit form this class or allow any one else to do so. Defining the class as final simply prevents inheritance nothing more. However, because it prevents inheritance all methods in a final class are implicitly final, since there’s no way to override them. So the compiler has the same efficiency options as it does if you explicitly declare a method final.


abstract Methods and Classes:

Java provides a mechanism for doing this called the abstract method. This is a method that is incomplete, it has only a declaration and no method body. Here is the syntax for an abstract methods declaration:

            abstract void f();
A class containing abstract methods is called an abstract class. If a class contains one or more abstract methods, the class must be qualified as abstract.

If an abstract class is incomplete, what is the compiler supposed to do when someone tries to make an object of that class? It cannot safely create an object of an abstract class, so you get an error message from the compiler. This way the compiler ensures the purity of the abstract class, and you don’t need to worry about misusing it.

If you inherit from an abstract class and you want to make objects of the new type, you must provide method definitions for all the abstract methods in the base class. If you don’t, then the derived class is also abstract and the compiler will force you to qualify that class with the abstract keyword.

It’s possible to create a class as abstract without including any abstract methods. This is useful when you’ve got a class in which it doesn’t make sense to have any abstract methods, and yet you want to prevent any instances of that class.

0 comments:

Post a Comment