What is an abstraction in java with example

Winnie 108 Published: 07/27/2024

What is an abstraction in java with example

I'm happy to help!

What is an Abstraction in Java?

In object-oriented programming (OOP), abstraction is a fundamental concept that allows you to hide the implementation details of an object while showing its public interface, which defines how the object can be used.

In Java, abstraction is achieved through encapsulation, inheritance, and polymorphism. An abstract class or interface is a concrete representation of an abstract concept, hiding the internal workings of the object and only revealing the essential features that other objects can use to interact with it.

Example: Abstract Shape Class

Suppose we want to define a set of shapes (e.g., circles, rectangles, triangles) that share common properties like color and fill mode. We can create an abstract class Shape to provide the foundation for all these shape types:

public abstract class Shape {

protected Color color; // shared attribute

protected boolean filled;

public Shape(Color color, boolean filled) {

this.color = color;

this.filled = filled;

}

public void paint() { // abstract method

System.out.println("Painting a " + getClass().getSimpleName());

}

}

In the Shape class:

We declare two protected attributes: color and filled. The constructor initializes these attributes. The paint() method is declared as abstract, meaning it must be implemented by any subclass that inherits from Shape.

Concrete Subclasses

Now we can create concrete subclasses for specific shape types:

public class Circle extends Shape {

public Circle(Color color, boolean filled) {

super(color, filled);

}

@Override

public void paint() {

System.out.println("Painting a circle with " + color + " fill.");

}

}

public class Rectangle extends Shape {

public Rectangle(Color color, boolean filled) {

super(color, filled);

}

@Override

public void paint() {

System.out.println("Painting a rectangle with " + color + " fill and " + (filled ? "filled" : "hollow") + ".");

}

}

public class Triangle extends Shape {

public Triangle(Color color, boolean filled) {

super(color, filled);

}

@Override

public void paint() {

System.out.println("Painting a triangle with " + color + " fill.");

}

}

Each subclass provides its own implementation of the paint() method, which is specific to that shape type.

Using the Abstraction

To use these shapes, we can create instances of the concrete subclasses and call their paint() methods:

public class Main {

public static void main(String[] args) {

Circle circle = new Circle(Color.RED, true);

circle.paint(); // Output: Painting a circle with RED fill.

Rectangle rectangle = new Rectangle(Color.BLUE, false);

rectangle.paint(); // Output: Painting a rectangle with BLUE fill and hollow.

Triangle triangle = new Triangle(Color.GREEN, true);

triangle.paint(); // Output: Painting a triangle with GREEN fill.

}

}

In this example, the Shape abstract class provides an abstraction of the common properties and behavior shared by all shape types. Each concrete subclass (Circle, Rectangle, and Triangle) inherits from Shape and implements its own specific details, hiding its internal workings. The client code (in this case, Main) interacts with these shapes using their public interfaces, without needing to know about the underlying implementation.

By abstracting away the internal complexities of each shape type, we've achieved encapsulation and modularity in our code, making it more maintainable, reusable, and scalable.

I hope this example helps illustrate the concept of abstraction in Java!

What is an abstraction in java w3schools

I'd be happy to help! However, please note that W3Schools is a web development platform that focuses on HTML, CSS, JavaScript, and other web technologies, not Java specifically. But I'll provide you with information about abstraction in the context of programming languages, including Java.

In programming, an abstraction is a concept or idea that represents complex systems, processes, or data structures in a simplified and manageable way. Abstraction allows developers to focus on essential features while hiding unnecessary details, making code more efficient, readable, and maintainable.

In object-oriented programming (OOP), such as Java, an abstraction can be achieved through encapsulation, inheritance, and polymorphism. Here's how:

Encapsulation: Encapsulate complex logic or data into a self-contained unit, like a class or module. This hides internal implementation details from the outside world, exposing only necessary interfaces or methods. Inheritance: Use inheritance to create a new class that inherits properties and behavior from an existing class (the superclass). This allows for code reuse, as the child class can build upon the foundation laid by the parent class. Polymorphism: Implement polymorphic methods that can operate on different data types or objects. This enables you to write generic code that works with various inputs without needing explicit type casting.

By using these abstraction techniques, Java developers can:

Simplify complex problems into manageable pieces Hide implementation details and expose only necessary interfaces Reuse code through inheritance and polymorphism Improve code readability by abstracting away low-level details

Here's an example of how you might use abstraction in a simple banking system using Java:

// Abstract class: BankAccount

public abstract class BankAccount {

private double balance;

public BankAccount(double initialBalance) {

this.balance = initialBalance;

}

public void deposit(double amount) {

balance += amount;

}

public void withdraw(double amount) {

if (amount <= 0 || amount > balance) {

throw new InsufficientFundsException();

}

balance -= amount;

}

public double getBalance() {

return balance;

}

}

// Concrete class: CheckingAccount

public class CheckingAccount extends BankAccount {

private int overdraftLimit;

public CheckingAccount(double initialBalance, int overdraftLimit) {

super(initialBalance);

this.overdraftLimit = overdraftLimit;

}

@Override

public void withdraw(double amount) {

if (amount > getBalance() + overdraftLimit) {

throw new OverdraftException();

}

super.withdraw(amount);

}

}

In this example:

The BankAccount class abstractly represents a basic banking account, encapsulating balance management. The CheckingAccount class inherits from BankAccount, adding specific features like overdraft limits and handling withdrawals accordingly.

This abstraction helps you focus on the essential behavior of a checking account (e.g., depositing, withdrawing) while hiding internal implementation details.