ABSTRACT CLASS

 
An abstract class is something which is not concrete,something which is incomplete.
Abstract class specifies what functionality is provided but not how that functionality is provided.
An abstract class is a class that is declared abstract.
Abstract class may or may not include abstract methods.
Abstract classes cannot be instantiated, but they can be sub-classed.
An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this: abstract void moveTo(double X, double Y);
An abstract Method doesn't have method body.
Use abstraction if you know something needs to be in class but implementation of that varies. e.g for Human class you know there will be eating method but eating method implementation will be different for different country.
If you declare any method in class as abstract,you have declare class also as abstract.
 

Abstract Class can contain abstract as well as non-abstract method.

1
2
3
4
5
6
7
package in.java4all.abstractclass;
public abstract class AbstractClasswithNonAbstractMethod {
    abstract void abstractmethod();
    public void nonabstractmethod(){
        System.out.println("Abstract Class can hold non-abstract Method also");
    }    
}

A class can be declared abstract without any abstract method also.

1
2
3
4
5
6
7
8
  package in.java4all.abstractclass;
public abstract class AbstractClassWithoutAbstractMethod {
    public void nonabstractmethod(){
        System.out.println("Abstract Class can be declared without any abstract method");
    }
    public static void main(String[] args){
        System.out.println("Main Method can be decalred inside Abstract Class");
    }

 

 

 

 When to Use Abstract Class?

The purpose of an abstract class is to specify the default functionality of an object and let its sub-classes to explicitly implement that functionality.

Suppose we were modelling the behaviour of Human, by creating a class hierarchy that started with a base class called Human. Human are capable of doing different things like seeing, sleeping and walking, but there are some common operations as well like eating and speaking. Some common operations are performed by all human, but in a different way as well. When an operation(behaviour) is performed in a different way, it is a good candidate for an abstract method (forcing subclasses to provide a custom implementation). Let's look at a very primitive Human base class, which defines an abstract method for making a speaking(such as a French speaking French, an Indian Speaking Hindi, or an American speaking English).
 
1
2
3
4
5
6
7
8
9
10
11
12
public abstract Human
{
   public void eating(Food food)
   {
        // do something with food....
   }
   public void sleeping (int hours)
   {
   // do something with sleeping
   }
   public abstract void speaking();
}

Now, any Human that wants to be instantiated (like a Indian or American) must implement the speaking method - otherwise it is impossible to create an instance of that class.

Let's look at a Indian and American subclass that extends the Animal class.

1
2
3
4
5
6
 public American extends Animal
{
   public void speaking() {
   System.out.println ("English English");
   }
}
 
Why we cannot instantiate Abstract class in java?
If the class had everything necessary to construct a meaningful object, the author wouldn't have declared it abstract.
The whole definition of "abstract class" is that it is an incompletely implemented parent, and therefore instances wouldn't make sense.
An Abstract class may or may not have abstract methods.
So in that case JVM does not know how much memory it has to allocate for that abstract method because abstract method does not have body implementation.
So JVM will not able to allocate memory for the abstract methods when the time of creating instance to Abstract class.
So JVM unable to create the instance to Abstract class. So that we can't create the object for the Abstract class.
It is also possible that we can create an Abstract class with all concrete methods, that is without any abstract methods also.
 
In that case also we can't create the instance for the Abstract class.
Why because the abstract keyword simply indicates to JVM that the class cannot be instantiated.
The designers of Java made the JVM that when it find abstract keyword for any class then JVM can't create the instance for that class.