WikiGalaxy

Personalize

Java Abstraction

Understanding Abstraction:

Abstraction in Java is a process of hiding the implementation details and showing only the functionality to the user. It can be achieved using abstract classes and interfaces.

Abstract Classes:

An abstract class is a class that cannot be instantiated, but can be subclassed. It may contain abstract methods, which are methods without a body.

Interfaces:

An interface in Java is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types.


abstract class Animal {
  abstract void sound();
}

class Dog extends Animal {
  void sound() {
    System.out.println("Woof");
  }
}

public class Main {
  public static void main(String[] args) {
    Animal myDog = new Dog();
    myDog.sound();
  }
}
    

Key Points:

Abstract classes can have both abstract methods and concrete methods.

Implementation Note:

Concrete subclasses must implement all abstract methods from the abstract class.

Console Output:

Woof

Java Abstraction with Interfaces

Interface Implementation:

Interfaces specify what a class must do and are implemented by classes. They provide a form of multiple inheritance.


interface Animal {
  void sound();
}

class Cat implements Animal {
  public void sound() {
    System.out.println("Meow");
  }
}

public class Main {
  public static void main(String[] args) {
    Animal myCat = new Cat();
    myCat.sound();
  }
}
    

Key Points:

Interfaces can be used to achieve loose coupling between classes.

Implementation Note:

A class can implement multiple interfaces, allowing for more flexible design.

Console Output:

Meow

Abstract Methods in Java

Defining Abstract Methods:

Abstract methods are declared without an implementation and must be implemented in subclasses.


abstract class Vehicle {
  abstract void run();
}

class Bike extends Vehicle {
  void run() {
    System.out.println("Bike is running");
  }
}

public class Main {
  public static void main(String[] args) {
    Vehicle myBike = new Bike();
    myBike.run();
  }
}
    

Key Points:

Abstract methods provide a template for subclasses to follow.

Implementation Note:

All abstract methods in an abstract class must be implemented in its concrete subclasses.

Console Output:

Bike is running

Using Abstract Classes

Abstract Class Features:

Abstract classes can have constructors, member variables, and methods with implementations.


abstract class Shape {
  String color;
  
  Shape(String color) {
    this.color = color;
  }
  
  abstract double area();
}

class Circle extends Shape {
  double radius;
  
  Circle(String color, double radius) {
    super(color);
    this.radius = radius;
  }
  
  double area() {
    return Math.PI * radius * radius;
  }
}

public class Main {
  public static void main(String[] args) {
    Shape myCircle = new Circle("Red", 5);
    System.out.println("Area: " + myCircle.area());
  }
}
    

Key Points:

Abstract classes can have state (fields) and behavior (methods).

Implementation Note:

Use abstract classes when you want to provide common base functionality to multiple classes.

Console Output:

Area: 78.53981633974483

Abstract Class vs Interface

Differences:

Abstract classes can have method implementations, while interfaces cannot (until Java 8).


// Abstract class example
abstract class Animal {
  abstract void eat();
}

// Interface example
interface LivingBeing {
  void breathe();
}

class Human extends Animal implements LivingBeing {
  void eat() {
    System.out.println("Eating");
  }
  
  public void breathe() {
    System.out.println("Breathing");
  }
}

public class Main {
  public static void main(String[] args) {
    Human person = new Human();
    person.eat();
    person.breathe();
  }
}
    

Key Points:

Interfaces provide a way to achieve multiple inheritance, while abstract classes allow for partial implementation.

Implementation Note:

Choose interfaces when you need to define a contract for other classes to follow.

Console Output:

Eating
Breathing

Real-world Example of Abstraction

Banking System Abstraction:

In a banking system, you can have an abstract class for Account with abstract methods like deposit() and withdraw().


abstract class Account {
  abstract void deposit(double amount);
  abstract void withdraw(double amount);
}

class SavingsAccount extends Account {
  private double balance;
  
  void deposit(double amount) {
    balance += amount;
    System.out.println("Deposited: " + amount);
  }
  
  void withdraw(double amount) {
    if (balance >= amount) {
      balance -= amount;
      System.out.println("Withdrawn: " + amount);
    } else {
      System.out.println("Insufficient balance");
    }
  }
}

public class Main {
  public static void main(String[] args) {
    Account myAccount = new SavingsAccount();
    myAccount.deposit(500);
    myAccount.withdraw(100);
  }
}
    

Key Points:

Abstraction allows you to focus on what the object does instead of how it does it.

Implementation Note:

Use abstraction to simplify complex systems by modeling classes based on their behavior.

Console Output:

Deposited: 500
Withdrawn: 100

Abstraction in Software Design

Design Patterns:

Abstraction is a key principle in many design patterns such as Factory, Singleton, and Adapter.


interface Shape {
  void draw();
}

class Rectangle implements Shape {
  public void draw() {
    System.out.println("Drawing Rectangle");
  }
}

class Circle implements Shape {
  public void draw() {
    System.out.println("Drawing Circle");
  }
}

public class Main {
  public static void main(String[] args) {
    Shape rectangle = new Rectangle();
    Shape circle = new Circle();
    rectangle.draw();
    circle.draw();
  }
}
    

Key Points:

Abstraction helps in creating a blueprint for objects, making software design more robust and scalable.

Implementation Note:

Use abstraction to define a common interface for a group of related classes.

Console Output:

Drawing Rectangle
Drawing Circle

Abstraction in Game Development

Game Objects:

In game development, abstraction is used to define game objects like Player, Enemy, and NPCs with common behaviors.


abstract class GameObject {
  abstract void update();
}

class Player extends GameObject {
  void update() {
    System.out.println("Player is moving");
  }
}

class Enemy extends GameObject {
  void update() {
    System.out.println("Enemy is attacking");
  }
}

public class Main {
  public static void main(String[] args) {
    GameObject player = new Player();
    GameObject enemy = new Enemy();
    player.update();
    enemy.update();
  }
}
    

Key Points:

Abstraction helps in defining common behaviors for different game elements, making the code more manageable.

Implementation Note:

Use abstraction to define essential behaviors for game objects, allowing for easier updates and maintenance.

Console Output:

Player is moving
Enemy is attacking

Advanced Abstraction Techniques

Generic Abstraction:

Generics in Java provide a way to define classes, interfaces, and methods with a placeholder for types.


interface Container {
  void add(T item);
  T get();
}

class Box implements Container {
  private T item;
  
  public void add(T item) {
    this.item = item;
  }
  
  public T get() {
    return item;
  }
}

public class Main {
  public static void main(String[] args) {
    Box stringBox = new Box<>();
    stringBox.add("Hello");
    System.out.println(stringBox.get());
  }
}
    

Key Points:

Generics allow for type-safe code and reduce the need for casting.

Implementation Note:

Use generics to create flexible and reusable code components.

Console Output:

Hello

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025