Sunday, November 8, 2009

SCJP 1.6 Study Guide: Declarations and Access Controls Part 4

Final Class and Abstract Class

We'll pick it right off Part 3 of Declarations and Access Controls where we discussed how a regular class is declared in Java and the 2 access modifiers we can define for our classes. With this post, I hope you will learn more about final classes and abstract classes.

In addition to access modifiers you can still modify a class by using non-access modifiers like strictfp, final and abstract keywords. But remember that you can't always mix non-access modifiers. You can combine strictfp with either final or abstract but you cannot combine final and abstract as you will learn later.

Final Class. When you use the final keyword in a class declaration you define that class to be a final class which means it cannot be subclassed. Any attempt to extend a final class will result into a compiler error. Also, since you cannot subclass a final class, you won't be able to override its methods (Overriding is discussed in Section 2: Object Orientation).

So when do you a class final? If you look at the javadocs of some classes of the Java core libraries you will see that most are declared to be final which guarantees that those classes will not be modified by others so that they will behave the same on all applications that use them. Therefore, if you depend heavily on the implementations of a class and you want it to behave exactly the same every time it is used then most probably you want that class to be a final class.

Example:
package bobhub.animal;
public final class Animal{
public void eat(){}
}
package bobhub.animal.mammals;
public class Dog extends Animal{}

The Animal class will compile, but if you try compiling the Animal class you will get a compiler error. Try it to see the error. In the course of your development, you will rarely make a final class.

Abstract Class. When we use the word abstract as an adjective we usually mean to say it's something generic.
An abstract object is an object which does not exist at any particular time or place, but rather exists as a type of thing.-Answers.com
Abstract classes are classes that you cannot instantiate. Instead you extend them. And when you are brave enough to instantiate an abstract class the compiler will curse at you with a compiler error.

Example:
package bobhub.animal;
public abstract class Animal{
public String name;
public double height;

public abstract void eat();
public abstract void sleep();
}
package bobhub.animal;
public class Dog extends Animal{
public void eat(){
System.out.println("Eating...");
}
public void sleep(){}
}
package bobhub.test;
import bobhub.animal.Animal;
import bobhub.animal.Dog;
public class TestAnimal{
public static void main(String[] args){
Animal animal = new Animal();
Dog dog = new Dog();
}
}
The above classes will compile except for TestAnimal where we tried to instantiate the Animal class. If you delete that line the TestAnimal class will compile.

Let's take a closer look at the Animal class. If you notice the eat() method was declared with the abstract keyword and ends in a semi-colon instead of a pair of braces.
public abstract void eat();
The eat() method is considered an abstract method since it was declared with the abstract keyword. If you see an abstract method in a class then the class should be declared abstract as well or else you will get a compiler error. When a class extends an abstract class with abstract methods the extending class should implement those abstract methods where you will define how the method should work (See example above). Moreover, an abstract class can define non-abstract methods as well. We will discuss abstract methods more on later posts.

strictfp. The strictfp keyword is used to modify a class or a method and never a variable. Floating points in a Java application may behave differently in different platforms but declaring a class as strictfp will make all methods in the class conform to the IEEE 754 standard rules for floating points. If you don't define a class as strictfp you can make individual methods as strictfp.

Example:
strictfp class Calculator{}
class TaxAnalyzer{
public strictfp analyzeTax(){}
}
This post ends the topic on how we declare and modify Java classes. The next posts will talk about declaring interfaces. See you until then!

0 comments:

Post a Comment