Java - Abstract classes
An abstract class in java is a class which cannot be instantiated and can have methods which are not defined.
- To make a class abstract, we need to place the 'abstract' keyword just before the class definition.
- And as said above, there can be one or more undefined method which must have 'abstract' keyword before the method declaration.
- We cannot create objects of an Abstract class.
- The classes extending an abstract class has to define the undefined methods(i.e. the abstract methods).
We will clear the above points with the example :
Story :
The supreme god decided that in order to communicate, every living being should make some sound. i.e Humans will talk, dogs will bark, cats meao e.t.c.
Now, there was no way he could define the sound behavior as it will be different for all living beings. So he made this rule that every living being should have a sound behavior but the sound they makes will be determined by their own gods. And thus the class was prepared by Master God:
abstract class LivingBeing{
void breathe(){
System.out.println("Breathes oxygen.");
}
abstract void sound(); // The sound method is incomplete.
}
Now we can see the abstract keyword used in two places:
1) Before the sound() behaviour(As it is incomplete).
abstract void sound();
2) Before the LivingBeing class(Since sound() is incomplete, the class is incomplete as well).
abstract class LivingBeing
So, from the above definition it is pretty clear that if a method/behaviour is not defined it should be marked as abstract.
Thus the LivingBeing class is also Abstract because it has an abstact behaviour(Incomplete behaviour).
And since the class is incomplete we cannot create any objects out of it.
In other words abstract classes cannot be instantiated.
Then, what is the purpose of this class from which we cannot create any objects? We will see it below:
Human Being Creator
class Human extends LivingBeing{
String name;
String food;
String language;
Human(String nme, String fd, String lang){
this.name = nme;
this.food = fd;
this.language = lang;
}
public void eat(){
System.out.println("Eats "+food);
}
public void speak(){
System.out.println("Speaks "+language);
}
public void sound(){
System.out.println("Humans will speak"); // This method has to be defined.
}
}
Story :
So, the Human creator god is bound to define the sound() method/behaviour as he is extending the LivingBeing class. Naturally the purpose of the supreme god was solved. As he wanted all living beings should make some sound. What kind of sound? will be determined by their own Gods.
If the Human creator god wouldn't have defined the sound() method/behaviour the code would have ended up with some kind of error.
Note :There is no hard and fast rule that at least one method has to be abstract in an abstract class. An abstract class can contain all methods which are defined(i.e. non abstract methods) but if there is at least one method in a class marked abstract, the class should be abstract as well.