Home » Java » Abstract classes & methods » Abstract classes & methods

Abstract classes & methods


arpit tak

arpit tak

I like JAVA . I code. I chill. I blog.I eat. I sleep. I dream.

Personal Links

View Full Profile →

Abstract classes & methods

When dealing with inheritance, we have seen the relationship between super classes and subclasses. However, this was all done on a public level.

An abstract, by its textbook definition, is a rough sketch of something. By it’s application in Java, an abstract class is a class that is a sketch of something to come. It is a class that cannot be directly constructed. This is a common error among Java programmers.

Abstract classes

In order for a class to be declared abstract, you must use the abstract keyword:

public abstract class Name {

As an example, let’s say that you are thinking of running an electronic store. We know that there can be CD’s, DVD’s, VHS tapes or even, for those oldies, Records. All of these are of a “super” type called Media. So for Java, the Media class can be declared abstract while the other classes can be subclasses of Media. Here is a diagram to show this:

/    |

where Media would be abstract (in addition to the super class) and the other classes would extend Media.

Now let’s see that sketch above implemented in some code. Try to follow the program and compute the output before looking:

Example 1:
Abstract classes example

public abstract class MediaItem{

private String title;  //global title variable
private int year;  //global year variable
private int quantity;  //global quantity variable

public MediaItem(String t, int yr, int q){

   if(t == null)
   throw new IllegalArgumentException("No title given");
      else if(yr < 0 || yr > 2010)
    throw new IllegalArgumentException("Bad year");
    else if(q < 0)
       throw new IllegalArgumentException("Bad quantity");
                title = t;
		year = yr;
		quantity = q;
        } //constructor

    public String toString(){
       return("Title: " + title
         + "nYear: " + year
	 + "nQuantity: " + quantity);
        } //toString()
} //class

public class CD extends Media{
	public CD(String t, int yr, int q){
		super(t, yr, q);
        } //constructor
} //class

public class DVD extends Media{
	public DVD(String t, int yr, int q){
		super(t, yr, q);
        } //constructor
} //class

public class VHS extends Media{
	public VHS(String t, int yr, int q){
		super(t, yr, q);
        } //constructor
} //class

public class Store{
	public static void main(String args[]){

        Media items[] = new Media[5];

	for(int i = 0; i < 5; i++){
	      if(i % 3 == 0)
	  items[i] = new CD("Im a CD", 1993, i*3);
	      else if(i % 3 == 1)
	  items[i] = new DVD("Im a DVD", 1999, i*2);
        items[i] = new VHS("Im old!", 1987, i*4);
	   }catch(IllegalArgumentException iae){}


	for(int i = 0; i < 5; i++)
            System.out.println("Item: " + items[i] + "n");

        } //main
} //class

The above is an example to show how a class can be declared abstract. Each of the CD, DVD, and VHS classes are subclasses of Media.

In the main method of the class Store, we can declare an array of Media objects. Here we declare it to be of size 5. This is certainly allowed so long as we don’t say:

items[i] = new Media( ... );

By the rules of abstract classes, this is not allowed. The abstract class Media cannot be constructed directly.

Also in the main method of class Store, we include a try/catch block since in the Media constructor, we are throwing an exception if a certain condition is met. This try/catch block is there just for good practice since none of the items in this example are throwing an exception.

The output from the above example is:


Item: Title: Im a CD
Year: 1993
Quantity: 0

Item: Title: Im a DVD
Year: 1999
Quantity: 2

Item: Title: Im old!
Year: 1987
Quantity: 8

Item: Title: Im a CD
Year: 1993
Quantity: 9

Item: Title: Im a DVD
Year: 1999
Quantity: 8

Abstract methods

In Java, a method can be declared abstract. This means that it will not have an implementation. It also means that it MUST appear inside an abstract class.

The method will use the same abstract keyword in it’s title:

abstract identifier type Name();

where identifier can be public, private or protected; type is the return type of the method and Name is the name of the useful name of the method. Notice that there is a semicolon appearing at the end of the method declaration. This is how to denote an abstract method since there is no implementation after it.

Let’s say that you were writing a Java program to keep track of different Food stores in your area. Each food store may have a different slogan. So in many ways, the FoodStore class can be abstract and inside that class, the method called slogan() can be abstract. The method will be implemented in each of the subclasses.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: