In the very first step, we will see can a class have an interface in Java?
在第一步中,我们将看到类可以在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.
不必在类中定义的接口前面加上“ 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?
在第二步中,我们将看到接口在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.
由于在默认情况下该类是公共类,因此不必在接口中定义的类前面加上“ static”关键字。
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