27-03-2012, 11:22 AM
Restricting Inheritance
Final Members: A way for Preventing Overriding of Members in Subclasses
All methods and variables can be overridden by default in subclasses.
This can be prevented by declaring them as final using the keyword “final” as a modifier. For example:
final int marks = 100;
final void display();
This ensures that functionality defined in this method cannot be altered any. Similarly, the value of a final variable cannot be altered.
Final Classes: A way for Preventing Classes being extended
We can prevent an inheritance of classes by other classes by declaring them as final classes.
This is achieved in Java by using the keyword final as follows:
final class Marks
{ // members
}
final class Student extends Person
{ // members
}
Any attempt to inherit these classes will cause an error.
Abstract Classes
When we define a class to be “final”, it cannot be extended. In certain situation, we want to properties of classes to be always extended and used. Such classes are called Abstract Classes.
An Abstract class is a conceptual class.
An Abstract class cannot be instantiated – objects cannot be created.
Abstract classes provides a common root for a group of classes, nicely tied together in a package:
Abstract Class Syntax
abstract class ClassName
{
...
…
abstract Type MethodName1();
…
…
Type Method2()
{
// method body
}
}
When a class contains one or more abstract methods, it should be declared as abstract class.
The abstract methods of an abstract class must be defined in its subclass.
We cannot declare abstract constructors or abstract static methods.
The Shape Abstract Class
public abstract class Shape {
public abstract double area();
public void move() { // non-abstract method
// implementation
}
}
Is the following statement valid?
Shape s = new Shape();
No. It is illegal because the Shape class is an abstract class, which cannot be instantiated to create its objects.
Abstract Classes
public Circle extends Shape {
protected double r;
protected static final double PI =3.1415926535;
public Circle() { r = 1.0; )
public double area() { return PI * r * r; }
…
}
public Rectangle extends Shape {
protected double w, h;
public Rectangle() { w = 0.0; h=0.0; }
public double area() { return w * h; }
}
Abstract Classes Properties
A class with one or more abstract methods is automatically abstract and it cannot be instantiated.
A class declared abstract, even with no abstract methods can not be instantiated.
A subclass of an abstract class can be instantiated if it overrides all abstract methods by implementation them.
A subclass that does not implement all of the superclass abstract methods is itself abstract; and it cannot be instantiated.
Summary
If you do not want (properties of) your class to be extended or inherited by other classes, define it as a final class.
Java supports this is through the keyword “final”.
This is applied to classes.
You can also apply the final to only methods if you do not want anyone to override them.
If you want your class (properties/methods) to be extended by all those who want to use, then define it as an abstract class or define one or more of its methods as abstract methods.
Java supports this is through the keyword “abstract”.
This is applied to methods only.
Subclasses should implement abstract methods; otherwise, they cannot be instantiated.