Java Abstract Classes and Interfaces

Level : Intermediate
Mentor: Shailendra Chauhan
Duration : 00:04:00

What is Abstract Class in Java?

Abstract classes are classes that cannot be instantiated. It serves as the base or parent class for all other classes, known as derived classes. Members may be abstract or non-abstract. The descendant classes must implement members that are defined as abstract.

When to Use the Abstract Class?

  • To offer common behavior that various derived classes can inherit or override.
  • To generate several versions of your component/class. 

Abstract Class Features

  • Template for Java Abstract Class: Hides code implementation, allowing for easier data abstraction. End-users receive a template for methods.
  • Loose coupling: Data abstraction lowers dependence and promotes loose coupling.
  • Code reusability: Abstract classes save time by enabling the reuse of abstract methods. Eliminates code redundancy by minimizing repeating coding.
  • Abstraction in Java: Hides code complexity from end users, focusing on essential components.
  • Dynamic Resolution: Dynamic method resolution helps to solve various problems with a single abstract method.


Rules for declaring an abstract class

  • In Java, the keyword "abstract" must be used when declaring an abstract class.
  • Abstract classes can't be instantiated directly.
  • There must be one abstract method in every abstract class.
  • An abstract class contains final methods.
  • Non-abstract methods can be included in an abstract class.
  • Static methods and constructors can be found in an abstract class.

What is an Interface in Java?

Java interfaces work as contracts between classes or structures that implement them, ensuring that all interface members are implemented. Interfaces, unlike abstract classes, do not operate as parent entities and consist only of member declarations, with each member being implicitly public and abstract.


When to use an Interface?

  • To group objects based on shared behavior, as in SOA (WCF).
  • To give a more abstract picture of an unchangeable model.
  • The goal is to construct loosely linked, maintainable, and pluggable components by separating interface implementation from itself.

Declare an Interface

The interface keyword is used to declare a new interface. It provides an entire abstraction, which implies that all methods in an interface are defined with an empty body, and all fields are public, static, and final by definition. A class that implements an interface must include all of the methods specified in the interface.

Uses of Java Interfaces

  • Complete abstraction is accomplished with it.
  • An interface can be used to accomplish multiple inheritances because Java does not support them for classes.
  • Classes can implement an endless number of interfaces, but they can only extend one class.
  • It is also used to obtain loose coupling.
  • Interfaces are used to create abstractions. 

Class-Interface Relationship

In the same way that an interface can extend another interface, a class can extend another class. However, a class can only extend to another interface; this cannot be done in reverse.


Extending Interfaces

The keyword extends allows one interface to inherit from another. A class that implements an interface that is derived from another interface has to provide an implementation for every method that the interface inheritance chain requires.

Self-paced Membership
  • 24+ Video Courses
  • 825+ Hands-On Labs
  • 400+ Quick Notes
  • 125+ Skill Tests
  • 10+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this