Monday , December 18 2017
Home / JAVA / Abstract class and abstract method in Java

Abstract class and abstract method in Java

This tutorial is to tell you about the abstract class and the abstract method in Java 

Abstract Class :- A class which cannot be instantiated i.e object cannot be created only is called Abstract class.

In other words ,we can say that only we can inherit the abstract class but we cannot create the object of this class to execute the functions.In abstract class we can have the abstract methods as well as simple methods but cannot execute those methods in the abstract class itself.For this we have to inherit the abstract class in the simple class for execution of the methods.

Also See:   Program for Matrix Multiplication in JAVA

Abstract Method :- An abstract method is a method in the abstract class which is not defined in the abstract class.Only the declaration of the functions are defined and end the declaration by semicolon.

And than define  the abstract method implementation after inherit the abstract class in the simple class.Implementation of the abstract methods are compulsory in the derived class.

abstract class Abstract   // *1
{

void Function1()   // simple function
{   // implementation of the function
System.out.println("Simple Function is executing");
}

abstract void Function2();  // declaration of method abstract *2

void Function3()  // simple function
{   // implementation of the function
System.out.println(" Simple function is executing");
}

}   // end of abstract class

class Abstract_Inherit extends Abstract   // *3
{

public static void main(String args[])  // main function
{
Abstract_Inherit obj=new Abstract_Inherit();  // creating the object
obj.Function1(); // calling the function
obj.Function2(); // calling the abstract function
obj.Function3(); // calling the function
} //end of main

void Function2()  // implementation of the abstract methods
{
System.out.println("Abstract method is implemented and executing");
}
} // end of class

*1  – Defining the class abstract which means it can be inherited but cannot be instantiated i.e object cannot be created for the abstract class.We need to inherit this abstract class and use it for the execution of the methods.

Also See:   Program for string in cross x format on given position

*2  – Abstract methods are not implemented in abstract class,they are only defined and than implemented after inheriting the abstract class and than defining the methods in the derived class and execute them after creating the objects in the child class.

*3  – Inheriting the abstract class and than implements all the abstract methods and than execute them using the objects.

OUTPUT:

 

 

abstract class abstract method

Check Also

Java program to implement polymorphism

Java program to implement polymorphism: Polymorphism is a technique in java with which you can ...

Leave a Reply

Your email address will not be published. Required fields are marked *