Abstraction in Java

Get introduced to what abstraction is in Java.

What is abstraction?

Showing only essential information and hiding unnecessary information is called abstraction. For example, when we use a cell phone, all we are seeing is the screen. We don’t need to know what’s happening behind the screen.

Abstract class

An abstract class is a class that declares one or more abstract methods. Such a class can have abstract methods as well as concrete (normal) methods, whereas a normal class cannot have any abstract methods. In Java, an abstract class cannot be instanced, which means that an object of an abstract class can’t be created.

Abstract method

An abstract method is a method that has the method definition but does not contain implementation. An abstract method has no body. It should be declared in an abstract class.

Implementation

Java provides the abstract keyword to implement the abstraction. The abstract keyword can only be used in an abstract class and will cause an error if used in a regular class. Here is an example of an abstract class:

public abstract class myClass
{
    //only abstract class can have abstract methods

    abstract void method1();
  
    abstract void method2();
}

Look at the example below.

Get hands-on with 1400+ tech skills courses.