Java example of Abstract class and abstract methods and declared abstract

7/25/2021

Java Abstract Class Example: Car and Maruti Implementation

Go Back
Java Abstract Class Example: Car and Maruti Implementation

 

Java Example of Abstract Class and Abstract Methods

Understanding Abstraction in Java

Abstraction is a fundamental concept in object-oriented programming (OOP) that hides the implementation details and exposes only the essential functionalities to the user. It helps in achieving a clean and modular code structure.

Abstraction is a process of hiding the implementation details and showing only functionality to the user.

below is key point to remaimber when you creating abstract class.

  • An abstract class is a class that is declared with abstract keyword.
  • An abstract method is a method that is declared without implementation.
  • An abstract class may or may not have all abstract methods. Some of them can be concrete methods.
  • A method defined abstract must always be redefined in the subclass, thus making overriding compulsory OR either make subclass itself abstract.
  • Any class that contains one or more abstract methods must also be declared with abstract keyword.
  • There can be no object of an abstract class. That is, an abstract class can not be directly instantiated with the new operator.
  • An abstract class can have parameterized constructors and default constructor is always present in an abstract class.

Example: Abstract Class in Java

Below is an example demonstrating abstract classes and methods in Java. Here, we define an abstract class Car and implement its methods in the subclass Maruti.

Abstract Class: Car

package com.developer;

abstract class Car {
    int reg_no;

    // Constructor
    Car(int reg) {
        reg_no = reg;
    }

    // Abstract methods
    abstract void price(int p);
    abstract void braking(int force);
}

Concrete Subclass: Maruti

package com.developer;

class Maruti extends Car {
    Maruti(int reg_no) {
        super(reg_no);
    }

    // Implementing abstract methods
    void price(int p) {
        System.out.println("This shows the price of Maruti car.");
    }

    void braking(int force) {
        System.out.println("Maruti cars use hydraulic brakes.");
    }

    public static void main(String args[]) {
        Maruti m = new Maruti(10);
        m.price(7);
        m.braking(9);
    }
}

Output of the Program

This shows the price of Maruti car.
Maruti cars use hydraulic brakes.

Conclusion

In this article, we explored how to implement an abstract class in Java using the Car example and its subclass Maruti.

Using abstraction, we can define common characteristics in a base class and enforce implementation in subclasses, leading to better code maintainability and reusability.

For more Java tutorials, follow us on Instagram and Facebook!

Key Points About Abstract Classes in Java

  • An abstract class is declared using the abstract keyword.
  • An abstract method is a method without an implementation.
  • An abstract class may contain both abstract and concrete methods.
  • If a class contains one or more abstract methods, it must be declared as an abstract class.
  • Any subclass of an abstract class must override all abstract methods or be declared abstract itself.
  • An abstract class cannot be instantiated directly.
  • An abstract class can have constructors, including parameterized constructors.

Table of content