Dude, Where s My Implementation?

     

Dude, Where's My Implementation?

An abstract class is a class whose declaration includes the keyword abstract . Based on that definition, you should be able to write the simplest possible abstract class. Okay geez I'll do it. How about this:

 

 public abstract class SimplestAbstractClass { } 

That meets the definition, and so it compiles.

You might never write an empty class such as this. But there are occasions when you will come across such empty classes; such usage aims to provide a formal organization to an application for later possibilities for extensibility.

The following is also an abstract class:

 

 public abstract SecondAbstractClass {    public abstract void someMethod(); } 

The preceding code declares an abstract class that includes an abstract method. The class should be subclassed, and when it is, an implementation for someMethod() must be provided. Notice that abstract methods don't have any curly braces (which is where the implementation would go). They just end in a semi-colon. If you end a method declaration in a semi- colon , you must also declare it abstract.

The following is also an abstract class:

 

 public abstract class ThirdAbstractClass {     int counter = 0;     void printMessage(String msg) {          System.out.println("Your message: " + msg);     } } 

Notice how we have what looks to be a totally regular class. It has a member variable that is initialized to a value, and it has an implemented method. But it's declared abstract, and it compiles. What gives?

Abstract classes can consist of implementations , such as in the preceding ThirdAbstractClass, or they can defer their implementation entirely, or they can have a combination of stuff they implement and stuff they defer to let someone else implement.



Java Garage
Java Garage
ISBN: 0321246233
EAN: 2147483647
Year: 2006
Pages: 228
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net