# Abstraction

**Hide the Details, Show the Essence ✨**

Abstraction in Java is about **hiding implementation details** 🙈 and only exposing essential functionality to users. 💡

It helps **reduce complexity** 😅 and allows focusing on what an object does rather than how it does it. 🔍

## **Ways to Achieve Abstraction**

```java
// Abstract Class 
public abstract class Animal {
  public abstract void makeSound(); // Abstract method
}

// Interface 
public interface Flyable {
  public void fly(); // Interface method
}
```

* **Abstract Classes** 🏫
  * 0-100% abstraction
  * Can have concrete methods 👍
* **Interfaces** ⛓️
  * 100% abstraction
  * Cannot have concrete methods ❌

**Benefits** 🎁

* Isolates impact of changes 💪
* Promotes reusability 🙌
* Enables focus on essentials 🎯

Abstraction is key to managing complexity in large systems! 🚀

## 📚 Rules for Abstraction in Java 📚

Abstraction refers to hiding implementation details and only exposing functionality to users. In Java, abstraction is achieved through abstract classes and interfaces.

### 🖼️ Rules for Abstract Classes 🖼️

Abstract classes have the following rules:

* An abstract class cannot be instantiated directly
* An abstract class may or may not contain abstract methods
* Abstract methods only declare the method signature and do not provide an implementation
* Subclasses must override all abstract methods by providing implementations

For example:

```java
public abstract class Shape {

  public abstract void draw();

}

public class Circle extends Shape {

  @Override
  public void draw() {
    // code to draw a circle
  }

} 
```

`Shape` is an abstract class with an abstract `draw()` method. `Circle` overrides `draw()` to provide an implementation.

### 📑 Rules for Interfaces 📑

Some key rules for interfaces are:

* An interface cannot be instantiated directly
* An interface can only contain abstract methods and static constants
* Methods in an interface are implicitly abstract and public
* Classes must override all interface methods when implementing an interface

For example:

```java
public interface Drawable {
  
  void draw();

}

public class Square implements Drawable {

  @Override
  public void draw() {
    // code to draw a square
  }

}
```

The `Drawable` interface enforces a `draw()` contract. `Square` implements `Drawable` and overrides `draw()`.

So these are the key rules around using abstraction in Java through abstract classes and interfaces.

## Difference between abstract class and interface

Abstract class and interface both are used to achieve abstraction where we can declare the abstract methods. Abstract class and interface both can't be instantiated.

But there are many differences between abstract class and interface that are given below.

<table data-full-width="true"><thead><tr><th>Abstract class</th><th>Interface</th></tr></thead><tbody><tr><td>1) Abstract class can <strong>have abstract and non-abstract</strong> methods.</td><td>Interface can have <strong>only abstract</strong> methods. Since Java 8, it can have <strong>default and static methods</strong> also.</td></tr><tr><td>2) Abstract class <strong>doesn't support multiple inheritance</strong>.</td><td>Interface <strong>supports multiple inheritance</strong>.</td></tr><tr><td>3) Abstract class <strong>can have final, non-final, static and non-static variables</strong>.</td><td>Interface has <strong>only static and final variables</strong>.</td></tr><tr><td>4) Abstract class <strong>can provide the implementation of interface</strong>.</td><td>Interface <strong>can't provide the implementation of abstract class</strong>.</td></tr><tr><td>5) The <strong>abstract keyword</strong> is used to declare abstract class.</td><td>The <strong>interface keyword</strong> is used to declare interface.</td></tr><tr><td>6) An <strong>abstract class</strong> can extend another Java class and implement multiple Java interfaces.</td><td>An <strong>interface</strong> can extend another Java interface only.</td></tr><tr><td>7) An <strong>abstract class</strong> can be extended using keyword "extends".</td><td>An <strong>interface</strong> can be implemented using keyword "implements".</td></tr><tr><td>8) A Java <strong>abstract class</strong> can have class members like private, protected, etc.</td><td>Members of a Java interface are public by default.</td></tr><tr><td><p>9)<strong>Example:</strong></p><p><br>public abstract class Shape{<br>public abstract void draw();<br>}</p></td><td><strong>Example:</strong><br>public interface Drawable{<br>void draw();<br>}</td></tr></tbody></table>

Simply, abstract class achieves partial abstraction (0 to 100%) whereas interface achieves fully abstraction (100%).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://qatesting.gitbook.io/qa/java/oops/abstraction.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
