当前位置:网站首页>抽象类和接口
抽象类和接口
2022-06-12 09:16:00 【不知努力的bd】
一、抽象类
abstract关键字修饰的类称为抽象类,修饰的方法称为抽象方法,抽象方法只有方法的声明,没有方法体 。它不能被实例化,只能被用作子类的超类。抽象类是被用来创建继承层级里子类的模板。
特点
1、抽象类不能被实例化(不能使用new关键字实例化对象),只能被继承;
2、包含抽象方法的一定是抽象类,但是抽象类不一定有抽象方法;
3、抽象方法的修饰符只能是public或者protected,默认为public;
4、抽象类中的抽象方法只有方法声明,没有方法实现;
5、子类重写父类(抽象类)的所有抽象方法,那么该子类是一个类,否则就还是抽象类;
6、抽象类可以包含属性、方法、构造方法,目的是让子类来调用。
二、接口
接口是一系列方法的声明,是一些方法特征的集合,一个接口只有方法的特征没有方法的实现,因此这些方法可以在不同的地方被不同的类实现,而这些实现可以具有不同的行为(功能)。
特点
1、接口可以包含变量、方法;变量被隐式指定为public,static,final,方法被隐式指定为public,abstract(JDK1.8之前);
2、接口支持多继承,即一个接口可以extends多个接口,间接的解决了Java中类的单继承问题;
3、一个类可以实现多个接口;
4、JDK1.8中对接口增加了新的特性:
4.1、默认方法(default method):JDK 1.8允许给接口添加非抽象的方法实现,但必须使用default关键字修饰;定义了default的方法可以不被实现子类所实现,但只能被实现子类的对象调用;如果子类实现了多个接口,并且这些接口包含一样的默认方法,则子类必须重写默认方法;
4.2、静态方法(static method):JDK 1.8中允许使用static关键字修饰一个方法,并提供实现,称为接口静态方法。接口静态方法只能通过接口调用(接口名.静态方法名)。
三、抽象类和接口的相同点
1、都不能被实例化。
2、接口的实现类或抽象类的子类都只有实现了接口或抽象类中的方法后才能实例化。
四、抽象类和接口的区别
1.抽象类要被子类继承,接口要被类实现。
2.接口只能做方法声明,抽象类中可以作方法声明,也可以做方法实现。
3.接口里定义的变量只能是公共的静态的常量,抽象类中的变量是普通变量。
4.接口是设计的结果,抽象类是重构的结果。
5.抽象类和接口都是用来抽象具体对象的,但是接口的抽象级别最高。
6.抽象类可以有具体的方法和属性,接口只能有抽象方法和不可变常量。
7.抽象类主要用来抽象类别,接口主要用来抽象功能。
8.抽象类中有构造方法,接口中无构造方法。
边栏推荐
- mySql学习记录——二、mySql建表命令
- More than 90% of the software companies will ask the software test interview questions. Hurry to recite them
- Automated test learning path, come and learn
- After going to the bathroom, I figured out the ES search server
- ADB命令集锦,一起来学吧
- Can you migrate backwards before the first migration in the south- Can you migrate backwards to before the first migration in South?
- Meeting time (topology sorting +dp)
- Ceil, floor and round functions
- Consumer configuration related
- 软件测试需求分析方法有哪些,一起来看看吧
猜你喜欢
ADB命令集锦,一起来学吧
Do you know how to improve software testing ability?

2022 simulated examination platform operation of high voltage electrician work license question bank

Black screen solution for computer boot
There must be something you want to know about software testing experience sharing

More than 90% of the software companies will ask the software test interview questions. Hurry to recite them

Filters and listeners

MySQL-MVCC

2022 melting welding and thermal cutting test questions and answers

mySql学习记录——二、mySql建表命令
随机推荐
网络层IP协议 ARP&ICMP&IGMP NAT
Selection of interview questions for software testing
[computer use] how to change a computer disk into a mobile disk?
Thread deadlock and its solution
Share the basic knowledge of software testing and write something you don't know
anxious
Load the source code of 2D 3D virtual anchor in the web page (1: project introduction and source code)
Diff prime pairs
2022 极术通讯-安谋科技纷争尘埃落定,本土半导体产业基石更稳
Chapter VI - procedures with multiple segments
Introduction Fibonacci series
Change tabledata column name
Distributed transaction solution 1: TCC (compensation transaction)
Basic exercise letter graphics
++ problems in C language
L1-002 print Hourglass (20 points)
2022 极术通讯-安谋科技迎来发展新机遇
Practical SQL statements and execution plan view
Grab screen and ground glass effect
Es6-- common basic knowledge