OOP(s): The Abstraction

OOP(s): The Abstraction

We just gotta know how it works or what functionalities it offers, rather than how it is implemented internally.

Scenario

abstarction_example_1.gif

Who doesn't love a pay-day! But all we do is

  • Go to an ATM,
  • Insert an ATM card,
  • Enter the amount,
  • Enter the pin,
  • And collect money.

But do we need to know how those things are implemented internally, I mean

  • Do we need to know how a machine reads a card?
  • Do we need to know how the money is counted?
  • Do we need to know how a receipt is printed after the transaction?
  • And many more...

The manufacturers took the ache of the internal implementation of those things and provided us with the functionalities. And this is called Abstraction.

Abstraction: The Definition

picture-abstract-class-UML-Diagram.gif Abstraction is the practice of concealing an application's internal information from the outside world. Abstraction is used to describe things in simple terms. It’s used to create a boundary between the application and the client programs.

In OOP(s) way

Objects are the foundation of Object-Oriented Programming. An object is made up of attributes and methods. We can conceal them from the outside world by using access modifiers. We can only provide the other applications access to the necessary functions and characteristics. This is the general method for implementing abstraction in OOPS.

Note: We can achieve abstraction either by using an abstract class or by using an interface.

An Abstract Method

An abstract method is one that only contains the method declaration and no implementation. An Abstract Method is a method that does not have a body. It must be declared in a class that is abstract. Because the abstract class must implement all of the abstract methods, the abstract method will never be final.

An Abstract Class

In OOPs, an Abstract Class is a type of class that declares one or more abstract methods. These classes can have both abstract and concrete methods. Abstract methods are not permitted in a regular class. An abstract class is one that includes at least one abstract method. And we cannot create objects for an abstract class.

Abstract Class: The Syntax

abstract class_name{
    abstract thisIsAAbstractMethod(){};
    void thisIsNormalMethod(){
        System.out.println("This is normal method inside abstract class");
    }
}

Abstraction: The Example

Let us assume, that we are a manufacturer of X scooters, and we have a different display deck for different versions of X scooters. But how do we achieve it? By using an abstract class. We can implement an abstract class with an abstract method, which we will be implementing for different versions of X scooters.

public abstract class DisplayDeck{
    public void abstract displaySpeedDeck(){};
    public void abstract setMaxSpeed(int maxSpeed){};
    public void displayCompany(){
        System.out.println("X-Scooters");
    }
}

Here we can see that we have two abstract methods(as of now) for the DisplayDeck class. From now on for each version, we have to extend this abstract class and implement these two methods.

Version V1X1

public class VersionV1X1 extends DisplayDeck{
      @Override
      public void displaySpeedDeck(){
          System.out.println("Display Deck: Version V1X1");
      }

      @Override
      public void setMaxSpeed(int maxSpeed){
          this.maxSpeed = maxSpeed;
      } 
}

Version VEXN

public class VersionVEXN extends DisplayDeck{
      @Override
      public void displaySpeedDeck(){
          System.out.println("Display Deck: Version VEXN");
      }

      @Override
      public void setMaxSpeed(int maxSpeed){
          this.maxSpeed = maxSpeed;
      } 
}

An Interface

interface_resource.png In Java, an interface is similar to Java classes. The main distinction is that an interface contains empty methods (methods without method implementation) and variables. In other terms, it is a set of abstract methods (methods without a method body) and static constants. The important point about an interface is that each method is public and abstract and does not contain any constructor. Along with the abstraction, it also helps to achieve multiple inheritance. The implementation of these methods is provided by the clients when they implement the interface.

Note: A Java interface contains static constants and abstract methods.

Interface: The Syntax

public interface interface_name{
    public void method1();
    public void method2();
}

Interface: The Example

Assume we have two interfaces CarStart, CarStop. These two interfaces will be implemented in their own implementation in class Car.

interface CarStart {
    void start();
}
interface CarStop {
    void stop();
}
public class Car implements CarStart, CarStop {
    public void start() {
        System.out.println("The car engine has been started.");
    }
    public void stop() {
        System.out.println("The car engine has been stopped.");
    }
    public static void main(String args[]) {
        Car c = new Car();
        c.start();
        c.stop();
    }
}

Abstraction Vs. Encapsulation

Screenshot from 2022-08-01 21-21-16.png More on GFG

Finally

Abstract classes provide semi-abstraction, and Interfaces provide 100% abstraction.