Java abstract class | Examples, constructor, default method

What is Abstract Class in java?

In a java which 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.

Java abstract class | Examples

A point of Java abstract class

Abstraction is one of the major features of 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 calss have any Abstract method (Without body) should be declared with Abstract Keyword.
  • Can have constructors, static methods and final methods.

Syntax

A simple syntax how to declare abstract class.

And abstract method syntax is:-

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.

Output: Draw Rectangle

Abstract Methods

An abstract method has no body, only a name of the method and “END” it with a semicolon’s.

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

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

AbstractionEx.java

Output:

Java Abstract class constructor Data member method

How to Java abstract class implements 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 interface.

interface: – Walk

Abstract class:- Animal implemented Walk interface

Main class for execution of code:- with Concrete Cat and dog methods.

Output: Meow
bark

Note: Interface methods are all public by default, even if the public keyword is not present (used). Classes that implement the interface must make all their implemented methods public, and the public keyword must be present.

Q: What is the purpose of the Abstract class in Java?

Answer: Mainly Abstract class is used for achieving an Abstraction. This 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.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.