TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: Java Interfaces Explained
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Java OOPJava Tutorials

Java Interfaces Explained

Last updated: Sep 29, 2023 9:14 pm
By Meenakshi Agarwal
Share
5 Min Read
Java interfaces with examples
Java interfaces with examples
SHARE

This tutorial will guide you on what are interfaces in Java. You will know how to create interfaces and use them in Java programs.

Contents
Interfaces concept in JavaDescription:Syntax:Example demonstrating Interfaces in Java:Extend interfacesDescription:Example to demonstrate extending interfaces:Related Posts

Basics of Interfaces in Java

You can go through the following sections to learn about Java interfaces.

Interfaces concept in Java

We can often mislead Interfaces with classes in Java. However, the outlining difference between a class and an interface is that an interface contains abstract methods. A class can implement an interface to provide definition or body to abstract methods while the other way is not possible.

Description:

An interface is just like a reference type in Java. An interface along with the abstract methods contains pre-defined constants and static methods. In OOPS language, an interface defines the behavior a class implements. It is important to realize that the class implementing the interface must define all the methods declared as abstract.

Syntactically, one can find many similarities between an interface and a class. An interface can define as many methods as needed. Much like a class, the name of the Java file for the interface contains the name of the interface. Even, JVM generates its class file in the same pattern.

However, it is equally important to remember that an interface unlike a class does not contain any constructors. Hence, there is no possibility to instantiate it. On the positive side, the interface can let us implement multiple Inheritance which is not possible with classes.

The variables defined through interfaces are always static and final. The methods within an interface are implicitly public. An interface with no methods is called a tagging interface.

Java interfaces explained

Syntax:

The general syntax to define an interface is much like that of a class. Interfaces and the methods within the interfaces are implicitly abstract, therefore, we do not need to use the abstract keyword.

The keyword used is “interface”:

public interface testInterface {
   // 1. *** abstract methods
   // 2. *** static methods
   // 3. *** static and final variables
}

Example demonstrating Interfaces in Java:

In this example, we will explore the concept of interfaces in Java and demonstrate how to define and implement them to achieve better code organization and abstraction.

interface miInterface {

    public static void disp( ) {
        System.out.println("Static method in interface");
    }
    
    public void testPrint( );
}

public class jTest implements miInterface {

    public void testPrint( ) {
        System.out.println("Defining interface abstract method");
    }

    public static void main(String args[]) {
    
        jTest o = new jTest();
        o.testPrint();

        miInterface.disp();
    }
}

Therefore, the output is:

Defining interface abstract method
Static method in interface

Extend interfaces

Description:

While giving the body to the abstract methods in the class, the signature of the methods should not be changed. A class can implement multiple interfaces at the same time. An interface is extended to other interfaces much like A parent class inherits a child class.

The keyword used is “extends” to make sure the parent interface inherits the functionality of the child interface. The example below deals with multiple Inheritance through interfaces:

Example to demonstrate extending interfaces:

Here, we’ll provide a code example to illustrate how to extend interfaces in a Java program.

interface miInterface1 {

    static void miMethod1() {
    
        System.out.println("Static method 1");
    
    }
    
    public void miMethod2();
}

interface miInterface2 {

    static void miMethod2() {
    
        System.out.println("Static method 3");
    
    }
    
    public void miMethod4();
}

interface miInterface extends miInterface1, miInterface2 {

    static int num = 10;

}

public class ifTest implements miInterface {

    public void miMethod2() {
    
        System.out.println("Method from interface 1");
    
    }
    
    public void miMethod4() {
    
        System.out.println("Method from interface 2");
    
    }
    
    public static void main(String args[]) {
    
        ifTest o = new ifTest();
        
        o.miMethod2();
        
        o.miMethod4();
        
        System.out.println(miInterface.num);
    }
}

The output for the above snippet is:

Method from interface 1
Method from interface 2
10

The thing to keep in mind is that miInterface does not have access to static methods of its subinterfaces (i.e., miMethod1 and miMethod3).

Related Posts

Class and Object in Java
Constructor in Java
Inheritance in Java
Types of Inheritance in Java

You Might Also Like

A Simple Guide to Exception Handling in Java

Difference Between Spring and Spring Boot

How to Use Java String Format with Examples

Java IRC Bot with Sample Code

Generate Random Number in Java – 10 Ways

Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article Java Inheritance Java Inheritance Explained
Next Article types of inheritance in java Java Inheritance Types

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x