What is Abstract Class in java?
A Java class is declared with Abstract Keyword called a “Java Abstract Class“. An abstract class can have Abstract methods and no abstract method or both. This class can’t be instantiated, it should be extended and its method implemented.
A point of Java abstract class
Abstraction is one of the major features of the OOP concept. Abstraction is a process of hiding the implementation details and handles complexity from the user, only main and usable functionality provided to the user.
For Example, A car only has the main option give to the user to control it. Like Break, Gear, Steering, accelerator, etc. Not all details how is internally working.
Let’s see the Points to Remember:-
- Abstract classes must be declared with Abstract Keyword.
- Java Abstract class can’t be instantiated.
- To use an abstract class in java, need to inherit it from another class and implement the abstract methods in it.
- A inherit an abstract class, need to provide implementations to all the abstract methods in it.
- Abstract classes can contain abstract methods or not (normal methods).
- If a class has any Abstract method (Without body) should be declared with Abstract Keyword.
- Can have constructors, static methods, and final methods.
Syntax
A simple syntax of how to declare an abstract class.
abstract class ClassName{ }
Abstract method syntax is:-
abstract void MethodName();
Java abstract class example Code
Let’s see an example, where an abstract class name “ObjectSahpe” and have one abstract method (without body method) with the name “draw()”
A shape class is to have the main method to access this program. It’s a very simple and basic example of abstraction in java.
package a.b; abstract class ObjectShapes { abstract void draw(); } class Rectangle extends ObjectShapes{ @Override void draw() { System.out.println("Draw Rectangle"); } } class Shape{ public static void main(String args[]){ ObjectShapes rect1=new Rectangle(); rect1.draw(); } }
Output: Draw Rectangle
Abstract Methods
An abstract method has no body, only a name of the method and “
public abstract void sleep();
This method must be overridden to use.
Note: You can’t have an abstract method in a non-abstract class. And you have to implement all abstract methods.
Java Abstract class constructor | Data member | More methods
An abstract class can have:
- Abstract method
- Non-abstract method (method body)
- Constructor
- Data member
- Java main() method.
Let’s see the example of class cantering data members and methods.
Animal.java
abstract class Animal { // declare data member String objectName = " "; //constructor Animal(String name) { objectName = name; System.out.print(name + "Animal created "); } //constructor Animal() { System.out.println("Animal created "); } // normal method public void sleep() { System.out.println("Animal sleeping time"); } // normal method public void eat() { System.out.println("Animal eat something"); } // Abstract method public abstract void makeNoise(); } class Dog extends Animal { Dog (String name){ super(name); } public void makeNoise() { System.out.println("Bark! Bark!"); } } class Cow extends Animal { Cow (String name){ super(name); } public void makeNoise() { System.out.println("Moo! Moo!"); } }
AbstractionEx.java
public class AbstractionEx { public static void main(String args[]){ //cow object Cow cow = new Cow("Cow "); cow.makeNoise(); cow.sleep(); cow.eat(); //Dog object Dog dog = new Dog("Dog "); dog.makeNoise(); dog.sleep(); dog.eat(); } }
Output:
How to Java abstract class implement interface?
Answer: A class can only inherit from one class but can implement multiple interfaces in Java.
An abstract class is very similar to an interface. The main difference is that an abstract class can define some function already, an interface can’t (note that this changed in Java9+).
Now coming to answer you have to use an “implement” keyword to implements the interface.
interface: – Walk
package abs.intr; interface Walk { String walk(); }
Abstract class:- Animal implemented Walk interface
package abs.intr; abstract class Animal implements Walk { public abstract String MakeNoise(); }
The main class for the execution of code:- with Concrete Cat and dog methods.
package abs.intr; public class MainClass { public static void main(String args[]){ Animal catObj = new Cat(); System.out.println(catObj.MakeNoise()); Animal dogObj = new Dog(); System.out.println(dogObj.MakeNoise()); } } class Cat extends Animal { public String MakeNoise() { return "Meow"; } @Override public String walk() { return "cat is walking"; } } class Dog extends Animal { @Override public String walk() { return "Dog is walking"; } @Override public String MakeNoise() { return "bark"; } }
Output: Meow
bark
Note: Interface methods are all public by default, even if public
public
Q: What is the purpose of the Abstract class in Java?
Answer: Mainly Abstract class is used for achieving an Abstraction. These classes are used to provide common functionality across a set of related classes while also allowing default method implementations.
Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1
Java version 11
All Abstract class in Java Examples are in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.