How to Java programming- Abstract Methods & Classes

In this guide, we are walking through the Abstract methods & Classes. Abstract classes are classes that contain one or more abstract methods.


Class Syntax
      abstract class class_name { }


A method that is declared without a body within an abstract class is called an abstract method. The method body will be defined by its subclass. An abstract method can never be final and static. Any class that extends an abstract class must implement all the abstract methods declared by the super class.


Abstract Method Syntax
     abstract return_type function_name ();    // No definition


Abstract keyword is used to denote both an abstract method and an abstract class

In below example, Bike the abstract class that contains only one abstract method run. Its implementation is provided by the Honda class.

	abstract class Bike{  
	  abstract void run();  
	class Honda4 extends Bike{  
	  void run(){System.out.println("running safely..");}  
	  public static void main(String args[]){  
		 Bike obj = new Honda4();;  


     running safely..


Abstract classes are not Interfaces. They are different, we will study this when we will study Interfaces.
An abstract class may or may not have an abstract method. But if any class has even a single abstract method, then it must be declared abstract.
Abstract classes can have Constructors, Member variables, and Normal methods.
Abstract classes are never instantiated.
When you extend Abstract class with an abstract method, you must define the abstract method in the child class, or make the child class abstract.


Anwar Yakkiparamban

Anwar Yakkiparamban is the founder of Lauyou Learning. Prior to Lauyou learning, Anwar worked at ARD Engineering & Development, Qatar. He holds bachelor degree in Electronics and Communication Engineering from Govt. Engineering College Idukki.

You may also like...