abstract
keyword.
abstract returnType methodName(parameters);
abstract void makeSound(); // Abstract method – no body
abstract
keyword.
abstract class ClassName
{
// abstract method
abstract void makeSound();
// concrete method
void sleep()
{
System.out.println("Sleeping...");
}
}
abstract class Car
{
// Abstract method (must be implemented by subclasses)
abstract void startEngine();
// Concrete method
void fuelType()
{
System.out.println("This car uses petrol or diesel.");
}
}
class Sedan extends Car
{
@Override
void startEngine()
{
System.out.println("Sedan engine started with key ignition.");
}
}
abstract
keyword.
class Car
{
int no_of_tyres = 4;
void displayTyres()
{
System.out.println("Car has " + no_of_tyres + " tyres.");
}
void start()
{
System.out.println("Car starts with a key ignition.");
}
}
// Scooter class without abstraction
class Scooter
{
int no_of_tyres = 2;
void displayTyres()
{
System.out.println("Scooter has " + no_of_tyres + " tyres.");
}
void start()
{
System.out.println("Scooter starts with a kick or self-start.");
}
}
// Main class to run the program
public class MainApp
{
public static void main(String[] args)
{
Car myCar = new Car();
myCar.displayTyres();
myCar.start();
System.out.println();
Scooter myScooter = new Scooter();
myScooter.displayTyres();
myScooter.start();
}
}
Car has 4 tyres. Car starts with a key ignition. Scooter has 2 tyres. Scooter starts with a kick or self-start.
Vehicle vehicle = new Car();
// Not possible, because there is no common Vehicle type
Car
and Scooter
uniformly.
displayTyres()
is repeated in every class (Car
, Scooter
, etc.).
// Abstract class used to remove code duplication and enforce method structure
abstract class Vehicle
{
int no_of_tyres;
// Common method to avoid duplication (removes disadvantage #2)
void displayTyres()
{
System.out.println("This vehicle has " + no_of_tyres + " tyres.");
}
// Abstract method to enforce implementation in all subclasses (removes disadvantage #3)
abstract void start();
}
// Car class extends abstract class and provides its own implementation
class Car extends Vehicle
{
Car()
{
no_of_tyres = 4;
}
// Required by abstract class - enforces structure (removes disadvantage #3)
@Override
void start()
{
System.out.println("Car starts with key ignition.");
}
}
// Scooter class also extends abstract class
class Scooter extends Vehicle
{
Scooter()
{
no_of_tyres = 2;
}
@Override
void start()
{
System.out.println("Scooter starts with kick or self-start.");
}
}
// Main class to test polymorphism and abstraction
public class Main
{
public static void main(String[] args)
{
// Using polymorphism (removes disadvantage #1)
Vehicle myVehicle1 = new Car();
myVehicle1.displayTyres();
myVehicle1.start();
System.out.println();
Vehicle myVehicle2 = new Scooter();
myVehicle2.displayTyres();
myVehicle2.start();
// Easier to scale and add new vehicle types consistently (removes disadvantage #4)
}
}
This vehicle has 4 tyres. Car starts with key ignition. This vehicle has 2 tyres. Scooter starts with kick or self-start.
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
We’re here to make our tutorials better based on your thoughts and suggestions.