DEV Community

Isaac Tonyloi
Isaac Tonyloi

Posted on • Edited on

Abstraction in Java.

Laptop showing Java Code
Photo by Luca Bravo on Unsplash

In java a class is considered to be abstract if it cannot be instantiated, abstract classes are similar to interfaces. An abstract class may include an abstract method or not. If a class includes an abstract method then it must be labeled abstract. An abstract class can also contain regular methods alongside the abstract methods.
A method is considered to be abstract if it is declared without implementation. We use the abstract keyword to create an abstract as shown here.

abstract Division {
    //methods and fields of the class
    abstract void method1();

    public void method2() {
        System.out.println("This method is regular")
    }
}


Enter fullscreen mode Exit fullscreen mode

Creating and Implementing Abstract methods.
Abstract methods are equally created using the abstract keyword. Unlike regular methods, abstract methods do not have a body. However, since abstract classes can be inherited we can use subclasses to access methods within the abstract classes.

abstract class Division {
    public void description() {
        System.out.println("Software and Network Engineers work in the Tech division");
    }
}

class Main extends Division {
    public static void main(String[] args) {
        Main obj = new Main();
        //accessing methods in the regular class
        obj.description();

    }
}
Enter fullscreen mode Exit fullscreen mode

In the above example we have created an abstract class with a regular method that we have then gone ahead to access through the subclass.
Using the same concept of Inheritance we can also create subclasses that Implement abstract methods that are created within an abstract class. It is required that the subclass should implement any abstract method created within the abstract class.
For example,

abstract class Birds {
    abstract void fly();

    public void eat() {
        System.out.println("All birds do eat");
    }

}

class Ostrich extends Birds {
    public void fly() {
        System.out.println("Ostrich do not fly");
    }
}

class Main {
    public static void main(String[] args) {

        Ostrich obj = new Ostrich();

        obj.fly();
        obj.eat();


    }
}
Enter fullscreen mode Exit fullscreen mode

Sample Output

Ostrich do not fly
All birds do eat
Enter fullscreen mode Exit fullscreen mode

In the example above the class Ostrich extends the abstract class Birds and also provides implementation for its abstract method fly().

Accessing Constructors in the Abstract Class.
Besides regular and abstract methods an abstract class can also contain its own constructors. Abstract classes can have both no-arg and parameterized constructors.

In this case we can use a special form of the super keyword i.e super() inside the constructor of a subclass that extends from the abstract class to access the constructor inside the abstract. However it is mandatory that the super() keyword should be the first statement in the constructor of the subclass. As shown here.

abstract class Birds {
    //constructor
    Birds() {
        //some code
    }

}

class Ostrich extends Birds {
    //subclass constructor
    Ostrich() {
        super();
        //some other code
    }

}

Enter fullscreen mode Exit fullscreen mode

Significance of Abstraction.

In java abstraction enables us to hide unnecessary details from the user and only show the necessary information, hence building more secure programs. Abstraction is a concept under object oriented programming since it makes heavy use of the concept of Inheritance.
Abstract classes allow us to achieve partial abstraction since abstract classes can still contain regular methods. However to achieve full abstraction we then need to make use of Interfaces that only contain abstract methods whose Implementation should also be provided.

Thank you for Reading/ skimming through 😊.
Connect with Me: Isaac Tonyloi.

Top comments (0)