Home » Java programming language

Can a class have an interface and can an interface have a class in Java?

Here, we are going to learn that can a class have an interface and can an interface have a class in Java programming language?
Submitted by Preeti Jain, on September 17, 2019

In the very first step, we will see can a class have an interface in Java?

  • Yes, it is possible to define an interface inside the class.
  • The interface is defined in another interface is known as nested interface, but when we define an interface inside the class then that is also known as the nested interface.
  • The objective of defining an interface inside a class is used to group related interfaces so that they can be managed easily.
  • Once an interface is defined in a class then we are not able to access an interface directly (i.e. an interface must be referred by a class).
  • There is a restriction on access modifiers when we define an interface in a class.
  • It is not mandatory to prefix "static" keyword with the interfaces defined in a class because the interface is by default static.

Syntax:

    class MyClass{
        // MyClass Code
        interface MyInterface(){
            //MyInterface Code
        }
    }

Example:

// Java program to demonstrate the example of
// defining an interface in a class

class MyClass {
    // Interface definition in a class
    interface MyInterface {
        void display();
    }
}

public class Main implements MyClass.MyInterface {
    String str = "we are learning Java Programming";
    // override abstract method of interface
    public void display() {
        System.out.print("Hi,");
    }

    public static void main(String[] args) {
        Main m = new Main();
        MyClass.MyInterface mc = new Main();

        // Calling Main class method of interface
        mc.display();
        System.out.println(m.str);
    }
}

Output

Hi, we are learning Java Programming

In the second step, we will see can an interface have a class in Java?

  • Yes, it is possible to define a class inside the interface.
  • The objective of defining a class inside an interface is used to group related interfaces so that they can be managed easily.
  • Once a class is defined in an interface then we are not able to access a class directly (i.e. a class must be referred by an interface).
  • There is no restriction on access modifiers when we define a class in an interface.
  • It is not mandatory to prefix "static" keyword with the class defined in an interface because the class is by default public.

Syntax:

    interface MyInterface{
        // MyInterface Code
        class MyClass(){
            // MyClass Code
        } 
    } 

Example:

// Java program to demonstrate the example of
// defining a class in an interface

interface MyInterface {
    // MyClass definition
    class MyClass {
        String str = "Java support OOPS Concept";
        void display() {
            System.out.print("Hi,");
        }
    }
}

public class Main extends MyInterface.MyClass {
    public static void main(String[] args) {
        // Main class is instantiated
        Main m = new Main();
        // Calling MyClass method
        m.display();
        System.out.println(m.str);
    }
}

Output

Hi, Java support OOPS Concept


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.