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

翻译自: https://www.includehelp.com/java/can-a-class-have-an-interface-and-can-an-interface-have-a-class-in-java.aspx

一个类可以有一个接口,接口可以有一个Java类吗?相关推荐

  1. java 使用不同目录下的类_如何运行在不同目录下的java类文件? - Break易站

    Java 基础语法 在本文中,我们将学习如何使用其他项目的实用程序,类和成员.在继续之前,让我们了解一些关键字. 类路径 类路径是jvm开始执行程序的位置.与传统的动态加载行为类似,当执行Java程序 ...

  2. android 调用java类_Android中在WebView里实现Javascript调用Java类的方法

    搜索热词 为了方便网页和Android应用的交互,Android系统提供了WebView中JavaScript网页脚本调用Java类方法的机制.只要调用addJavascriptInterface方法 ...

  3. java 映射类_将数据库类型映射到具体的Java类

    解 答案比使用getMetaData方法更复杂,因为getMetaData方法返回的整数类型和完整的类名没有直接映射.此解决方案需要两段代码: >实现一个方法来获取java.sql.Types常 ...

  4. 【Java进阶】有哪些方法可以在运行时动态生成一个Java类?

    在开始今天的学习前,我建议你先复习一下专栏第 6 讲有关动态代理的内容.作为 Java 基础模块中的内容,考虑到不同基础的同学以及一个循序渐进的学习过程,我当时并没有在源码层面介绍动态代理的实现技术, ...

  5. 规则引擎集成接口(九)Java类对象

    Java类对象 右键点击"对象库" -"添加java类对象",如下图: 弹出窗体,在文本框中输入类的全名"com.flagleader.test.Te ...

  6. Java 接口基础详解,java开发面试笔试题

    我总结出了很多互联网公司的面试题及答案,并整理成了文档,以及各种学习的进阶学习资料,免费分享给大家. 扫描二维码或搜索下图红色VX号,加VX好友,拉你进[程序员面试学习交流群]免费领取.也欢迎各位一起 ...

  7. Java深度历险(二)——Java类的加载、链接和初始化

    在上一篇文章中介绍了Java字节代码的操纵,其中提到了利用Java类加载器来加载修改过后的字节代码并在JVM上执行.本文接着上一篇的话题,讨论Java类的加载.链接和初始化.Java字节代码的表现形式 ...

  8. Java类的加载过程详解 面试高频!!!值得收藏!!!

    受多种情况的影响,又开始看JVM 方面的知识. 1.Java 实在过于内卷,没法不往深了学. 2.面试题问的多,被迫学习. 3.纯粹的好奇. 很喜欢一句话: 八小时内谋生活,八小时外谋发展. 望别日与 ...

  9. 使用JAXB将XML Schema绑定到Java类

    http://blog.csdn.net/zsyspace/article/details/1786079 Java Architecture for XML Binding (JAXB) 是一项可以 ...

  10. YangTools从YANG生成Java类(Maven)

    1.说明 ODL提供了Yang Tools工具从YANG文件生成Java类, 本文介绍使用Maven插件的方式生成, 基于yang-maven-plugin这个插件. 2.创建Maven工程 Ecli ...

最新文章

  1. 设计模式之装饰模式学习笔记
  2. 青春是如此美好,又怎忍平凡度过
  3. 课后作业:字符串加密
  4. 自学Zabbix3.0版本以上资产清单inventory
  5. Springboot 整合微信小程序实现登录与增删改查
  6. 谨慎Asp.net中static变量的用法
  7. 外中断---汇编学习笔记
  8. boost.asio openssl zlib protobuf icu vs2015编译
  9. ASP.NET Web API 接口执行时间监控
  10. windows环境通过cmd命令到ftp上下载文件到linux服务器
  11. linux之登录式shell和非登录式shell
  12. ubuntu16.04装机7:安装VScode
  13. 虚拟机下Ubuntu打开摄像头是黑屏问题
  14. Python:实现pigeon sort鸽巢算法(附完整源码)
  15. 用java定义图书book类_Java封装图书信息类
  16. android 锁屏音乐控制
  17. github上三个不错的开源框架
  18. 华为双前置摄像头_华为第一款“刘海屏”手机发布,前置摄像头逆天
  19. oracle oid查询 视图,OID View
  20. 知了堂笔记之html+CSS动画万花筒

热门文章

  1. 华为笔记本会不会用鸿蒙,华为MateBook Pro笔记本为什么不用鸿蒙操作系统HarmonyO?...
  2. 进程调度rr算法java实现_Java实现进程调度算法(二) RR(时间片轮转)
  3. opencv获取模板旋转角度_OpenCV入门之获取图像的旋转角度
  4. 【Java从入门到头秃专栏 】(一)学在Java语法之前
  5. sambd ERROR: job for smbd.service failed
  6. 搞懂toString()与valueOf()的区别
  7. 字符串拼串 能缓解我们的开发难度→!←(ε=(´ο`*)))唉,又是一个不知道该怎么写题目的随笔啊,头疼)...
  8. HDFS DataNode 设计实现解析
  9. [nodejs] 利用openshift 撰寫應用喔
  10. 路由器ospf动态路由配置