Jobs
Blogs
Courses
Mentorship
Partners
For Employers
e2e logo

Abstract Methods and Classes In Java

mac

Abstract Methods and Classes

An abstract class is a class that has been explicitly designated as such; it may or may not have abstract methods. Although abstract class objects cannot be created, they may be subclassed.

An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:

abstract void add(int X, int Y);



1. Vehicle Class: The abstract method "start" is the only one present in the "Vehicle" class.
     The implementation is provided by the Car and Bike class.
Hello


2. Car Class: Extends Vehicle, which has the abstract method start.

     The car class overrides the method start because cars start with buttons.                                            


3. Bike class: extends Vehicle (an abstract class) and overrides the abstract method start ().

    Since a bike starts with a kick and a button, the Bike class overrides the start() method.


4. Display class: The Display class has a method called "print" that includes a vehicle reference. As a variable 


5. Main class: Car and bike objects were created in the main class.

Here, we make a new object called Display.

     The print method is referenced by the passing car and bike.


6. Output

Points to Remember

  • An abstract keyword must be used when declaring an abstract class.
  • Both abstract and non-abstract approaches are possible.
  • You can't instantiate it.
  • Both static methods and constructors are possible.
  • It may contain final methods that compel the subclass not to alter the method body.