Introduction to Computer Science - Java

Creating User Defined Classes – part three

Abstract Classes.

        Sometimes we may wish to create an inheritance hierarchy in which the superclass is never instantiated. What does that mean? In other words, we may make a parent class and son classes. Then we may make objects of type son but not of type father. Why would we want to do this you may ask. Well, perhaps we have a parent class SpaceObject and son classes Meteor, Star, Planet and SpaceShip. If we are modeling the real world we must admit there are not space objects, there are only particular types of space objects, like stars and planets. So we will never make an instance od SpaceObject; we will always use the son classes. In such a case the father class is said to be abstract. This is because SpaceObjects are not real things, they are the imaginary constructs of our minds. They are virtual. They are abstract.

        Simply put, an abstract class is one in which some of its methods are not defined and are labelled as abstract. The class will simply name the methods, give return values and parameters, but not actually define what the methods do. Such undefined methods are called abstract methods. Any class which contains one or more abstract methods is an abstract class. Although you are able to make a reference to the abstract class, you cannot instatiate it with a new command. Nevertheless, you should make a constuctor method for the abstract class. The abstract class must be a parent class or superclass. Moreover there must be a son class which extends the superclass and defines the behaviour of the abstract methods. Theoretically, the son class may leave one or more abstract methods undefined, but then the son class is also abstract and must be declared as abstract and must have a son class extend it.
Here is how to define an abstract method in an abstract class:

public abstract void myMethod( parameters );
Notice that there is no implementation of the method. There is simply a semi-colon ; to indicate the end of the method. Here is how to define an abstract class:

public abstract class  myClass{
abstracts methods


        An abstract class that has no implemented methods at all is called an interface or pure abstract class. Strictly speaking interfaces are not classes. They simply define how other classes must be arranged. Therefore interfaces cannot have constuctors. When defining such an interface use the keyword interface. In such a class all methods are declared to be public abstract. ( Note: making them private abstract would not make much sense since then they would be locked to subclasses and it would be impossible to implement them. ) Since interfaces have no constructors you cannot run the new command on them, however you can create a reference to an interface by declaring a variable like this:

myInterface a;

You can use a reference to a superclass or interface to reference a subclass. This permits you to refer to different types of subclasses with the same superclass or interface reference. Interfaces in addition to having abstract methods may have public static final data types as attributes.

        The subclass will implement the interface using the keyword implements in place of extends in regular inheritance. The subclass must implement every method in the interface and cannot add any more methods which are not declared already in the interface. If is does not implement all the methods then the subclass will be abstract and must be declared as such. In any case, you must at least declare all the interface's methods. This is a way of forcing uniformity among a set of classes. In other words, by using interfaces and/or abstract classes you can guarentee that a set of subclasses will all have the same methods with the same return values and parameters. If you use an interface then you force every son to implement the methods himself. Then you can relate to objects by a superclass reference and call the subclass method implementations and you can be 100% sure these methods exist. Now, anyone who wants to add a class to your program will know what methods he needs to write in order to work in your program.

        For example, if I have an interface AudioFile (or even an abstract class AudioFile) and I want to use this class in my JukeBox program. I want the JukeBox to handle any type of audio file. But the JukeBox class will have to be able to play the file and stop the file. So I can make an interface AudioFile containing the abstract methods play and stop. Then if I write a class mp3File it will implement AudioFile and implement these two methods. I can do the same for wavFile etc. If some time in the future someone invents a new file format and wants to add the capacity to play these files in my JukeBox class all he has to do is to implement the methods of AudioFile and then my JukeBox can work with the new file type. This is because JukeBox works with class AudioFile. He does not care what subclass it is as long as it has the AudioFile "interface". From the perspective of JukeBox, all that exists are AudioFiles. There are no subclasses in his programming. This allows him to manipulate any future subclass that may be created.
Here is how to define an interface:

public interface AudioFile{
public abstract void play( parameters );
public abstract void stop( parameters );

Another advantage of using interfaces is that one can inherit from only one class but one can implement several interface classes. Simple separate with a comma each interface you are implementing. An interface is a form of an "is a" relationship. Here is how to implement an interface:

public class mp3File implements AudioFile{
public  void play( parameters )
   some code


public void stop( parameters )
   some code


some code


A full example of abstract class and interface.

Some more features of classes useful in dealing with abstract classes and inheritance

Nested Classes

A nested class is a class which is defined inside another class. For example, inside a class you might write a new class.
For example:
class myClass{
         class myNestedClass {
	                      some code 
         myNestedClass a;
This could be useful when making a list such as a first in first out ( fifo ) list. In such a case, we might make a class "fifoList" to contain all our list items. The class fifoList will use objects of type fifoInsertable. The outside world will insert instances of type Object into the fifoList and the fifoList will convert them into fifoInsertable objects. Therefore, only the class fifoList needs to be know about the fifoInsertable class. In such a case, the fifoInsertable class should be made as an inner class.

Some facts about inner classes:

Anonymous Inner Classes

An inner class declared in a method can be created without a name. This class must implement an interface. In such a case there is an inline instance made instead of a name being assigned to the instance. This instance can only be referenced at the line where it is created since there is no name by which to refer to it. This type of inner class is called an anonymous inner class.

Here is an example of an anonymous inner class.

© Nachum Danzig December 2003