🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
In Java, an abstract class is a class that cannot be instantiated on its own and is meant to be subclassed. It can contain both abstract methods (methods without a body) and concrete methods (methods with an implementation). Abstract classes are used to provide a common base class for other classes to extend and share common code.
Key Points:
- Cannot be Instantiated: Abstract classes cannot be instantiated directly.
- Abstract Methods: Can contain methods without implementation.
- Concrete Methods: Can also contain methods with implementation.
- Constructors: Can have constructors, which can be called by subclasses.
- Common Base Class: Used to share common code among related classes.
Table of Contents
- Defining an Abstract Class
- Abstract Methods
- Implementing Abstract Methods
- Constructors in Abstract Classes
- Real-World Example
- Conclusion
1. Defining an Abstract Class
An abstract class is defined using the abstract keyword. It can contain both abstract and concrete methods.
Example:
public abstract class Animal {
// Abstract method
public abstract void makeSound();
// Concrete method
public void eat() {
System.out.println("Eating...");
}
}
Explanation:
- abstract class Animal: Declares an abstract class named
Animal. - public abstract void makeSound(): An abstract method that must be implemented by subclasses.
- public void eat(): A concrete method with an implementation.
2. Abstract() Methods
Abstract methods are methods that do not have an implementation. Subclasses must provide an implementation for these methods.
Example:
public abstract class Animal {
public abstract void makeSound();
}
Explanation:
- public abstract void makeSound(): Declares an abstract method that subclasses must implement.
3. Implementing Abstract() Methods
Subclasses of an abstract class must implement all abstract methods from the abstract class.
Example:
public class Dog extends Animal {
@Override
public void makeSound() {
System.out.println("Bark");
}
}
public class AbstractClassExample {
public static void main(String[] args) {
Dog dog = new Dog();
dog.makeSound(); // Outputs: Bark
dog.eat(); // Outputs: Eating...
}
}
Explanation:
- Dog: A subclass of
Animalthat provides an implementation for themakeSound()method. - AbstractClassExample: A class with a
mainmethod to test theDogclass.
4. Constructors in Abstract Classes
Abstract classes can have constructors, which can be called by subclasses to initialize common fields.
Example:
public abstract class Animal {
private String name;
public Animal(String name) {
this.name = name;
}
public String getName() {
return name;
}
public abstract void makeSound();
public void eat() {
System.out.println("Eating...");
}
}
public class Dog extends Animal {
public Dog(String name) {
super(name);
}
@Override
public void makeSound() {
System.out.println("Bark");
}
}
public class ConstructorExample {
public static void main(String[] args) {
Dog dog = new Dog("Buddy");
System.out.println("Dog's name: " + dog.getName()); // Outputs: Dog's name: Buddy
dog.makeSound(); // Outputs: Bark
dog.eat(); // Outputs: Eating...
}
}
Explanation:
- Animal(String name): A constructor in the abstract class
Animalto initialize thenamefield. - Dog(String name): A constructor in the
Dogclass that calls the superclass constructor usingsuper(name).
5. Real-World Example
Let's create a real-world example with multiple abstract classes and concrete classes that extend them.
Example:
public abstract class Vehicle {
private String model;
public Vehicle(String model) {
this.model = model;
}
public String getModel() {
return model;
}
public abstract void start();
public void stop() {
System.out.println("Vehicle stopped.");
}
}
public class Car extends Vehicle {
public Car(String model) {
super(model);
}
@Override
public void start() {
System.out.println("Car " + getModel() + " started.");
}
}
public class Motorcycle extends Vehicle {
public Motorcycle(String model) {
super(model);
}
@Override
public void start() {
System.out.println("Motorcycle " + getModel() + " started.");
}
}
public class RealWorldExample {
public static void main(String[] args) {
Car car = new Car("Toyota");
Motorcycle motorcycle = new Motorcycle("Harley Davidson");
car.start(); // Outputs: Car Toyota started.
car.stop(); // Outputs: Vehicle stopped.
motorcycle.start(); // Outputs: Motorcycle Harley Davidson started.
motorcycle.stop(); // Outputs: Vehicle stopped.
}
}
Explanation:
- Vehicle: An abstract class with an abstract method
start()and a concrete methodstop(). - Car: A subclass of
Vehiclethat provides an implementation for thestart()method. - Motorcycle: Another subclass of
Vehiclethat provides an implementation for thestart()method. - RealWorldExample: A class with a
mainmethod to test theCarandMotorcycleclasses.
6. Conclusion
Abstract classes in Java provide a way to define common behavior that can be shared among related classes while allowing for customization through abstract methods. By using abstract classes, you can achieve a higher level of code reuse and organization. Understanding how to define and use abstract classes and methods can help you design more flexible and maintainable Java applications.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment