You are currently viewing Java Abstract Class and Abstract Methods

Java Abstract Class and Abstract Methods

In this tutorial, we will learn about Java abstract classes and methods/abstract class in java.

Java Abstract Class

In Java, an abstract class is one that is declared with the abstract keyword. It can include both abstract and non-abstract methods.

In Java, the abstract class cannot be instantiated (we cannot create objects of abstract classes). To declare an abstract class, we use the abstract keyword.

// create an abstract class
abstract class Sample {
// fields and methods
}

// try to create an object Sample
// throws an error
Sample obj = new Sample ();

Regular and abstract methods can both be found in an abstract class. 

abstract class Language {
// abstract method
abstract void method1();
// regular method
void method2() {
System.out.println(“This is regular method”);
}
}

Let’s first learn about abstraction in Java before diving into the Java abstract class.

Java Abstraction

Abstraction is the process of hiding implementation details from the user and displaying only functionality.

  • Abstraction is a key concept in object-oriented programming because it allows us to hide unnecessary details and only display the information that is required.
  • This enables us to manage complexity by omitting or concealing details in favor of a simpler, higher-level concept.
  • Motorcycle brakes are a practical example of abstraction. We understand what brake does. The motorcycle will come to a halt when we apply the brake. The operation of the brake, on the other hand, remains a mystery to us.
  • The primary benefit of concealing the operation of the brake is that the manufacturer can now implement the brake differently for different motorcycles while still performing the same function.

In another way, it shows only the essentials to the user while hiding the internal details, such as when sending SMS, where you type the text and send the message. You are unaware of the internal message delivery processing.

Abstraction allows you to concentrate on what the object does rather than how it does it.

Methods for Achieving Abstraction

In Java, there are two ways to achieve abstraction.

  • The abstract class (0 to 100 percent )
  • Interaction (100 percent )

Java Abstract Method

An abstract method is one that is declared as abstract but does not have any implementation.

abstract void display();

Here, display() is an abstract method. The body of display() is replaced by:

If a class contains an abstract method, then the class should be declared abstract. Otherwise, it will generate an error. For example:


// class should be abstract
class Language {
// abstract method
abstract void method1();
}

Example: Java Abstraction

abstract class Car {
  abstract void brake();
}

class SportCar extends Car {
    
  // implementation of abstract method
  public void brake() {
    System.out.println("SportCar Brake");
  }
}

class SimpleCar extends Car {
    
  // implementation of abstract method
  public void brake() {
    System.out.println("Simple Brake");
  }
}

class Main {
  public static void main(String[] args) {
    SimpleCar b = new SimpleCar();
    b.brake();
    SportCar s = new SportCar();
    s.brake();
  }
}

Output:

Simple Brake
SportCar Brake

  • In the preceding example, we created an abstract super class called Car. The abstract method brake is available in the superclass Car ().
  • Inside Car, the brake() method cannot be implemented. This is due to the fact that each Car has a unique brake implementation. As a result, each subclass of Car would have a unique brake implementation ().
  • As a result, the implementation of brake() in Car is hidden.
  • SimpleCar implements brake() on its own, while SportCar implements brake() on its own ().

Example: Java Abstract Class and Method

In another way, it shows only the essentials to the user while hiding the internal details, such as when sending SMS, where you type the text and send the message. You are unaware of the internal message delivery processing.

  • Although abstract classes cannot be instantiated, they can be subclassed. The object of the subclass can then be used to access members of the abstract class.
abstract class Language {

  // method of abstract class
  public void display() {
    System.out.println("This is Java Programming Language");
  }
}

class Main extends Language {

  public static void main(String[] args) {
    
    // create an object of Main
    Main obj = new Main();

    // access method of abstract class
    // using object of Main class
    obj.display();
  }
}

Output:

abstract class in java, difference between abstract class and interface in java , abstract class and interface in java , in java declaring a class abstract is useful, use of abstract class in java, abstract class and method in java, when to use abstract class and interface in java, abstract class program in java , abstract class syntax in java , why we use abstract class in java

This is Java Programming Language
  • In the preceding example, we created an abstract class called Language. The class includes a standard method for displaying data ().
  • We created the Main class, which inherits from the abstract class. Take note of the statement
obj.display()
  • The object of the child class Main is denoted by obj in this case. Using the object obj, we invoke the method of the abstract class.

Example: Understanding the real-world example of the Abstract class

  • Shape is the abstract class in this example, and the Rectangle and Circle classes implement it.
  • We mostly don’t know anything about the implementation class (which is hidden from the end-user), and the factory method provides an object of the implementation class.
  • A factory method is one that returns the class’s instance. Later on, we’ll learn about the factory method.
  • If you create an instance of the Rectangle class in this example, the draw() method of the Rectangle class will be called.
abstract class Shape{  
abstract void draw();  
}  
//In real example, implementation is provided by others that is unknown by end user  
class Rectangle extends Shape{  
void draw(){System.out.println("drawing rectangle shape");}  
}  
class Circle extends Shape{  
void draw(){System.out.println("drawing circle shape");}  
}  
//In real scenario, method is called by programmer or user  
class Test{  
public static void main(String args[]){  
Shape x=new Circle();//In a real scenario, object is provided through method, e.g., getShape() method  
x.draw();  
}  
}  

Output:

drawing circle
Note: if you are extending an abstract class that has an abstract method, you must either provide the implementation of the method or make this class abstract.

Important points about Java Abstraction

  1. To create abstract classes and methods, we use the abstract keyword.
  2. An abstract method has no implementation (method body).
  3. A class that contains abstract methods should be abstract as well.
  4. We are unable to create objects of an abstract class.
  5. We inherit subclasses from an abstract class and create objects of the subclass to implement its features.
  6. All abstract methods of an abstract class must be overridden by a subclass. Overriding abstract methods is not required if the subclass is declared abstract.

You may like:

Instanceof Java with Example

Java final Keyword with Example

Hope this article will guide you to recognize all about the abstract class in java that you needed and still if you have any problem or queries regarding this, post them in the comments section and we will be glad to assist you.

Leave a Reply