当前位置:网站首页>Abstract classes and interfaces
Abstract classes and interfaces
2022-06-29 09:04:00 【Chang'an has a hometown y】
abstract class
1. abstract class
grammar :
[ List of modifiers ] abstract class Class name {
// The class body
}
Be careful ,final And abstract Cannot modify classes at the same time , They are in conflict ,final The decorated class cannot be inherited .
Naming of abstract classes : You don't have to
AbstractXXX
BaseXXX
Members that can be defined in ordinary classes can also be defined in abstract classes
Abstract class cannot create object , But there are also constructors
Code block , Both static and static
Two subclasses of an abstract class :
Common subclasses , You must implement all the abstract methods of an abstract class
abstract class , It can be realized according to requirements
Be careful : Abstract classes can also inherit from ordinary classes
abstract Can only be used to decorate classes and methods
Cannot be associated with private final static Modify the method together
2. Interface
A new data type
1. No emphasis on attributes , Emphasize only the abstraction of behavior ," Inherit " It represents an extension of functionality .
2. After a class inherits a class , Can go on " Inherit ", Not subject to Java Single inheritance limit .
3. No, "is-a" The limitations of this inheritance relationship .
Definition of interface
[ Access modifier ] interface The interface name {
}
Interface access rights public And default
Interface usage syntax :
[ Access modifier ] class Class name extends Class name implements The interface name {
}
A class can implement multiple interfaces , Have inherited , Is to inherit first and then implement ;
Advantages of the interface
Interfaces are not subject to single inheritance , This is a very important advantage of the interface .
Interface is not a class , It and its implementation classes , There is no need to exist "is-a" Relationship , Interfaces are more flexible than abstract classes .
The relationship between an interface and its implementation classes in program development , be called "like-a" Relationship . As long as it is realized Xxx The function of , It's like Xxx.
Interfaces represent a development standard , A specification . Represents an extension to the function , It only focuses on behavior , Don't pay attention to attributes .
3.1 Interface features
(1) Out of commission final modification , Born to be abstract Embellished , Hide the , Cannot create its own object
(2)public It can't be changed
(3) The global constants in the interface must be explicitly assigned , Cannot assign with static code block , Because the interface has no static code block
(4) stay Java8 Before , The default methods in the interface are public abstract Modifies the member method , That is, only abstract methods can be defined .
(5) Interface has no constructor , No building blocks , There are no static blocks of code .
(6) Subclasses of interfaces can be ; General class , abstract class
Inherit (extends) You can't cross race , Class and class inheritance , Interfaces and interface inheritance , There is no inheritance relationship between interfaces and classes .
An ordinary class inherits an abstract class , You need to implement all the abstract methods inside .
An abstract class inherits an abstract class , Implement the abstract methods on demand .
An abstract class inherits a common class , Yes. .
An interface inherits an interface , You will get all the abstract methods of the interface .
Class inheritance is single inheritance , Interface inheritance is multi inheritance .
Realization (implements), Must be found between classes and interfaces , Interfaces have nothing to do with classes .
Common classes implement interfaces , All abstract methods must be implemented
The abstract class implements the interface , Implement abstract methods on demand
3.2 java8 Default and static methods in
(1) The syntax of the default method
// The syntax of the default method :
default return type Method name ( Parameter list ){
// Method body
}
Modify permissions public You cannot change
effect :
Implementation methods in abstract classes are used as the default implementation of subclasses , But the interface does not have this function .
Interfaces represent extensions to functions , It is more of a standard and specification , In most cases, it does not need to implement methods .
Java8 The default method is still added , It is mainly considered from two perspectives :
Once there is a default implementation method in the interface , The flexibility of programming interface increases . When there is no default method , Once any method is added to the interface , Subclasses must implement it . Now there is a default method , You don't need to do this :
Rewrite what you need to implement , Inherit what you don't need . Code compatibility and flexibility increase .
Java8 Introduced in Lambda Expression and StreamAPI, The default methods in the interface can be used as their implementation , Complete functional programming .( This only understands , The back can speak Lambda expression )
Code :
public class Demo01 {
public static void main(String[] args) {
Person p1 = new B();
p1.test();
}
}
interface Person{
default void test(){
System.out.println(" I'm the default method , Implementation classes can choose to override , It can also be inherited directly !");
}
}
class B implements Person{
@Override
public void test() {
System.out.println(" Override the default method !!");
}
}
(2) Static methods
grammar :
static return type Method name ( Parameter list ){
// Method body
}
The static methods in the interface are also the default public Embellished , Non modifiable .
It is called by the interface name . Static method name .
The static methods in the interface are also used to cooperate Lambda Expression and StreamAPI The use of , Other scenes are rare .
Interface since Java8 Later, there will be static methods , You can even write a main Method .
Static implementation methods in the interface , You can only use it yourself , Cannot call with the implementation class name .
Code :
public class Demo01 {
public static void main(String[] args) {
// You can only call
Person.test1();
}
}
interface Person{
static void test1(){
System.out.println(" Static methods , You can only call , Implementation classes cannot call ");
}
}
class B implements Person{
}
4. The difference between abstract classes and interfaces

边栏推荐
- 查找字符串中重复次数最多的元素
- Calculus Learning
- Robcogen tutorial of robot code generator
- PointNet/Pointnet++训练及测试
- 2022第六季完美童模 清远赛区 海选赛圆满落幕
- Core development board & debugger
- io流的总结
- 积分商城运营要如何做才能获取到利润
- Unity C # e-learning (12) -- protobuf generation protocol
- Compare homekit, MI family, and zhiting family cloud edition for what scene based experiences
猜你喜欢

New paid Tarot calculation source code (with building tutorial)

CDGA|交通行业做好数字化转型的核心是什么?

2022第六季完美童模 合肥賽區 决賽圓滿落幕

Let you know today that the passing rate of the PMP Exam is 97%, is it believable

首次触电,原来你是这样的龙蜥社区 | 龙蜥开发者说第8期

【最全】PS各个版本下载安装及小试牛刀教程(PhotoShop CS3 ~~ PhotoShop 2022)

搭建开源物联网平台教程

MQTT第二话 -- emqx高可用集群实现

2022年7月产品经理认证招生简章(NPDP)

Self attention mechanism
随机推荐
cokkie和session的区别
调试H5页面-vConsole
(III) encoder self attention mask
十大券商账号开户安全吗?是靠谱的吗?
【无标题】
Uber前安全主管面临欺诈指控 曾隐瞒数据泄露事件
手写 redux-thunk
Working for many years, recalling life -- three years in high school
闭关修炼(二十)如何做好单元测试
js for in循环 for of循环的区别以及用法
2022 spring summer collection koreano essential reshapes the vitality of fashion
Unity C # e-learning (12) -- protobuf generation protocol
搭建开源物联网平台教程
编程语言
开发小技巧-图片资源管理
Verilog expression
批量处理实验接触角数据-MATLAB分析
The sixth season of 2022 perfect children's model Qingyuan competition area audition came to a successful conclusion
工厂模式
微信小程序最新canvas2d手写签名