当前位置:网站首页>Abstract classes, interfaces and common keywords
Abstract classes, interfaces and common keywords
2022-04-23 03:34:00 【Chen Yu】
Java Summary of knowledge points : You can enter from here if you want to see
4、 Abstract classes and interfaces
4.1、 abstract class
use abstract The class modified by keyword is abstract class , use abstract The method of keyword modification is abstract method . Abstract methods only define structures , Without concrete realization , The implementation of the class is handed over to its descendants . A class with abstract methods must be an abstract class , But an abstract class can have ordinary methods or use abstract methods .
An abstract class can't instantiate an object , Other functions of the class still exist , Member variables 、 Member methods and construction methods are accessed in the same way as ordinary classes . So abstract classes need to have Descendant classes inherit and implement all their abstract methods
, Subclasses that inherit abstract classes must implement all abstract methods of the parent class , Otherwise, it should also be set as an abstract class
. Because the abstract method itself has no meaning , Therefore, abstract methods must be rewritten to implement ( All abstract methods of a class must be implemented ).
The significance of the existence of abstract classes lies in their Designability 、 Reusability and expansibility
. In terms of the actual requirements of development , Ordinary classes try not to inherit another ordinary class , But to inherit abstract classes .
// Define animals as abstract classes , There are abstract methods
abstract class Animals {
abstract void sound();
}
//Dog Implements the abstract class , You must implement the abstract methods of the abstract class
class Dog extends Animals{
@Override
public void sound() {
System.out.println(" Barking of a dog ");
}
}
1、final When decorating a method, the representation method cannot be overridden by subclasses , Abstract methods must be overridden by subclasses , So it can't be used together . 2、static It's a static method , Abstract methods need to be inherited and implemented by subclasses ,static It has nothing to do with Subclasses . 3、native It means that this method should be implemented in another platform dependent programming language , There is no subclass inheritance problem , So it can't be used . 4、synchronized It's thread synchronization , and abstract Abstract methods only declare that they do not implement , So there is no synchronization , Cannot be used together . Can be used in subclasses that implement this method .
4.2、 Interface
Interface (interface) And abstract classes (abstract) It means something like , But we know that , A class can only inherit one parent class , Then this has great limitations at some times . So the concept of interface appears ,Java Specify that a class can implement multiple interfaces . Single inheritance and multiple implementation .
The interface is mainly to formulate specifications , There is no specific method inside , Interface cannot be instantiated , Unless defined as an abstract class , Otherwise, once the interface is implemented, all methods in the interface must be implemented .
Interface is to provide services , Like a socket at home , It just provides a specification , I don't care about the use of electrical appliances , As long as the electrical appliance conforms to the interface of this socket , You can use the socket , Turn on the power .
- Interfaces cannot be used to instantiate objects , There is no internal construction method . A class that implements an interface , All methods described in the interface must be implemented , Otherwise, it must be declared as an abstract class . in addition , stay Java in , The interface type can be used to declare a variable , They can be a null pointer , Or be bound to an object implemented by this interface .
- JDK 1.8 in the future , There can be static methods and method bodies in the interface .JDK 1.8 in the future , Interfaces allow methods that contain concrete implementations , This method is called " The default method ", The default method is default Keyword modification .
- Member variables in an interface can only be public static final type
- Interface is not inherited by class , But to be implemented by class . A class can implement multiple interfaces
// Interface declaration
[public] interface The interface name [extends Other interfaces ]{
// Variable
// Method
}
Take computers for example , There are many interfaces on the computer , among USB Interface , keyboard 、 mouse 、 Headphones, etc , As long as meet USB The specification of the interface can be connected to the computer . In contrast, the function of the interface is similar to USB, Once defined, as long as it conforms to the specification of the interface , You can use
-
Define a computer
public class Computer{ // The computer defines a USB The interface of , As long as meet USB The interface can work on the computer private USB usb; public void setUsb(USB usb){ this.usb = usb; } public void wook(){ if(usb != null){ System.out.println("USB The device is working "); usb.write(); } } }
-
Definition USB Interface
public interface USB { void read(); // How to read and write void write(); }
-
Define keyboard and mouse , Realization USB Interface
// Define a keyboard and a mouse , accord with USB Specification of the interface public class Keyboard implements USB { public void Knock(){ System.out.println(" Tap the keyboard "); } @Override public void read() { } @Override public void write() { this.Knock(); } } public class Mouse implements USB { public void rClick(){ System.out.println(" Click on the right "); } public void lClick(){ System.out.println(" Click the left button "); } public void read() { } public void write() { this.rClick(); this.lClick(); } }
-
The keyboard and mouse work through the computer
4.3、 difference
The difference between interface and class :
1、 Interfaces cannot be used to instantiate objects . 2、 Interface has no constructor . 3、 All methods in an interface must be abstract methods . 4、 Interface cannot contain member variables , except static and final Variable . 5、 Interface is not inherited by class , But to be implemented by class . 6、 Interface supports multiple inheritance .
The difference between interface and abstract class
1、 A class can only inherit one abstract class , A class can implement multiple interfaces . 2、 Member variables in an interface can only be public、static、final Type of , Abstract classes can define various types of instance variables and static variables 3、 Interfaces mainly play a role in system architecture design , Used to define the communication contract between modules . Abstract classes are mainly used in code implementation . Can achieve code reuse .
Interfaces are mainly used to constrain the behavior of classes , It only restricts the existence of class behavior , There is no provision for the specific realization of the behavior . Interfaces only define behavior , How to implement the body of a class , The interface doesn't care . yes like a The relationship between . Therefore, the interface is mainly used for the communication contract between different modules of the architecture .
Abstract classes are mainly used to realize code reuse , When parts of some methods of more than two classes have the same implementation , These same behaviors are usually abstracted out , Make an abstract class , After inheriting these functions, you can also expand your own functions . Abstract the essence of a class , A subclass is at least one parent ,is a.
5、 Several commonly used keywords
5.1、final、finally、finalize
1、final Used to declare properties 、 Method 、 class . They represent immutable attributes 、 Methods cannot be overridden 、 Class cannot be inherited . When an inner class accesses a local variable , Local variables must have final modification .
2、finally Is to cooperate when handling exceptions try、catch Use it together ,finally The code block in the is bound to execute .
3、finalize yes Object class , When objects are recycled , The garbage collection processor calls this method .JVM There is no guarantee that this method will always be called .
5.2、static
Can I static Call non - within a modified method static Method ?
answer : Can not be ,static The modified method is static , It belongs to the class , When a class is loaded, it is allocated space , Call... Directly with the class name . Instead of static Property method belongs to the object , The system allocates space for objects only when they are created , Call with method name .
therefore static Modified static methods when calling non static methods , The object may not have been created yet , So at this time, call non static There will be anomalies .
5.3、 Permission modifier
5.4、 super、this
stay Java In language this Represents a reference to an object of this class , General implicit member variables and methods used to reference objects . Simply speaking this It is used to call its own properties and methods .
this and super keyword
this: Refers to the current object itself ( Who uses this It means who , Just as we said in the display ), You can call your own properties and methods ( No static , Static corresponds to a class, not an object ).java The principle of proximity , There are local variables and methods with the same name , Direct calls are local variables , add this You can call properties .
super: Reference to parent space , That is, the reference to the parent class space ( Does not represent an object , It just represents a piece of memory in an object ),super You can call the property method of the parent class . There is a member with the same name in the child and parent class ( Including variables and methods ) when , In a subclass, the default is to access the members of the subclass , Can pass super Keyword specifies the member accessing the parent class ;
super And this Keywords can not appear at the same time to call other constructors in the same constructor . Because both statements need the first one .
5.5、instanceof
版权声明
本文为[Chen Yu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230332246250.html
边栏推荐
- What to pay attention to when writing the first code
- Design and implementation of redis (1): understand data structures and objects
- C abstract class
- Supersocket is Use in net5 - startup
- Redis(17) -- Redis缓存相关问题解决
- The content of the website is prohibited from copying, pasting and saving as JS code
- Scenario Title: how does system a use the page of system B
- Unity knowledge points (common core classes)
- Using swagger in. Net5
- Identifier and type conversion
猜你喜欢
Development record of primary sensitive word detection
On the principle of concurrent programming and the art of notify / Park
Visual programming - Experiment 2
Optimization of especially slow startup in idea debugging mode
Use of rotary selector wheelpicker
MySQL zip installation tutorial
C interface
JS - accuracy issues
Codeforces round 784 (Div. 4) (AK CF (XD) for the first time)
Unity knowledge points (common core classes)
随机推荐
JS changes the words separated by dashes into camel style
Problem C: Hanoi Tower III
Do you really understand hashcode and equals???
2021-08-11
Supersocket is Use in net5 - concept
打卡:4.22 C语言篇 -(1)初识C语言 - (11)指针
Build websocket server in. Net5 webapi
Codeforces Round #784 (Div. 4)题解 (第一次AK cf (XD
The art of concurrent programming (6): explain the principle of reentrantlock in detail
Talent Plan 学习营初体验:交流+坚持 开源协作课程学习的不二路径
标识符、关键字、数据类型
When migrating tslib_ setup: No such file or directory、ts_ open: No such file or director
移植tslib时ts_setup: No such file or directory、ts_open: No such file or director
SQL topic exercise summary
Applet - WXS
MySQL keyword group_ Concat, combined connection query
JS calculates the display date according to the time
浅学一下I/O流和File类文件操作
C-11 problem I: find balloon
2022 团体程序设计天梯赛 模拟赛 L2-4 哲哲打游戏 (25 分)