Concept of Abstract classes and methods
Features of Abstract Classes
Concept of Abstract classes and methods
Features of Abstract Classes
Abstract class
An abstract class in programming is a class that cannot be instantiated directly, meaning you cannot create objects or instances of it.
Instead, it serves as a blueprint or template for other classes that derive from it, known as subclasses or concrete classes.
Abstract classes are typically used to define common attributes and methods that multiple related classes share.
Abstraction in Java
Abstraction is one of the four fundamental Object-Oriented Programming (OOP) concepts, alongside Encapsulation, Inheritance, and Polymorphism. It involves hiding the implementation details of an object and exposing only the relevant characteristics or behavior to the outside world. In Java, abstraction is achieved through abstract classes and interfaces.
There are two ways to achieve abstraction in java.
Abstract class (0 to 100%)
Interface (100%)
Abstract class in Java
An abstract class, when declared with the "abstract" keyword, is referred to as an abstract class. It can consist of both abstract methods (methods without a body) and non-abstract methods. It must be extended by other classes, and those subclasses are required to provide implementations for its abstract methods. Importantly, an abstract class cannot be directly instantiated.
An abstract class follows these rules:
It can contain both abstract (methods without a body) and non-abstract methods.
It must be marked with the "abstract" keyword.
It cannot be directly instantiated, meaning you cannot create objects of the abstract class.
It can include constructors and static methods.
It may have final methods, which prevent subclasses from altering their implementation.
Rules of Abstract Class
Example of abstract class
abstract class Shape {
int x, y;
abstract void draw(); // Abstract method
}
Abstract Methods
An abstract method is a method declared in an abstract class but without an implementation.
Subclasses of the abstract class must provide concrete implementations for all abstract methods.
Abstract methods are declared using the abstract keyword.
abstract void draw(); // Abstract method
abstract class Bike {
abstract void run();
}
class Honda4 extends Bike {
void run() {
System.out.println("Running safely");
}
public static void main(String[] args) {
Bike obj = new Honda4();
obj.run();
}
}
Output
Running safely
In this code:
Bike is an abstract class with an abstract method run().
Honda4 is a concrete subclass of Bike that provides an implementation for the run() method.
In the main method, an instance of Honda4 is created and assigned to a reference of type Bike. This demonstrates polymorphism, as you can use a reference to the abstract base class to refer to an object of the concrete subclass.
The run() method is called on the obj reference, which calls the run() method of the Honda4 subclass, printing "Running safely" to the console.
Abstract Methods
In object-oriented programming, abstraction is a concept that involves concealing or encapsulating the intricate implementation details from the user, while highlighting and providing access to the essential functionality. This practice enhances efficiency and simplifies complexity.
In Java, abstraction is realized through the use of abstract classes and methods. This tutorial will delve into abstract methods and their application in Java.
An abstract method is a method declared within an abstract class that is marked with the "abstract" keyword and does not provide an actual implementation (definition) in the abstract class itself.
When there's a requirement for only the method declaration in a superclass, you can achieve this by declaring the methods as abstract.
Abstract methods are often referred to as "subclass responsibilities" because they lack an implementation in the superclass. Consequently, any subclass that inherits from this superclass is obliged to override the abstract method and provide its own method implementation.
The syntax for declaring an abstract method in Java is as follows:
abstract returnType methodName(parameterList);
Here's an explanation of each part of the syntax:
abstract: The abstract keyword is used to declare a method as abstract. Abstract methods are methods that do not have a method body and must be implemented by concrete (non-abstract) subclasses.
returnType: This specifies the data type of the value that the method will return when it is called. It can be any valid Java data type, including primitive types (int, double, etc.) or reference types (class or interface types).
methodName: This is the name of the abstract method. It follows the same naming conventions as regular methods in Java.
parameterList: This is a list of parameters (if any) that the method accepts. Parameters are enclosed in parentheses (). If the method does not take any parameters, the parentheses are still required, but they will be empty.
example of an abstract method declaration:
abstract void calculateArea();
In this example, we have an abstract method named calculateArea() that does not return any value (void) and does not take any parameters
important rules and points to remember about abstract methods in Java:
Abstract methods lack an implementation; they only have a method signature (declaration). Subclasses that extend the abstract class are responsible for implementing these abstract methods.
If a non-abstract (concrete) class extends an abstract class, it must implement all the abstract methods of that abstract class. Otherwise, the concrete class must also be declared as abstract.
Abstract methods end with a semicolon (;) since they don't contain method bodies.
Abstract methods cannot have certain combinations of modifiers, including:
final
abstract native
abstract synchronized
abstract static
abstract private
abstract strictfp
A class containing at least one abstract method must itself be declared as abstract. However, the reverse is not true: an abstract class doesn't necessarily need to have abstract methods; it can have concrete methods as well.
Example:-
abstract class Shape {
abstract void draw(); // Abstract method
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing a circle");
}
}
class Rectangle extends Shape {
void draw() {
System.out.println("Drawing a rectangle");
}
}
public class Main {
public static void main(String[] args) {
Shape circle = new Circle();
Shape rectangle = new Rectangle();
circle.draw(); // Calls the draw() method of Circle
rectangle.draw(); // Calls the draw() method of Rectangle
}
}
In this example:
We have an abstract class Shape with an abstract method draw().
Two concrete subclasses, Circle and Rectangle, extend the Shape class and provide their own implementations for the draw() method.
In the main method, we create instances of Circle and Rectangle but store them in Shape references.
When we call the draw() method on these instances, it calls the appropriate draw() method defined in their respective subclasses.
Output:
Drawing a circle
Drawing a rectangle
The output demonstrates polymorphism, as we use references of the abstract class type (Shape) to call the concrete subclass's draw() method.
Features of Abstract Classes
Abstract classes in Java are a way to achieve abstraction, which is one of the four fundamental object-oriented programming (OOP) concepts (the others being encapsulation, inheritance, and polymorphism). Abstract classes provide a blueprint for other classes and can have both abstract (unimplemented) and concrete (implemented) methods. Here are the key features and characteristics of abstract classes in Java:
Abstract Methods:
Abstract classes can contain abstract methods, which are declared without any implementation using the abstract keyword.
Abstract methods serve as placeholders for methods that must be implemented by any concrete subclass.
Concrete Methods:
Abstract classes can also have concrete methods, which provide default implementations.
Subclasses are not required to override or provide implementations for concrete methods, but they can choose to do so if needed.
Incomplete Class:
An abstract class itself cannot be instantiated, meaning you cannot create objects of an abstract class.
It serves as a partially complete class that provides a structure for derived classes.
Subclassing:
Subclasses (concrete classes) extend abstract classes using the extends keyword.
Subclasses must provide implementations for all abstract methods declared in the abstract class.
Constructors:
Abstract classes can have constructors.
When a concrete subclass is instantiated, the constructor of the abstract class is called first.
Polymorphism:
Abstract classes enable polymorphism. You can use references of the abstract class type to refer to objects of concrete subclasses.
This allows for flexibility in object creation and method invocation.
Force Implementation:
Abstract classes provide a way to enforce a certain structure or set of methods that must be implemented by subclasses.
This ensures that specific behaviors are present in all derived classes.
Mix of Common and Specific Behavior:
Abstract classes can define common behavior and properties that are shared among multiple subclasses, reducing code duplication.
They can also include abstract methods that leave room for specialized behavior in subclasses.
example demonstrating some of these features
abstract class Animal {
String name;
public Animal(String name) {
this.name = name;
}
abstract void makeSound();
void eat() {
System.out.println(name + " is eating.");
}
}
class Dog extends Animal {
public Dog(String name) {
super(name);
}
@Override
void makeSound() {
System.out.println(name + " barks.");
}
}
class Cat extends Animal {
public Cat(String name) {
super(name);
}
@Override
void makeSound() {
System.out.println(name + " meows.");
}
}
Output
Buddy barks.
Buddy is eating.
Whiskers meows.
Whiskers is eating.
In this example, Animal is an abstract class with a constructor, an abstract method makeSound(), and a concrete method eat(). Dog and Cat are concrete subclasses of Animal that provide implementations for makeSound(). This structure enforces common behavior (e.g., eat()) and specific behavior (e.g., makeSound()) for different types of animals.