0% found this document useful (0 votes)
4 views11 pages

Abstract Classes in JAVA

The document explains the rules and characteristics of abstract classes in Java, including their inability to be instantiated, the requirement for subclasses to implement abstract methods, and the allowance of constructors and static methods. It provides examples of abstract classes such as Animal and Vehicle, demonstrating their use in enforcing method implementation and defining common structures for related classes. Additionally, it outlines real-world use cases for abstract classes, such as in a zoo management system and a transportation system.

Uploaded by

kesavat0001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views11 pages

Abstract Classes in JAVA

The document explains the rules and characteristics of abstract classes in Java, including their inability to be instantiated, the requirement for subclasses to implement abstract methods, and the allowance of constructors and static methods. It provides examples of abstract classes such as Animal and Vehicle, demonstrating their use in enforcing method implementation and defining common structures for related classes. Additionally, it outlines real-world use cases for abstract classes, such as in a zoo management system and a transportation system.

Uploaded by

kesavat0001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Abstract Classes in Java:

Rules for Abstract Classes in Java


1. Cannot be instantiated: You cannot create an object of
an abstract class.
2. Can have both abstract and non-abstract methods:
o Abstract methods (declared but not implemented)

must be implemented by subclasses.


o Concrete methods (regular methods with

implementations) can be used directly or


overridden.
3. Can have constructors: An abstract class can have
constructors, which are called when a subclass object is
created.
4. Can have fields and methods: It can contain instance
variables and concrete methods like a normal class.
5. Must be extended: Any subclass that extends an abstract
class must either:
o Implement all abstract methods or

o Declare itself as abstract.

6. Can have static methods: Static methods are allowed in


abstract classes but cannot be abstract.
7. Supports inheritance: Abstract classes can implement
interfaces and extend other classes.

Example of Abstract Class in Java


// Abstract class
abstract class Animal {
// Abstract method (no implementation)
abstract void makeSound();

// Concrete method
void sleep() {
System.out.println("Sleeping...");
}
}

// Concrete subclass
class Dog extends Animal {
// Implementing the abstract method
void makeSound() {
System.out.println("Barking...");
}
}

public class AbstractExample {


public static void main(String[] args) {
Dog myDog = new Dog();
myDog.makeSound(); // Output: Barking...
myDog.sleep(); // Output: Sleeping...
}
}

Explanation:
 Animal is an abstract class with an abstract method
makeSound().
 Dog extends Animal and implements makeSound().
 The sleep() method is already implemented in
Animal, so Dog can use it directly.

Example of Abstract Class with a Constructor


abstract class Vehicle {
String brand;

// Constructor
Vehicle(String brand) {
this.brand = brand;
System.out.println("Vehicle constructor called");
}

abstract void start();


}
class Car extends Vehicle {
Car(String brand) {
super(brand); // Calling parent constructor
}

void start() {
System.out.println(brand + " Car is starting...");
}
}

public class AbstractWithConstructor {


public static void main(String[] args) {
Car myCar = new Car("Toyota");
myCar.start();
}
}

Output:
Vehicle constructor called
Toyota Car is starting...

Explanation:
 The Vehicle abstract class has a constructor.
 The Car class calls the parent constructor using
super(brand).
 This allows initialization of the brand attribute before
calling start().

Key Takeaways
✔ Abstract classes cannot be instantiated.
✔ They can contain abstract and concrete methods.
✔ Subclasses must implement all abstract methods or
declare themselves as abstract.
✔ Constructors are allowed in abstract classes.
✔ Static methods are allowed, but they cannot be abstract.

Real-Time Use Cases of Abstract Classes in


Java
Abstract classes are useful when you want to define a common
template for multiple related classes while ensuring that certain
methods are implemented by subclasses. Below are some real-world
scenarios where abstract classes are commonly used.

1. Template for Different Types of Animals (Polymorphism)

Scenario:

In a zoo management system, different animals make different


sounds. Instead of defining a separate makeSound() method in every
class, an abstract class can enforce that all animals must implement it.

Example:
abstract class Animal {
abstract void makeSound(); // Must be implemented by subclasses

void sleep() {
System.out.println("Sleeping...");
}
}

class Dog extends Animal {


void makeSound() {
System.out.println("Dog barks");
}
}

class Cat extends Animal {


void makeSound() {
System.out.println("Cat meows");
}
}

public class Zoo {


public static void main(String[] args) {
Animal myDog = new Dog();
myDog.makeSound(); // Output: Dog barks

Animal myCat = new Cat();


myCat.makeSound(); // Output: Cat meows
}
}

✅ Use Case: Ensures all animals must have a makeSound() method.

2. Defining a Common Vehicle Structure (Blueprint for


Subclasses)

Scenario:

In a transportation system, different types of vehicles (cars, bikes,


trucks) have different ways of starting, but all vehicles share common
attributes like brand and speed.

Example:
abstract class Vehicle {
String brand;

Vehicle(String brand) {
this.brand = brand;
}

abstract void start(); // Each vehicle starts differently

void stop() {
System.out.println(brand + " has stopped.");
}
}

class Car extends Vehicle {


Car(String brand) {
super(brand);
}

void start() {
System.out.println(brand + " Car starts with a key.");
}
}

class Bike extends Vehicle {


Bike(String brand) {
super(brand);
}

void start() {
System.out.println(brand + " Bike starts with a kick.");
}
}

public class TransportSystem {


public static void main(String[] args) {
Vehicle myCar = new Car("Toyota");
myCar.start(); // Output: Toyota Car starts with a key.
myCar.stop(); // Output: Toyota has stopped.

Vehicle myBike = new Bike("Yamaha");


myBike.start(); // Output: Yamaha Bike starts with a kick.
}
}

✅ Use Case: Defines a common structure for all vehicles while


allowing different starting mechanisms.

Assignment
1. Banking System (Defining Account Types)

Scenario:

Banks have different types of accounts like savings and current accounts. They all share a
method to calculate interest but implement it differently.

2. Employee Payroll System (Common Pay Structure)

Scenario:

A company has permanent and contract employees, both of whom receive salaries but have
different ways of calculating pay.

You might also like