Abstraction with Examples

Abstraction is the idea of simplifying complex things by focusing only on the important details, while ignoring the rest. This concept can be applied in many different areas, including programming.

Let's say you're writing a program that needs to interact with different shapes, such as circles and rectangles. Rather than writing separate code for each shape, you can use abstraction to create a "shape" class that defines the common features of all shapes, such as their color and size.

Within the "shape" class, you can create abstract methods that define the unique features of each shape. For example, a circle would have a radius, while a rectangle would have a length and width. By using abstract methods, you can leave the details of how these features are calculated to the subclasses that inherit from the "shape" class.

Here's an example of how this might look in Java:

java
abstract class Shape { String color; // Abstract methods abstract double area()abstract class Shape {
String color; // Abstract methods abstract double area(); public abstract String toString(); // Constructor public Shape(String color) { System.out.println("Shape constructor called"); this.color = color; } // Concrete method public String getColor() { return color; } } class Circle extends Shape { double radius; public Circle(String color, double radius) { super(color); System.out.println("Circle constructor called"); this.radius = radius; } // Implement abstract methods double area() { return Math.PI * Math.pow(radius, 2); } public String toString() { return "Circle color is " + super.color + " and area is : " + area(); } } class Rectangle extends Shape { double length; double width; public Rectangle(String color, double length, double width) { super(color); System.out.println("Rectangle constructor called"); this.length = length; this.width = width; } // Implement abstract methods double area() { return length * width; } public String toString() { return "Rectangle color is " + super.color + " and area is : " + area(); } } public class Test { public static void main(String[] args) { Shape s1 = new Circle("Red", 2.2); Shape s2 = new Rectangle("Yellow", 2, 4); System.out.println(s1.toString()); System.out.println(s2.toString()); } }

In this example, we create an abstract "shape" class that defines a color property, as well as abstract methods for calculating the area of the shape and converting it to a string representation. We also include a concrete method for getting the color property.

We then create two subclasses that inherit from "shape": "circle" and "rectangle". These subclasses implement the abstract methods for calculating the area and converting to a string representation, and also include their own properties (radius for circle, length and width for rectangle).

Finally, in the "test" class, we create instances of both circle and rectangle shapes, and use their implemented methods to calculate their area and convert them to strings.

Overall, abstraction allows us to write more efficient and reusable code by focusing on the essential details of objects and leaving out the unnecessary details.

Previous Post Next Post