In Java
In Java
of abstract classes and abstract methods. It plays a critical role in object-oriented programming
(OOP) by facilitating the creation of base classes and enforcing method implementation in
subclasses. Below, I'll cover everything about the abstract concept in Java in detail.
1. Abstract Classes
An abstract class in Java is a class that cannot be instantiated on its own. It is meant to be
subclassed, and its abstract methods (if any) must be implemented by its subclasses. The primary
purpose of an abstract class is to provide a common base and some default behavior for other
classes to build upon.
• Cannot be instantiated: You cannot create objects directly from an abstract class using
the new keyword.
• May have abstract methods: An abstract class can have abstract methods, which are
methods without implementation. These methods must be implemented by the subclasses.
• May have concrete (non-abstract) methods: An abstract class can also contain methods
with an implementation. This allows the abstract class to provide default functionality to
its subclasses.
• Can have instance variables: Abstract classes can have fields, including both static and
instance variables.
• Can have constructors: Abstract classes can have constructors, which can be called by
subclasses during instantiation.
java
Copy code
abstract class Animal {
// Abstract method (no implementation)
abstract void sound();
• Animal is an abstract class with an abstract method sound() and a concrete method
sleep().
• The Dog class extends Animal and provides an implementation for the sound() method.
• Animal cannot be instantiated, but Dog can be.
2. Abstract Methods
An abstract method is a method that is declared in an abstract class but has no implementation
(i.e., it ends with a semicolon rather than curly braces).
• Syntax:
java
Copy code
abstract returnType methodName(parameters);
java
Copy code
abstract class Vehicle {
abstract void startEngine();
}
Here:
Java has both abstract classes and interfaces, and they can sometimes serve similar purposes.
However, there are key differences between the two:
• Abstract Class: Can have both abstract methods (without implementation) and concrete
methods (with implementation). A class can extend only one abstract class (single
inheritance).
• Interface: All methods in an interface are abstract by default (though starting from Java
8, interfaces can have default and static methods with implementations). A class can
implement multiple interfaces (multiple inheritance).
Key Differences:
• You want to provide a common interface for all subclasses: You can define common
methods in the abstract class and force subclasses to implement abstract methods.
• You want to share common functionality: Abstract classes can have implemented
methods that can be shared across multiple subclasses.
• You want to create a base class that cannot be instantiated: An abstract class serves
as a blueprint for other classes.
5. Abstract Classes with Abstract Methods
If a class contains abstract methods, that class must be declared abstract. However, if an abstract
class contains only concrete methods (with implementations), it can technically be instantiated
(unless it is abstract for other reasons).
java
Copy code
abstract class Shape {
abstract void draw();
}
Abstract classes are often used in combination with polymorphism. For example, you can use an
abstract class type to refer to objects of subclasses, allowing for dynamic method calls based on
the actual object type.
Example:
java
Copy code
abstract class Animal {
abstract void makeSound();
}
myDog.makeSound(); // Bark
myCat.makeSound(); // Meow
}
}
Conclusion:
4o mini