Java Abstraction

Abstraction is a process of hiding the implementation details from the user, only the functionality will be provided to the user. In other words, the user will have the information on what the object does instead of how it does it. In Java programming, abstraction is achieved using Abstract classes and interfaces.

Example: ATM GUI, Mobile GUI, Windows GUI etc...


Let’s take, ATM GUI (Graphical User Interface) screen.

Bank people are highlighting the set of services what they are offering without highlighting internal implementation.

The main advantages of Abstraction are:

  1. 1. We can achieve security as we are not highlighting our internal implementation. (i.e., outside person doesn't aware our internal implementation.)
  2. 2. Enhancement will become very easy because without effecting end user we can able to perform any type of changes in our internal system.
  3. 3. It provides more flexibility to the end user to use system very easily.
  4. 4. It improves maintainability of the application.
  5. 5. It improves easiness to use our system.
How to achieve Abstraction?

There are two ways to achieve abstraction in java

  1. Abstract class (0 to 100%)
  2. Interface (Achieve 100% abstraction)

Abstract Classes and methods In Java

abstract is a keyword, which is applicable to classes and methods. If we declare a class as abstract then will call it as a abstract class and if a declare a method as abstract then will call it as a abstract method, these methods does not have any implementation. Using abstract class we cannot achive 100% abstraction. abstract classes will have both abstract and concret methods.
abstract method: A methods doesn't have the body. Its implementation will be in child classes.
concret method: These are the normal methods which contain the body.
we cannot instantiation abstract classes, if we want to use the methods available in abstract class, through child class only will be able to access it.

Some important points about abstract classes and methods
  1. 1. Java abstract classes may or may not contain abstract methods, i.e., methods without body ( public void get(); )
  2. 2. But, if a class has at least one abstract method, then the class must be declared abstract.
  3. 3. If a class is declared abstract, it cannot be instantiated.
  4. 4. To use an abstract class, you have to inherit it from another class, provide implementations to the abstract methods in it.
  5. 5. If you inherit an abstract class, you have to provide implementations to all the abstract methods in it.
  6. 6. abstract keyword is used to declare the method as abstract.
  7. 7. You have to place the abstract keyword before the method name in the method declaration.
  8. 8. An abstract method contains a method signature, but no method body.
  9. 9. Instead of curly braces, an abstract method will have a semoi colon (;) at the end.

Example
    
        // Class 1
        package com.java.session.six;

        public abstract class AbstractExamples {
            public void display() { // normal method
                System.out.println("display method");
            }
            public abstract String absMethod(); // abstract method
        }

        // Class 2
        package com.java.session.six;

        public class Examples extends AbstractExamples {
            public void test() {
                display();
            }
            public static void main(String[] args) {
                Examples e = new Examples();
                e.test();
                String str = e.absMethod();
                System.out.println(str);
            }
            @Override
            public String absMethod() {
                return "Overridden abstract method";
            }
        }
    
Interfaces in Java

Java interface is a collection of abstract methods. The interface is used to achieve 100% abstraction in which you can define methods without their implementations. Inside the interface will have only abstract methods. Inside the interfaces all the methods by default public and abstract it means we no need to specify the methods as public abstract. We cannot instantiate interfaces. Thorugh child class implementation we can access the methods of an interface. Inside interfaces we can also declare variables, by default all the variables inside the interfaces are constants (static fianl).

Declaring an Interface

    public interface NameOfInterface {
    // Any number of final, static fields
    // Any number of abstract method declarations\
    }


    interface Animal {
        public void eat();
        public void travel();
        }

Implementing Interfaces

If we want to use methods inside the interface, we need to implement the interface so that we can access all the data members. implements is a keyword which only used to implement the interfaces in the java program. Two interfaces cannot implement it can only extends. See below examples

Two interfaces can only extends

    interface Car {
        public void speed();
        public void insurance();
        }

        interface Bike extends Car {
        public void speed();
        }

Class and interface can only implements

    interface Animal {
        public void eat();
        public void travel();
        }

        class Mamals implements Animal {
        @Overrides
        public void eat() {
            System.out.println("Mamal is eating");
        }

        @Overrides
        public void travel() {
            System.out.println("Mamal will change it location bases on the weather conditions");
        }
        }