abstract - Java key word
abstract Java classes and methods
1. Definition of 'abstract' as commonly used in English
2. Abstract Java classes and methods
2.1 Attributes of abstract classes and methods
2.2 Rules for abstract classes and methods
3. Java Examples: Creating and using an abstract class
3.1 Example from Pleac Project
3.2 Example from Learning Java (Niemeyer and Knudsen).
4. See also
1. Definition of 'abstract' as commonly used in English
In the English language, abstract can be used as a verb, adjective, or noun.
Abstract, by simple definition means 'to draw from or seperate'.
- To seperate ideas by the operation of the mind; to consider one part of a complex object, or to have a partial idea of it in the mind.
- To select or seperate the substance of a book or writing; to epitomize or reduce to a summary.
- Abstract terms are those which express abstract ideas, as beauty, roundness, without regarding any subject in which they exist; or abstract terms are the names of orders, genera, classes, or species of things in which there is a combination of similar qualities.
- When something is considered 'in the abstract', it is in a state of seperation, as a subject considered without reference to a particular person or thing.
2. Abstract Java classes and methods
An abstract Java class can really be considered a superclass. Classes and objects created in Java are an abstraction (a generalized representation of a complex object) of something in the real world. So an abstract class is really a superclass or super-abstraction. As we see from the english definition number four (4) above, an abstract is 'considered without reference to a particular person or thing'. So it follows that an abstract Java class cannot be instantiated. Therefore if you attempt to create an object from an abstract class -- you will get a compile time error. You must create subclasses of the superclass to create any 'real objects'.
Does this make sense? Lets consider an example:
Let's start with the abstract concept of 'roundness'. Then we'll look for examples (or instances) of roundness. Several examples (or instances) of roundness might be:
- A round rubber ball.
- A round wooden wheel.
- A round plastic coffee cup lid.
Do you see how the abstract superclass (roundness) has less definition then the subclass instances (examples) of roundness?
to be continued...
2.1 Attributes of abstract classes and methods
- An abstract class contains at least one abstract method.
- An abstract class is a class that is missing definitions for one or more of it's methods.
- An abstact method is a method without a body.
2.2 Rules for abstract classes and methods
- A class that contains one or more abstract methods, must be declared abstract.
- One cannot instantiate an abstract class
- Attempting to create a new object of an abstract class, will result in a compile-time error.
- A subclass that inherits from an abstract class, must implement all of the abstract methods in the superclass.
3. Java Examples: Creating and using an abstract class
Looking for a few good examples...
3.1 Example from Pleac Project
Here is a code fragment from the pleac project:
package pleac.ch01.part17;
import java.io.*;
import java.util.*;
public abstract class FixStyle { ... }
The complete source code listing is here. (Scroll down the page to FixStyle example).
3.2 Example from Learning Java (Niemeyer and Knudsen).
Here is another code fragment from Learning Java, by Patrick Niemeyer and Jonathan Knudsen:
package learningjava.protocolhandlers.crypt;
import java.io.*;
abstract class CryptInputStream extends InputStream {
InputStream in;
OutputStream out;
abstract public void set( InputStream in, OutputStream out );
} // end of class CryptInputStream
The complete source code listing is here.
4. See also
- Abstract class - Java glossary article by Roedy Green (mindprod.com)
- Interface vs. Abstract - article by Roedy Green (mindprod.com)
- Abstract java classes and methods - Java Tutorial (java.sun.com)
- Abstract classes vs. Interfaces - from JavaWorld magazine - article by Tony Sintes
- Java Reserved Words - Instant Expert Guide
Created with ixDoc - 19-Sep-2005/5:25:51
:: Jump to Index page - Instant Expert Guides
Copyright © 2005, IX Publishing, Inc. - All rights reserved.