January 15, 2016

Abstract class and Interface.

Important Points: At every level abstract class and interface is always an important topic.

My simple effort to make you understand the basic requirements.Declare your class as an abstract class when your class describes an abstract idea rather than a specific one.

Let’s take an example from real life for an abstract class:

public abstract class Food {

}

Food! Who doesn’t like food! There are so many types of Food but wait… what exactly is Food???? Why did I declare Food abstract????

I’ll answer that question with another question. Suppose I asked you “what did you eat for lunch?” You may answer: “I ate pizza for lunch!” or maybe “I ate Cake for lunch!” But you will never answer, “I ate Food for lunch!”. Why? Because Food is an abstract definition for “Things we eat”.

While Pizza, and Cake are subclasses of food, in code it would look something like this:

public abstract class Food {}

public class Pizza extends Food {}

public class Cake extends Food {}

You can declare instances of Pizza, and Cake but not of food.

Ok. now, after we understood what an abstract class is, let’s move to: Abstract methods!

An abstract method is declared that way:

public abstract void abstractMethod();

Some important things concerned abstract methods:

  1. 1. Abstract method cannot be declared private!
  2. 2. Abstract methods don’t have a body. these are empty methods.
  3. so when declaring an abstract method, end the line with “;”.

  4. 3.Only abstract classes can contain abstract methods!

(or interfaces, will be explained later).

 

Why would you want to use abstract method?

 

lets understand from our food example.

To eat food, you have to prepare it.(cook\fry or so).

So how do you prepare Food?

There is no question for this question. Why??

 

Because each instance of food(pizza, cake, salad) is prepared in a different way!

public abstract class Food{

public food(){

}

public abstract void prepare();//the abstract method

}

public class Pizza extends Food {

public Pizza(){

}

public void prepare(){

//cut tomatoes, have the vegetables, add cheese, insert the oven…

}

}//end of pizza subclass

public class Cake extends Food{

public Cake(){

}

public void prepare(){

//add all ingredients on the pan, add sauce, onions, fry it…

}

}//end of Cake class

 

In that example you see how you use an abstract method. Abstract method should be used when you want to do the same operation, but in different ways.

 

INTERFACE

——————

What is an interface?

Think of an interface as a contract that a class does. In order to implement an interface a class must implement the interfaces methods.

An interface contains constants and abstract methods.(only abstract methods!).

Let’s proceed, and have an example for using interfaces from the Java API.

The List interface.

A List is an interface.

ArrayList and LinkedList are classes implementin the List interface. Suppose in your code you nedd a List of String objects(could be any other data type though).

List<String> stringList = new ArrayList<String>();

Later on your code, you decide that you made a mistake, and a LinkedList is what you need.

So you just change the declaration to:

List<String> stringList = new LinkedList<String>();

 

When will you use an interface? why not use an abstract class?

The following code is the wrong way to achieve our task. it is just to show why an abstract class is the wrong choice for what we want to do.

public abstract class Washable {

public Washable(){

}

public abstract void wash(){;

}

now, washing a car is different than washing a bicycle, and is definitely different than washing a Truck. right?

also, washing colored clothes is different than washing white clothes, so, what will you do if you want to create a car class?

public class car extends vehicle, extends Washable { //cant extend both!

}

What you want to do in such a case is declare Washable as an interface.

public interface Washable {

void wash();

}