Abstract Classes

There are situations in which you will want to define a superclass that declares the structure of a given abstraction without providing a complete implementation of every method. For implementing this Java uses abstract classes. Below example shows the use of abstract classes. It’s nice to say that suppose every car is equipped with some safety features that are imposed. then we can design our class same as below :


//class should be declared abstract if any othe the method is abstract
abstract class CarVehicle { 
	int cost;

	abstract void SafetyBelt(); // abstract method1
	abstract void AirBags(); // abstract method1

//extend abstract class
public class AbstractExample extends CarVehicle { 
	public static void main(String[] args) {
		AbstractExample ab = new AbstractExample();
		ab.cost = 50000;

	// compulsory to override both abstract methods in derived class, otherwise it gives error
	void SafetyBelt() {
		System.out.println("4 seat belts");

	void AirBags() {
		System.out.println("5 air bags");


4 seat belts
5 air bags

You may also like...