Abstract Class and Method in Java with Example

A class that is declared with an abstract keyword is called an abstract class. All the abstract methods may or may not be present in the abstract class. Before going into depth about abstract class, let’s first deal with the concept of abstraction in Java.

Abstraction in Java

Abstraction is the process of keeping the internal details a secret and only making the functionality visible to the user. It lets the user understand the function of the object rather than how the object performs its function.

Only the important things are shown to the user and the internal details are hidden. It is the quality of dealing with only ideas, not the events by keeping the implementation details hidden. The user won’t see the non-essential and irrelevant details and only deal with the required characteristic of an object through abstraction in Java.

Abstraction helps to reduce the complexities of viewing the codes and can increase the reusability of code as well. It helps to avoid code duplication and increase the security of the program since only the essential parts are made visible to the user.

Methods to achieve abstraction in Java

There are two methods to achieve abstraction in Java. They are:

  • Abstract class
  • Interface

Abstract class in Java

An abstract class is such a type of class that is declared with an abstract keyword. There may or may not be the presence of all the abstract methods in the abstract class.

There can also be the presence of concrete methods instead of the abstract methods. An abstract class cannot be directly instantiated. In other words, an abstract class does not have an object.

A class that has at least one abstract method must be declared as an abstract class. There is always a default constructor present in an abstract class and it can also include a parameterized constructor. It can be used for some implementation of the interface. An abstract class must be inherited from another class so that it can be used.

syntax:

abstract class class_name {
}

Abstract method

A method that is declared without an implementation is called an abstract method. Such type of method should always be redefined in the child class.

During the method declaration, the abstract keyword must be placed before the method name. An abstract keyword is used to declare a method in the superclass as an abstract method when the programmer wants a class to contain a specific method but wants the actual implementation of that method to be determined by the subclass.

It only has method signature but not a method body. Semi-colons are used instead of curly brackets.

syntax:

abstract return_type function_name ();

Abstract class having constructor, methods, data members, and fields

An abstract class can have an abstract method, data member, fields, method body, main method, and constructor. Here is a simple example:

First class: Bank.java

package com.javafolder.abstraction;

public abstract class Bank {

	abstract void money();
}

Second class: SavingAccount.java

package com.javafolder.abstraction;

public class SavingAccount extends Bank {

	@Override
	void money() {

		System.out.println("Saving amount: $10,000");

	}
}

Third class: CheckingAccount.java

package com.javafolder.abstraction;

public class CheckingAccount extends Bank {

	@Override
	void money() {
		System.out.println("Checking amount: $15,000");
	} 
}

Fourth class: TestClass.java

package com.javafolder.abstraction;

public class TestClass {

	public static void main(String[] args) {

		SavingAccount sa = new SavingAccount();
		sa.money();

		CheckingAccount ca = new CheckingAccount();
		ca.money();
	}
}

Interface in Java

An interface is a blueprint of a class that is used to achieve abstraction in Java. An interface contains abstract methods, static methods, constants, default methods, and nested types.

It has methods and variables but the methods that are declared in interface only contain a method signature, not the method body. It represents an IS-A relationship and cannot be instantiated like an abstract class.

All methods of the interface need to be defined in the class unless the class is an abstract class. An interface contains behavior that a class implements and writing it is the same as writing a class. It specifies what a class needs to do but now the class needs to do it. It is related to capabilities. An interface can further extend to several interfaces.

Usage of Java interface

The main uses of an interface in Java are:

  1. Through the use of interfaces, abstraction is achieved in Java and is implemented.
  2. An interface can be used to support the functionality of multiple inheritance which is not supported in case of class.
  3. Loose coupling can be achieved using interfaces.

How to declare an interface?

An interface is declared by the use of interface keyword. Similarly, an implements keyword is used to implement the interface. Since all the methods in an interface are declared with an empty body and are public, it helps to achieve total abstraction in Java.

All the fields are public, final, and static by default in an interface. All the methods declared in the interface must be implemented by the class that implements the interface.

Java 8 interface improvement

Since the Java 8 version, default and static methods can be included in an interface. This topic will be dealt with later.

Relationships between classes and interfaces

A class extends another class and an interface extends another interface. However, a class implements an interface. There are both similarities and differences between an interface and a class.

Both class and interface can contain any number of methods and are written in a file with a .java extension. However, an interface cannot be instantiated and does not contain any constructors.

Multiple inheritance in Java by an interface

Multiple inheritance is not supported through class. But it can be achieved through an interface. A class implements multiple interfaces and an interface extends multiple interfaces. This is known as multiple inheritance.

In Java, multiple inheritance is not supported by a class but only possible by an interface. Why?

The fact that multiple inheritance is not supported in case of class because of ambiguity has already been dealt with in the inheritance chapter. However, multiple inheritance is supported in case of an interface.

This is mainly because there is no ambiguity as the implementation of an interface is provided by the implementation of a class.

Interface Inheritance

An interface is implemented by a class. On the other hand, an interface extends another interface. To do so the extends keyword is used.

Java 8 default method in an interface

Since Java 8, a method body can be included in an interface. But in order to do so, it must be made a default method.

Java 8 static method in an interface

Java 8 has also added the feature of including a static method in an interface.

What is a marker or tagged interface?

A marker or tagged interface is an empty interface. Cloneable, remote, and serializable interfaces are tagged interfaces that have no member. They help the JVM to perform various important operations by providing useful information.

The nested interface in Java

An interface that is an extension of another interface is known as a nested interface. The details about nested interface will be dealt in the nested class’s chapter.

Dissimilarities between abstract class and interface

Abstract class and interface are both used to achieve abstraction and both cannot be instantiated. But there are various dissimilarities between an abstract class and an interface. They are:

  1. Type of methods:

An abstract class can have both abstract and non-abstract methods. However, an interface can only have abstract methods. It can also have static and default methods since Java 8.

  1. Supporting multiple inheritance:

An interface supports multiple inheritance but an abstract class does not.

  1. Type of variables:

An abstract class can include final, non-final, static, and non-static variables whereas an interface can only include final and static variable.

  1. Final variables:

An abstract class may have non-final variables but the variables that are declared in an interface are all final variables by default.

  1. Implementation:

An abstract class can implement an interface but an interface cannot implement an abstract class.

  1. Declaration:

An abstract class is declared by using an abstract keyword and an interface is declared by using an interface keyword.

  1. Multiple implementations:

An abstract class can extend another class and implement several interfaces but an interface can only extend another interface.

  1. Accessibility of data members:

An abstract class includes private and protected data members but an interface can only have public data members.

  1. Inheritance vs. Abstraction:

The extends keyword is used to extend an abstract class and the implements keyword is used to implement an interface.

Leave a Reply

Your email address will not be published. Required fields are marked *