Zone Of Makos

Menu icon

Interfaces and Abstract Classes

In Java, interfaces and abstract classes are key concepts that allow for the creation of more flexible and modular code. They provide a way to define common behaviors and structures that can be implemented or extended by other classes. In this section, we will explore the differences between interfaces and abstract classes and how to effectively use them in your Java programs.

Interfaces

An interface in Java is a collection of abstract methods and constant variables. It defines a contract that implementing classes must adhere to. Interfaces are used to establish a common set of methods that multiple classes can implement, regardless of their specific implementation details. Key points to note about interfaces include:

1. Declaration and Implementation

An interface is declared using the interface keyword. For example:


    public interface MyInterface {
      // abstract method declarations
      void method1();
      void method2();
      
      // constant variables
      int MY_CONSTANT = 10;
    }

Classes that implement an interface must provide an implementation for all its abstract methods. They do so using the implements keyword. For example:


    public class MyClass implements MyInterface {
      public void method1() {
        // implementation goes here
      }
      
      public void method2() {
        // implementation goes here
      }
    }
  

2. Multiple Interface Implementation

Java allows a class to implement multiple interfaces, separated by commas. This enables a class to inherit multiple sets of behaviors. For example:


    public class MyClass implements MyInterface1, MyInterface2 {
      // implementation of abstract methods from both interfaces
    }
  

3. Default Methods

Starting from Java 8, interfaces can include default methods. These methods have an implementation and are used to provide a default behavior for all implementing classes. Default methods are declared using the default keyword. For example:


    public interface MyInterface {
      void method1(); // abstract method
      
      default void method2() {
        // default implementation goes here
      }
    }
  

Abstract Classes

Abstract classes in Java serve as a blueprint for other classes. They cannot be instantiated themselves but can be extended by other classes. Abstract classes can contain both abstract and non-abstract methods, as well as instance variables. Key points to note about abstract classes include:

1. Declaration and Inheritance

An abstract class is declared using the abstract keyword. For example:


    public abstract class MyAbstractClass {
      // abstract method declaration
      public abstract void abstractMethod();
      
      // non-abstract method
      public void nonAbstractMethod() {
        // implementation goes here
      }
    }
  

Abstract classes are meant to be extended by other classes using the extends keyword. For example:


    public class MyClass extends MyAbstractClass {
      public void abstractMethod() {
        // implementation goes here
      }
    }
  

2. Single Inheritance

Unlike interfaces, a class can extend only one abstract class. This is known as single inheritance. However, a class can still implement multiple interfaces along with extending an abstract class. For example:


    public class MyClass extends MyAbstractClass implements MyInterface1, MyInterface2 {
      // implementation of abstract methods from the abstract class
      
      // implementation of abstract methods from the interfaces
    }
  

3. Abstract Methods

Abstract methods are methods without an implementation. They are meant to be overridden by the class that extends the abstract class. Abstract methods are declared using the abstract keyword. For example:


    public abstract class MyAbstractClass {
      public abstract void abstractMethod(); // abstract method
    }
  

By understanding the concepts of interfaces and abstract classes, you can design more flexible and maintainable Java programs. Interfaces allow for loose coupling between classes, while abstract classes provide a way to define common functionality across a hierarchy of classes. Take advantage of these powerful features to write efficient and reusable code in Java.