Abstract classes are one way in which Java implements
abstraction. The other way in which abstraction is implemented is by creating interfaces. The abstract class is
modified using the keyword abstract.
This kind of class is created to provide a generalized structure that can be
shared by other classes. Abstract class will always have at least one subclass.
An Abstract
class will have abstract methods which have to be overridden by the subclasses
of it. The subclasses cannot simply use the version of abstract methods defined
in the superclass because abstract methods will have no body. The subclass has
to provide some body of its own to the abstract methods.
General form
of Abstract class is shown below:
abstract class AbsClass extends subclasses{
//body of class
// Abstract methods
//…………
}
General form of Abstract method is
shown below:
abstract type
AbsMethodName(parameter-list);
You can see no body is present for the abstract method
in the above general form.
Rules to follow while dealing with abstract
class:
1. Any class that contains one or more
than one abstract methods must be declared abstract.
2. You cannot create objects of abstract
class. It cannot be instantiated using new operator.
3. The reference variable of abstract
class can be created.
4. Abstract constructor cannot be
declared.
5. Abstract static methods also cannot be declared.
6. Subclass of abstract class must
implement all the abstract methods present in its superclass or the subclass
itself must be declared as abstract class.
In the
below program example you can see the implementation of Abstract class and its
subclasses.
package blog.abstraction.abstractClass;
public abstract class Automobile
{
int a=10, b=10;
abstract void engine_type(); //abstract method
abstract void audio_system();//abstract method
void addMethod()
{
int c=a+b;
System.out.println("concrete method is
allowed in abstract class");
System.out.println("addition(a+b)=
"+c);
}
public static void main(String[] args)
{
Bike bk=new Bike();
Car car=new Car();
HeavyVehicle
hvyV=new HeavyVehicle();
Automobile auto;
//
reference variable of type Automobile, an abstract class.
auto=bk;
auto.addMethod();
//bike class
bk.engine_type();
bk.audio_system();
//car class
car.engine_type();
car.audio_system();
//HeavyVehicle class
hvyV.engine_type();
hvyV.audio_system();
}
}
package blog.abstraction.abstractClass;
public class Bike extends Automobile
{
@Override
void engine_type()
{
System.out.println("Bike
Specification");
System.out.println("only petrol engine
is avaible");
}
@Override
void audio_system()
{
System.out.println("No audio
system");
}
}
package blog.abstraction.abstractClass;
public class Car extends Automobile
{
@Override
void engine_type()
{
System.out.println();
System.out.println("Car
Specification");
System.out.println("Petrol Type
Available");
System.out.println("Diesel Type
Available");
}
@Override
void audio_system()
{
System.out.println("Audio System
Available");
System.out.println("Branded audio
system");
}
}
package blog.abstraction.abstractClass;
public class HeavyVehicle extends Automobile
{
@Override
void engine_type()
{
System.out.println("Heavy Vehicle
Specification");
System.out.println("Only Diesel Available");
}
@Override
void audio_system()
{
System.out.println("Audio System
Available");
System.out.println("Local audio
system");
}
}
Output:
concrete method is
allowed in abstract class
addition(a+b)= 20
Bike Specification
only petrol engine is
avaible
No audio system
Car Specification
Petrol Type Available
Diesel Type Available
Audio System Available
Branded audio system
Heavy Vehicle
Specification
Only Diesel Available
Audio System Available
Local audio system
In above
example you can see Automobile is abstract class with abstract methods
engine_type() and audio_system() and also one concrete method addmethod(). The
Bike, Car and HeavyVehicle are the subclasses of the abstract class Automobile.
All the subclasses override the abstract methods of superclass Automobile and
implement them in its own way.