Abstract Class In Java

abstract class in java

Abstract class

In Java, an abstract class is one that has the abstract keyword declared in its declaration. An abstract class is a restricted class that cannot be used to create objects. Both abstract and non-abstract methods are possible.

To know more about abstract Class in java read the complete article.

 

Abstraction in Java

Data Abstraction is the process of withholding some information from the user and only displaying what is absolutely necessary. In a different way, it only displays the user’s needs and hides internal information, such as when sending SMS, where you type the text and send the message. You are unaware of the internal message delivery processing.
 

Important Points To Be Remember :

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

Syntax :

public abstract class GraphicObject {
   // declare fields
   // declare non abstract methods
   abstract void draw();
}

Abstract Method in Java

An abstract method is one that has been declared abstract and does not have an implementation.

Example :

public abstract class Student {
   private String name;
   private String address;
   private int number;
   
   public abstract double RemaingFees();
   // This is abstract method
}

Code Example for Abstract Class :

Run

abstract class vehical {
   
    public abstract void start();
    public abstract void stop();
    public abstract void accelerate();
    public abstract void brake();

    }   

class car extends vehical 
    {
    public void start() {
        System.out.println("Car started");
    }

    public void stop() {
        System.out.println("Car stopped");
    }

    public void accelerate() {
        System.out.println("Car accelerated");
    }

    public void brake() {
        System.out.println("Car braked");
    }
}

class bike extends vehical 
    {
    public void start() {
        System.out.println("Bike started");
    }

    public void stop() {
        System.out.println("Bike stopped");
    }

    public void accelerate() {
        System.out.println("Bike accelerated");
    }

    public void brake() {
        System.out.println("Bike braked");
    }
}
public class Main {
    public static void main(String[] args) {
        car c = new car();
        bike b = new bike();
        c.start();
        c.stop();
        c.accelerate();
        c.brake();
        b.start();
        b.stop();
        b.accelerate();
        b.brake();
    }
}

Output :

Car started
Car stopped
Car accelerated
Car braked
Bike started
Bike stopped
Bike accelerated
Bike braked

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription