Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
In Java, an abstract class is a class that cannot be instantiated (i.e., you cannot create an object of an abstract class). It is meant to be subclassed (inherited) by other classes. Abstract classes can have abstract and non-abstract methods.
In this tutorial, we'll cover the following topics related to abstract classes in Java:
Creating an Abstract Class
Abstract Methods
Inheriting from an Abstract Class
Abstract Classes vs. Interfaces
Creating an Abstract Class:
To create an abstract class, use the abstract
keyword before the class
keyword:
abstract class Animal { // Class body }
An abstract method is a method that has no implementation in the abstract class and must be implemented by any non-abstract subclass of the class. To create an abstract method, use the abstract
keyword before the method signature and end the method declaration with a semicolon:
abstract class Animal { abstract void sound(); // Abstract method }
When a class inherits from an abstract class, it must implement all the abstract methods of the abstract class, unless the subclass is also an abstract class. To inherit from an abstract class, use the extends
keyword:
class Dog extends Animal { @Override void sound() { System.out.println("Woof!"); } }
While both abstract classes and interfaces can contain abstract methods, there are some differences between the two:
Here's an example to illustrate the differences:
abstract class Mammal { protected int age; public Mammal(int age) { this.age = age; } abstract void sound(); public void sleep() { System.out.println("The mammal is sleeping."); } } interface Pet { void play(); } class Cat extends Mammal implements Pet { public Cat(int age) { super(age); } @Override void sound() { System.out.println("Meow!"); } @Override public void play() { System.out.println("The cat is playing."); } }
In the example above, Mammal
is an abstract class with an abstract method sound()
and a non-abstract method sleep()
. Pet
is an interface with an abstract method play()
. Cat
is a concrete class that extends Mammal
and implements Pet
, providing implementations for both sound()
and play()
methods.
This tutorial should give you a basic understanding of abstract classes in Java. With this knowledge, you can effectively use abstract classes to create reusable and organized code in your Java programs.
Java Abstract Class Definition:
// Abstract class definition abstract class Shape { // Abstract method declaration abstract void draw(); }
Java Abstract Class vs. Interface:
// Abstract class example abstract class Animal { abstract void makeSound(); } // Interface example interface Movable { void move(); }
Abstract Class in Java Example:
abstract class Animal { abstract void makeSound(); void sleep() { System.out.println("Zzz..."); } }
How to Declare Abstract Methods in Java:
abstract
keyword and have no implementation in the abstract class.abstract class Vehicle { abstract void start(); }
Extending an Abstract Class in Java:
extends
keyword.// Concrete class extending an abstract class class Circle extends Shape { void draw() { System.out.println("Drawing a circle"); } }
Implementing Abstract Methods in Java:
// Concrete class implementing abstract methods class Car extends Vehicle { void start() { System.out.println("Car starting..."); } }
Java Abstract Class Constructor:
abstract class Bird { Bird() { System.out.println("A bird is born"); } abstract void fly(); }
Abstract Class and Inheritance in Java:
abstract class Vehicle { void start() { System.out.println("Vehicle starting..."); } } class Car extends Vehicle { // Inherits the start method }
Abstract Class and Polymorphism in Java:
// Polymorphism with abstract class Vehicle myCar = new Car(); myCar.start(); // Calls the start method of Car
Abstract Class and Encapsulation in Java:
abstract class Shape { private String color; Shape(String color) { this.color = color; } abstract void draw(); }
Abstract Class vs. Concrete Class in Java:
// Abstract class with concrete and abstract methods abstract class Shape { abstract void draw(); void resize() { System.out.println("Resizing..."); } }
Abstract Class and Final Keyword in Java:
final
to prevent further subclassing.final abstract class Shape { abstract void draw(); }
Abstract Class and Access Modifiers in Java:
abstract class Shape { private int sides; protected abstract void draw(); }
Abstract Class and Static Methods in Java:
abstract class Utility { static void doSomething() { System.out.println("Doing something..."); } }
Abstract Class and Instance Variables in Java:
abstract class Animal { String name; Animal(String name) { this.name = name; } abstract void makeSound(); }
When to Use Abstract Classes in Java:
abstract class Shape { abstract void draw(); void resize() { System.out.println("Resizing..."); } }
Abstract Class Design Patterns in Java:
abstract class Algorithm { void execute() { step1(); step2(); step3(); } abstract void step1(); abstract void step2(); abstract void step3(); }
Common Pitfalls with Java Abstract Classes:
abstract class Vehicle { abstract void start(); } // Incorrect: Missing implementation for start in subclass class InvalidCar extends Vehicle { // Compilation error }