Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
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:
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:
A class declared abstract may or may not include abstract methods. But, what exactly 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();
Listed below are key features of Abstract Method:
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.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 7th December,2024 7th December SAT&SUN (Weekend Batch) | View Details |
edureka.co