Monday, February 26, 2007

Final and Abstract Class in Java

Final Class :

When a class is declared final,it cannot be extended or subclassed.If any attempt is made to do so will result in compilation error.

Why final class:

If you need an absolute guarantee that none of the methods in that class will ever be overridden.But declaring a class final violates object-oriented (OO) notion of inheritance.

Abstract Class :

An abstract class can never be instantiated.Its main mission is to be subclassesd.If even a single method is abstract, the whole class must be declared abstract.However you can have nonabstract methods in abstract class.Declaring a class abstract supports the concept of polymorphism.You cannot create an object out of abstract classes,which will result in
compilation error.

Why Abstract class:

It helps the concept of polymorphism.Just declare all the generic methods(behaviour) and variable(state) for a particular group in a abstract class and subclass it , so that those methods can be overridden by the class that extends abstract class.

You can't mark a class as both abstract and final.

1 comment:

Sudharsan said...

Nice information
It helps me a lot ThanX dude