11 Mar Java – Abstract Classes & Interfaces
In Java, data abstraction means hiding implementation details and exposing only essential functionality to the user. It is achieved using abstract classes (partial abstraction) and interfaces (full abstraction).
Data Abstraction in Java
- Definition: Abstraction is the process of hiding internal details and showing only the necessary features of an object.
- Purpose: Reduces complexity, improves code maintainability, and enhances flexibility.
How It’s Achieved:
- Abstract Classes: Can contain abstract methods (without implementation) and regular methods. Subclasses must implement abstract methods.
- Interfaces: Provide 100% abstraction. All methods are abstract by default.
Abstract Classes in Java
An abstract class is a class declared with the keyword abstract. It cannot create objects. It can contain:
- Abstract methods (methods without a body, only a declaration).
- Concrete methods (methods with full implementation).
- Variables (instance and static).
It serves as a base class that cannot be instantiated directly. Instead, other classes extend it and provide implementations for its abstract methods.
An Abstract Class:
- Is declared using the abstract keyword.
- Can have both abstract and non-abstract methods.
- Can include constructors, fields, and access modifiers.
An Abstract Method is a method declared without implementation (no body).
- It only has a method signature (name, parameters, return type).
- It must be defined inside an abstract class or an interface.
- Subclasses (or implementing classes) are responsible for providing the actual implementation.
Example of Abstract Classes
Let us see an example of Abstract classes in Java. Here, Animal defines what needs to be done (sound()), while Cat defines how it is done.
abstract class Animal {
public abstract void sound(); // abstract method
public void sleep() { // concrete method
System.out.println("Zzz...");
}
}
class Cat extends Animal {
public void sound() {
System.out.println("Meow");
}
}
public class Main {
public static void main(String[] args) {
Animal c = new Cat();
c.sound(); //
c.sleep(); //
}
}
Output
Meow Zzz...
Interface in Java
An interface in Java is a completely abstract class with no method bodies. It defines a contract (a set of methods) that implementing classes must fulfil. Unlike abstract classes, interfaces don’t provide implementation.
Features of Interfaces
- 100% abstraction
- Only method signatures (no body, except default/static methods).
- Multiple inheritance: A class can implement multiple interfaces.
- Variables: All variables in an interface are implicitly public, static, and final.
- Methods: By default, methods are public and abstract.
Override all the methods of the interface once it is implemented.
Example of Interface
Let us see an example of interfaces in Java.
interface Shape {
void draw(); // abstract method
double area(); // abstract method
}
class Circle implements Shape {
private double radius;
Circle(double radius) {
this.radius = radius;
}
public void draw() {
System.out.println("Drawing a Circle");
}
public double area() {
return Math.PI * radius * radius;
}
}
public class Main {
public static void main(String[] args) {
Shape s = new Circle(10);
s.draw();
System.out.println(s.area());
}
}
Output
Drawing a Circle 314.1592653589793
Multiple Interfaces
Java avoids the complexity of multiple inheritance of classes, but it achieves the same flexibility through interfaces. Since a class can implement multiple interfaces, you get the effect of multiple inheritance without the ambiguity.
Here’s a simple example:
// First interface
interface Singer {
void sing();
}
// Second interface
interface Dancer {
void dance();
}
// A class implementing both interfaces
class Performer implements Singer, Dancer {
public void sing() {
System.out.println("Singing a song...");
}
public void dance() {
System.out.println("Dancing on stage...");
}
}
public class Main {
public static void main(String[] args) {
Performer p = new Performer();
p.sing();
p.dance();
}
}
Output
Singing a song... Dancing on stage...
Here,
- It shows how a single class (Performer) can inherit multiple behaviors (Singer and Dancer).
- Interfaces let us model capabilities rather than rigid hierarchies.
- This makes the design flexible.
If you liked the tutorial, spread the word and share the link and our website, Studyopedia, with others.
For Videos, Join Our YouTube Channel: Join Now
Read More:
No Comments