Java/J2EE and SOA (348 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (4 Blogs)
  • Java/J2EE and SOA (345 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

What is the Use of Abstract Method in Java?

Last updated on Jun 17,2021 13K Views

8 / 14 Blog from Java OOPS

In any programming language, abstraction means hiding the irrelevant details from the user to focus only on the essential details to increase efficiency thereby reducing complexity. In Java, abstraction is achieved using abstract classes and methods. Let’s get to know more about the abstract method in Java.

The topics discussed in this article are as follows:

What is an Abstract Class?

In any programming language, a class which is declared with the keyword abstract is known as an abstract class. An abstract class captures common characteristics of subclasses and may or may not contain any abstract method. It cannot be instantiated but can be only used as a superclass by its subclasses.

Listed below are some key points regarding abstract class:

  • An abstract class can have constructors and static methods
  • It can have final methods, they force the subclass not to change the body of the method
  • You can use an abstract class by inheriting it from another class and then provide implementations to the abstract methods in it
  • If an abstract class doesn’t have any method implementation, it’s always better to use interface

A class declared abstract may or may not include abstract methods. But, what exactly is an abstract method?

What is an Abstract Method?

A method declared without a body (no implementation) within an abstract class is an abstract method. In other words, if you want a class to contain a particular method but you want the actual implementation of that method to be determined by child classes, then you can declare the method in the parent class as an abstract.

This is how an abstract method looks in Java:

abstract public void habitat(); 

Key Features of Abstract Method

Listed below are key features of Abstract Method:

  • Abstract methods don’t have an implementation (body), they just have method signature as shown in the above example
  • If a class has an abstract method it should be declared abstract, the vice versa is not true
  • Instead of curly braces, an abstract method will have a semicolon (;) at the end
  • If a regular class extends an abstract class, then the class must have to implement all the abstract methods of that class or it has to be declared abstract as well

Example Program: Abstract Method in Java

Check out the example program to understand how abstraction is achieved using abstract classes and abstract methods. Do take a look.

package MyPackage;
//abstract class
abstract class Animal{
	 String AnimalName = " "; 
     
	    Animal(String name) 
	    { 
	        this.AnimalName = name; 
	    } 
	      
	    // declare non-abstract methods 
	    // it has default implementation 
	    public void BasicInfo(String details) 
	    { 
	        System.out.println(this.AnimalName + " " + details); 
	    } 
	      
	    // abstract methods which will be 
	    // implemented by its subclass(es) 
	    abstract public void habitat(); 
	    abstract public void respiration(); 
	} 
class Terrestrial extends Animal 
{ 
   
      
    // constructor 
    Terrestrial(String name) 
    { 
        super(name);
    } 
      
    @Override
    public void habitat()  
    { 
        System.out.println("leave on land and");  
    } 
      
    @Override
    public void respiration()  
    { 
    	System.out.println("respire through lungs or trachea. ");  
    }
}
class Aquatic extends Animal 
{ 
      
    // constructor 
    Aquatic(String name) 
    { 
        super(name);
    } 
      
    @Override
    public void habitat()  
    { 
        System.out.println("It leaves in water and");  
    } 
      
    @Override
    public void respiration()  
    { 
    	System.out.println("respire through gills or their skin. ");  
    }
}


class AbstractClassDemo
{ 
    public static void main (String[] args)  
    { 
      
        // creating the Object of Terrestrial class 
        // and using Animal class reference. 
        Animal object1 = new Terrestrial("Humans"); 
        object1.BasicInfo("are terrestrial beings, they "); 
        object1.habitat(); 
        object1.respiration(); 
         
          
        System.out.println(" "); 
          
        // creating the Objects of circle class 
        Animal object2 = new Aquatic("Fishes"); 
        object2.BasicInfo("are aqautic beings, they "); 
        object2.habitat(); 
        object2.respiration(); 
        
    } 
} 

Output:

The method BasicInfo() is a concrete method which is used by both Terrestrial and Aquatic classes. The methods habitat() and respiration() are abstract methods and they do not have any implementation, just the signature. Terrestrial and Aquatic classes have to provide their own implementation for both of these methods. Also, notice that both methods begin with the keyword abstract. At this point, you might be wondering how is abstract class different from the interface.

Interface in Java 

Another way of achieving abstraction in Java is by using interfaces. An interface is a collection of abstract methods, it does not have any concrete methods, unlike an abstract class. But unlike abstract class, an interface provides full abstraction in Java. It can have both methods and variables just like a class. However, the methods declared in an interface are abstract by default.

Abstract classes and interfaces are the two main building blocks of the Java Programming Language. Though both are primarily used for abstraction, they are very different from each other and cannot be used interchangeably. 

This brings us to the end of this ‘Abstract Method in Java’ article. I have covered one of the most frequently asked Java Interview Questions, which is an abstract class in Java.

Make sure you practice as much as possible and revert your experience.  

Check out the Java Course Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Got a question for us? Please mention it in the comments section of this ‘Abstract Method in Java’ article and we will get back to you as soon as possible.

Upcoming Batches For Java Certification Training Course
Course NameDateDetails
Java Certification Training Course

Class Starts on 27th April,2024

27th April

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

What is the Use of Abstract Method in Java?

edureka.co