Hi all!
In that tutorial I'll explain about abstract classes, abstract methods and interfaces.
Plus, the differences between an interface and an abstract class.
Ok! lets start!
Abstract class
An abstract class is a class that is declared abstract.
It cannot be instantiated, so if you'll try running the following code you'll get an error messege:
public abstract class AbstractClass {
public AbstractClass(){//the constructor
}
public static void main(String args[]){
AbstractClass abstractClassInstance = new AbstractClass(); //Error!
}
}
However, an abstract class can be subclassed.
public abstract class AbstractClass {
}
public class SubClass extends AbstractClass{
}
After defining an abstract class, lets check why would you want to declare your class as an abstract class?
you would want to declare your class as an abstract class when your class describes an abstract idea
rather than a specific one.
Lets take an example from real life for an abstact class:
public abstract class Food{
}
Food! who doesnt like food! there are so much types of Food
but wait... what exactly is Food???? or, 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 Pancake 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 Pancake are subclasses of food. in code it would look something like this:
public abstract class Food {
}
public class Pizza extends Food {
}
public class Pancake extends Food {
}
You can declare instances of Pizza, and Pancake but not of food.
Ok. now, after we understood what an abstract class is, lets move to:
Abstract methods!
An abstract method is declared that way:
public abstract void abstractMethod();
Some important things concerned abstract methods:
1. Abstract method cannot be declared private!
2. Abstract methods don't have a body. these are empty methods.
so when declaring an abstract method, end the line with ";".
3.Only abstract classes can contain abstract methods!
(or interfaces, will be explained later).
Why would you want to use abstract method?
lets return to 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, Pancake, 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 dough, add cheese, insert the oven...
}
}//end of pizza subclass
public class Pancake extends Food{
public pancake(){
}
public void prepare(){
//add all ingredients on the pan, add sauce, onions, fry it...
}
}//end of Pancake 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.
After defining abstract methods and abstract classes we are ready for
the next topic.
Interfaces!
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!).
it looks like:
public interface newInterface{//newInterface can be replaced by every name you want.
int CONST1;//can be every data type. (long,char etc...).
String CONST2;
//more constants..
void Method1();
int Method2();//can return all sort of data (long,char, boolean etc).
//more methods...
}
This is how an interface should be look like.
important!
1.notice that I don't declare the constant variables as public\private\protected.
in an interface they all get: public static.
2.notice that i dont declare the methods with public\private and also not abstract.
in an interface all methods get by default: "public abstract".
3.There is another type of interface. Marker Interfaces. marker interfaces are markers without methods. like the serializable interface.
It looks like that:
public interface serializable {
}
later on my tutorial you'll understand why will you need an empty interface????
Lets 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?
first lets check the differences between an abstract class and an interface.
1.a certain class can extend only ONE superclass, while it can implement multiple interfaces.
2.an abstract class can contain implementation for some of its methods, while an interface only declares methods.
notice that if you declare an abstract class with only abstract methods, it should be declared as an interface!
3.an abstract class can contain all sort of variables(not only public static), and also can contain private, protected methods.
Now after understanding all basic definitions, we are ready for the main subject!
When will you use an interface rather than an abstract class?
Suppose you want to have Washable items.
who doesnt wash his stuff???
By stuff i mean your vehicle, your clothes etc.
Probably you have some kind of vehicle. right? a car? motorbike? a bicycle? rollerblades??
And i assume you wash them once a week..
And of course you have clothes you wear. (shirt, pants, jacket).
And i HOPE you wash them at least once a week!
so first declare a new class Washable:
NOTE: 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 definately different than washing a rollerblades. 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!
}
NOTE:the end of the wrong code i mentioned before.
What you want to do in such a case is declare Washable as an interface.
public interface Washable {
void wash();
}
Now you can implement it with all these classes, and implement the wash method differently!
public abstract class Vehicle implements Washable{
public Vehicle(){
}
public abstract void wash();//wash is abstract because it is not yet implemented!
}
public abstract class Clothes implements Washable{
public Clothes(){
}
public abstract void wash();//wash is abstract because it is not yet implemented!
}
public class Car extends Vehicle {
public Car(){
}
public void wash(){
//the wash method for a car
}
}
public class Bicycle extends vehicle {
public Bicycle(){
}
public void wash(){
//the wash method for a bicycle
}
}
//now for clothes
public Shirt extends Clothes {
public Shirt(){
}
public void wash(){
the wash method for shirt
}
}
//and so on!
most important note:
When implementing an interface's method, you can't assign it different access privelege other than public.
The reason is that all methods in theare declared public. and in java you cant override a method by assign it a weaker access.
implementing the Washable interface in both Clothes and Vehicles has benefits!
Suppose you have a new Robot and you want it to wash all your stuff.
BY stuff i mean all your Clothes and all your Vehicles. (i want that robot too!)
you could have a list of Washable objects and use the wash method on all of them!
public class Robot {
public Robot(){
}
public void washAllMyStuff(){
List<Washable> objectsToWash = new ArrayList<Washable>();
//add your washable items to the list
//then go over the list and invoke wash()
for(Washable washableObject : objectsToWash){
washableObject.wash();
}
}
Of course, you can create other abstract classes like Furniture and implement Washable on those classes.
then add them to the robot's washing list!
Have i already mention how Cool that Robot is?
ok that is the end of my tutorial, where i covered abstract classes, abstract methods and interfaces.
hope it helped






MultiQuote








|