Abstraction and Interfaces
Overview:
In Lesson 4, we explore the concepts of abstraction and interfaces in Java. Abstraction involves simplifying complex systems by modeling classes based on essential properties and behaviors. Interfaces provide a way to achieve abstraction by defining a contract for classes to implement.
Key Concepts:
- Abstract Classes and Methods:
- Definition: An abstract class is a class that cannot be instantiated and may contain abstract methods (methods without a body).
- Purpose: Abstract classes allow the definition of common behavior to be shared among subclasses, while abstract methods provide a blueprint for methods to be implemented by subclasses.
// Example of an abstract class
public abstract class Shape {
// Abstract method
public abstract void draw();
// Concrete method
public void resize() {
System.out.println("Resizing the shape.");
}
}
// Concrete subclass
public class Circle extends Shape {
@Override
public void draw() {
System.out.println("Drawing a circle.");
}
}
Interfaces:
- Definition: An interface is a collection of abstract methods. A class implements an interface by providing concrete implementations for all the methods declared in the interface.
- Purpose: Interfaces define a contract that classes can adhere to, allowing for code to interact with objects through a common set of methods.
// Example of an interface
public interface Drawable {
void draw();
void resize();
}
// Class implementing the interface
public class Rectangle implements Drawable {
@Override
public void draw() {
System.out.println("Drawing a rectangle.");
}
@Override
public void resize() {
System.out.println("Resizing the rectangle.");
}
}
Example:
Let’s create a program that demonstrates abstraction using abstract classes and interfaces. We’ll define an abstract class Shape
and an interface Drawable
, and then implement them in concrete classes.
// Abstract class
public abstract class Shape {
// Abstract method
public abstract void draw();
// Concrete method
public void resize() {
System.out.println("Resizing the shape.");
}
}
// Interface
public interface Drawable {
void draw();
void resize();
}
// Concrete class implementing abstract class and interface
public class Circle extends Shape implements Drawable {
@Override
public void draw() {
System.out.println("Drawing a circle.");
}
}
// Concrete class implementing only the interface
public class Square implements Drawable {
@Override
public void draw() {
System.out.println("Drawing a square.");
}
@Override
public void resize() {
System.out.println("Resizing the square.");
}
}
// Main class
public class AbstractionExample {
public static void main(String[] args) {
// Creating objects
Circle myCircle = new Circle();
Square mySquare = new Square();
// Using abstract class method
myCircle.resize(); // Output: Resizing the shape.
// Using interface methods
myCircle.draw(); // Output: Drawing a circle.
mySquare.draw(); // Output: Drawing a square.
mySquare.resize(); // Output: Resizing the square.
}
}
In this example, Shape
is an abstract class with an abstract method draw
, and Drawable
is an interface with methods draw
and resize
. The Circle
class implements both the abstract class and the interface, while the Square
class implements only the interface. The AbstractionExample
class demonstrates how to use abstract classes and interfaces for abstraction, allowing for a common interface to be shared among different classes.