Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What are Interfaces in Java?
3.
How to declare an interface?
4.
Implementing Interfaces
4.1.
Example
4.2.
Java
5.
Extending Interfaces
5.1.
Java
5.2.
Java
6.
Extending Multiple Interfaces
6.1.
Java
7.
Tagging Interfaces
7.1.
Java
8.
Frequently Asked Questions
8.1.
Does the Java interface uses variables?
8.2.
What is the purpose of an interface?
8.3.
In Java, does an abstract class have constructors?
8.4.
What is the distinction between interfaces and inheritance?
9.
Conclusion
Last Updated: Mar 27, 2024
Hard

Extending Interfaces

Author Rinki Deka
0 upvote
Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

In Java, an interface is a means for achieving abstraction. The Java interface can only have abstract methods, not method bodies. In Java it is used to achieve abstraction and multiple inheritance. In other words, interfaces can have abstract methods and variables. It is not allowed to have a method body.

Extending Interfaces

In this tutorial, you will get an understanding of Interfaces in Java, their declaration, and extending interfaces, along with some code examples.

What are Interfaces in Java?

In Java, an interface is a reference type. It is comparable to class. It is a set of abstract procedures. A class implements an interface, inheriting the interface's abstract functions.

An interface may also have constants, default methods, static methods, and nested types in addition to abstract methods. Method bodies are only available for default and static methods.

Creating an interface is comparable to creating a class. A class, on the other hand, describes an object's features and behaviors. An interface also includes the actions that a class implements. Unless the class that implements the interface is abstract, all interface methods must be declared in the class.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

How to declare an interface?

The interface keyword is used to declare an interface. It supports complete abstraction, which implies that all methods in an interface are declared with an empty body by default, and all fields are public, static, and final by default. A class that implements an interface must implement all of the interface's functions.

import java.lang.*;
// Any number of import statements


public interface NameOfInterface {
   // Any number of final, static fields
   // Any number of abstract method declarations
}

Interfaces include the following characteristics:

  • An interface is, by definition, abstract. When declaring an interface, you do not need to utilize the abstract keyword.
     
  • Since each method in an interface is implicitly abstract, the abstract keyword is unnecessary.
     
  • Interface methods are implicitly public.

Implementing Interfaces

When a class implements an interface, it is signing a contract, agreeing to perform the interface's defined behaviors. If a class does not perform all of the interface's actions, it must declare itself abstract.

To implement an interface, a class utilizes the implements keyword. Following the extends component of the declaration, the implements keyword appears in the class declaration.

Example

  • Java

Java

interface Animal {
public void dogSound(); // interface method (does not have a body)
public void sleep(); // interface method (does not have a body)
}

class dog implements Animal {
public void dogSound() {
System.out.println("The dog always says bhao bhao");
}
public void sleep() {
System.out.println("Zzz");
}
}

class Main {
public static void main(String[] args) {
dog myDog = new dog();
myDog.dogSound();
myDog.sleep();
}
}

 

Output:

Implementing Interfaces

There are numerous guidelines to follow when overriding methods defined in interfaces:

  • Checked exceptions should not be defined on implementation methods that aren't declared by the interface method or subclasses of those that are.
     
  • When overriding the methods, the interface method's signature and the same return type or subtype should be preserved.
     
  • An implementation class can be abstract, in which case interface methods do not need to be implemented.

Extending Interfaces

When one interface inherits from another, the sub-interface inherits all of the methods and constants declared by the super-interface. Furthermore, it can declare new abstract procedures and constants. To extend an interface, use the extends keyword in the same way that you would in a class definition. An interface can directly extend many interfaces, as opposed to a subclass, which can only directly extend one subclass. The following syntax can be used to accomplish this:

  • Java

Java

[public] interface InterfaceName
extends interfacel[, interface2, , interfaceN]
{//interface body}

 

The interface's name is InterfaceName in this case. This interface extends one or more other interfaces according to the extends clause. These are referred to as super interfaces and are listed alphabetically. Commas separate the names of the super interfaces.

Consider the following program, which demonstrates how one interface extends another.

  • Java

Java

interface Interface1 {
public void func1();
}
//Interface2 extending Interface1
interface Interface2 extends Interface1 {
public void func2();
}
class temp implements Interface2 {
//definition of method declared in interfacel
public void func1() {
System.out.println("Contents of Method func1() in Interface1");
}
public void func2() {
System.out.println("Contents of Method func2() in Interface2");
}
public void func3() {
System.out.println("Contents of Method func3() of Class temp");
}
}
class ExtendingInterface {
public static void main(String[] args) {
Interface2 v2; //Reference variable of Interface2
v2 = new temp(); //assign object of class temp
v2.func1();
v2.func2();
temp xl = new temp();
xl.func3();
}
}

 

Output:

output

Extending Multiple Interfaces

Only one parent class can be extended by a Java class. Multiple inheritance is prohibited. Interfaces, on the other hand, are not classes and an interface can extend more than one parent interface.

The keyword extends is used just once, and the parent interfaces are listed in a comma-separated list.

For instance, if the Hockey interface included both Sports and Events, it would be described as:

  • Java

Java

public interface Hockey extends Sports, Event

Tagging Interfaces

The most common application of extending interfaces is when the parent interface lacks methods. The MouseListener interface in the java.awt.event package, for example, enhanced java.util.EventListener, which is defined as:

  • Java

Java

package java.util;
public interface EventListener
{}

 

A tagging interface is one that does not have any methods. Tagging interfaces serve two primary design functions:

  • Creates a common parent like the EventListener interface, which is extended by dozens of other interfaces in the Java API. You may use a tagging interface to create a common parent among a set of interfaces. For example, when an interface extends EventListener, the JVM understands that this interface will be used in an event delegation scenario.
     
  • Adds a data type to a class. This is the context for the word "tagging." A class that implements a tagging interface does not need to declare any methods (since the interface does not have any), but the class gets transformed into an interface type via polymorphism.

Also read, even number program in java

Read more, how to run java program

Frequently Asked Questions

Does the Java interface uses variables?

An interface can have methods and variables as a class, but those methods declared in an interface are by default abstract (only method signature, without body). Interfaces specify what a class must do and not how.

What is the purpose of an interface?

An interface is used to define a protocol of behavior that any class at any level of the class hierarchy can implement. Interactions can assist detect commonalities between unrelated classes without requiring a class relationship.

In Java, does an abstract class have constructors?

Yes, an abstract class can declare and define a constructor in Java. Since you can not create an instance of an abstract class, a constructor can only be called during constructor chaining, i.e. when you create an instance of the concrete implementation class. 

What is the distinction between interfaces and inheritance?

Interfaces only permit the declaration of properties and methods, and interface-type objects are not permitted to declare new methods or variables. However, using superclass, we can declare variables and methods. A subclass that inherits a class can also declare its variables and methods.

Conclusion

In object-oriented programming, interfaces offer a vital method for attaining abstraction and modularity. They provide a contract that each class must follow, assuring uniform behavior. This method is enhanced by interface extensions, which enable new interfaces to inherit declarations and methods, encouraging code reuse. In this article, we discussed interfaces and extending interfaces in Java, along with some code examples.

To better understand the topic, you can refer to InterfacesInterface in Java, and Interfaces in Typescript.

For more information, refer to our Guided Path on Coding Ninjas Studio to upskill yourself in PythonData Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more! 

Head over to our practice platform, CodeStudio, to practice top problems, attempt mock tests, read interview experiences and interview bundles, follow guided paths for placement preparations, and much more!

Live masterclass