^

Abstract Method

The method declared but not implemented is called an abstract method. In other words, the method does not have body but terminates with a semicolon. The keyword abstract must be used in the header of an abstract method.
Generally, abstract methods are used to force the subclasses to provide the implementation by defi ning their body.
An abstract method has the following syntax.
real java abstract method
The final and static methods can not be declared abstract. Only instance methods can be declared abstract.

Abstract Class

A class containing abstract method is called an abstract class.Such class must have keyword abstract in its header, otherwise, compiler gives an error message. An abstract class has the following syntax:
    abstract class name
    {  --------
      ------- 
    } 
An abstract class can not be instantiated. It can be used for inheritance only.
Generally, abstract methods are used to force the subclasses to provide the implementation by defi ning their body. If the sub class does not implement the abstract methods of super class, it will also be treated as an abstract class which can not be instantiated. A class without any abstract method is called concrete class. Concrete just means a non-abstract class.
A class without any abstract method can also be declared using keyword abstract.It means that an abstract class does not necessarily have abstract methods.
Implementation: (Abstract method and class)
The following program shows an abstract class:Salary, consisting of an abstract method:calculate(). The classes:Monthly and Hourly, which are subclasses of Salary , override calculate() method to calculate the payable amount.
     abstract class Salary //abstract class
	    {
		int perMonth=500000;
		int perHour=2000,amount;
		abstract void calculate(); //abstract method
	    }
class Monthly extends Salary
	   {
		int noOfMonths;
		Monthly(int nom)
		      {
			noOfMonths=nom;
		      }
		void calculate() //method overriding 
		      {
			int amount=noOfMonths*perMonth;
		        System.out.println(“No. Of Months: “+noOfMonths);
			System.out.println(“Total Amount:”+amount);
		      }
	   }
class Hourly extends Salary
            {
		int noOfHours;
		Hourly(int h)
		    {
			noOfHours=h;
		    }	
void calculate() //method overriding
		{
			int amount=noOfHours*perHour;
			System.out.println(“No. Of Hours: “+noOfHours);
			System.out.println(“Total Amount:”+amount);
		}
      }
public class AbDemo 
     {
	public static void main(String[] args) 
	   {
		Hourly h=new Hourly(1000);
		h.calculate();
		Monthly m=new Monthly(6);
		m.calculate();
	   }
    }
      Output:  No. Of Hours: 1000
               Total Amount:2000000
               No. Of Months: 6 
               Total Amount:3000000
      

Difference Between Abstract Class And Concrete Class

    Classes can be either concrete or abstract depending on the level of implementation of their method functionalities. Some diff erences between them are listed below:
  • The default class is a concrete class, it doesn’t use any additional keyword, whereas, an abstract class is declared using keyword abstract.
  • A concrete class completely implements all its methods, whereas an abstract class can have abstract methods and concrete methods.
  • Unlike concrete classes, abstract classes cannot be instantiated.
  • A class derived from concrete class may override any, none or all methods but if it is derived from abstract class, then it must override the abstract methods, otherwise it will also become an abstract class.

About the Author
Rajesh K. Bansal (SCJP-Sun Certified Java Programmer)
23 Years experience in Training & Development. Founder of realJavaOnline.com, loves coding in Java(J2SE, J2EE), C++,PHP, Python, AngularJS, Android,MERN Stack(MongoDB,Express,ReactJS,NodeJS). If you like tutorials and want to know more in depth about Java , buy his book "Real Java" available on amazon.in.
#Email : bcebti@gmail.com #Contact : 98722-46056
Available on Amazon
Card image cap
Under the guidance of Founder & Author of "realJavaOnline.com". M:9872246056
Card image cap