当前位置:网站首页>Abstract classes and interfaces
Abstract classes and interfaces
2022-07-06 13:07:00 【犇犇犇犇犇犇犇】
Abstract classes and interfaces
Catalog :
List of articles
1. abstract class
1.1 The concept of abstract classes
Abstract classes are often used to represent the analysis of problem fields 、 Abstract concepts in design , It's about a series that looks different , But essentially the same abstraction of concrete concepts .
Usually used in programming statements abstract The decorated class is an abstract class . stay C++ in , Classes containing pure virtual functions are called abstract classes , It can't generate objects ; stay java in , A class containing abstract methods is called an abstract class , Similarly, objects cannot be generated .
Abstract classes are incomplete , It can only be used as a base class . stay Object oriented methods in , Abstract classes are mainly used for type hiding and acting as global variables .
1.2 Syntax of abstract classes
abstract class A
1.3 Abstract class features
- Use abstract The method defined by is called abstract method
- A class with abstract methods must be an abstract class , But abstract classes don't necessarily have abstract methods
- Abstract classes, like ordinary classes, can have member variables 、 Member method 、 Construction method 、 Code block
- Our abstract method cannot be private,static,final Embellished
- Abstract classes cannot instantiate objects , Can be inherited 【 That is to say, abstract classes are used to be inherited 】
- Ordinary classes inherit abstract classes , If there are abstract methods in an abstract class , Then you must override all the abstract methods in the abstract class
- When an abstract class A, Inherited the abstract class B You don't have to rewrite abstract classes B Abstract method of
- Abstract classes are equivalent to an extra layer of compiler validation
1.4 The role of abstract classes
Abstract classes are used to be inherited
In practice , The actual operation is not done in the parent class , When we compile with ordinary classes, if we misuse the parent class , The compiler does not report errors , But if it is an abstract class, it will remind us of errors when instantiating .
2. Interface
2.1 The concept of interface
An interface is a common code of conduct , When you realize , As long as it conforms to the standard , It can be universal . stay Java in , Interfaces can be seen as : Common specification for multiple classes , Is a reference data type .
2.2 grammar
- When creating an interface , Interfaces are generally named in uppercase I start
- Interfaces are usually named with adjective words
- The relationship between classes and interfaces is implementation (implements)
interface IRunning
class A implements IRunning
2.3 Interface to use
The interface cannot be used directly , There must be a class , To implement all the abstract methods in the interface
2.4 Interface features
- Use interface To represent the interface
- The member variables in the interface must be initialized , Because it is by default public static final Embellished
- Methods in the interface , The default is abstract methods , That is to say public abstract Embellished
- Ordinary member methods in interfaces cannot have specific implementations
- If the ordinary member method in the interface needs to have a specific implementation , Have to add default modification
- Interfaces can have static member methods ,default Methods are all default public Embellished
- Interfaces are also not instantiatable
- When a class implements an interface , All abstract methods within the interface must be implemented
- The relationship between classes and interfaces is implementation (implements)
2.5 Implement multiple interfaces
stay java Multiple inheritance is not supported in , But a class can implement multiple interfaces
abstract class Animal{
public String name;
public int age;
public Animal(String name,int age){
this.name = name;
this.age = age;
}
public abstract void eat();
}
interface IRunning{
void iRun();
}
interface IFlying{
void iFly();
}
interface ISwimming{
void ISwim();
}
// Can run , Can swim
class Dog extends Animal implements IRunning,ISwimming{
public Dog(String name, int age) {
super(name, age);
}
@Override
public void eat() {
System.out.println(name+" Dog food ");
}
@Override
public void iRun() {
System.out.println(name+" Running on four legs ");
}
@Override
public void ISwim() {
System.out.println(name+" I'm planing with my four legs ");
}
}
// Can run , Can fly
class Bird extends Animal implements IFlying,IRunning{
public Bird(String name,int age){
super(name,age);
}
@Override
public void eat() {
System.out.println(name + " Eat bird food ");
}
@Override
public void iFly() {
System.out.println(name + " Flying with wings ");
}
@Override
public void iRun() {
System.out.println(name+ " Running on two legs ");
}
}
class Duck extends Animal implements IRunning,IFlying,ISwimming{
public Duck(String name, int age) {
super(name, age);
}
@Override
public void eat() {
System.out.println(name + " Eating duck food ");
}
@Override
public void iRun() {
System.out.println(name + " Running with big feet ");
}
@Override
public void iFly() {
System.out.println(name + " Flying with wings ");
}
@Override
public void ISwim() {
System.out.println(name + " Swimming with big feet ");
}
}
2.6 Inheritance between interfaces
interface A{
void funcA();
}
interface B{
void funcB();
}
// Extend the functionality
interface C extends A,B {
void funC();
}
class T implements C{
@Override
public void funcA() {
}
@Override
public void funcB() {
}
@Override
public void funC() {
}
}
2.7 The difference between interface and abstract class
Interface | abstract class | |
---|---|---|
keyword | interface | abstract |
Member variables | By default, the member variables in the interface are pubic static final modification | The member variables of abstract classes are the same as those of ordinary classes |
Method | No construction method , Common methods must be implemented by default modification , Static methods can be concretely implemented | There's a way to structure , There are common ways to implement , There are static methods that can be implemented |
Subclass Use | implements | extends |
Relationship | Interfaces cannot inherit abstract classes , But the interface can be used extends Keyword inherits multiple parent interfaces | An abstract class can implement multiple interfaces |
Whether you can inherit more | A class can implement multiple interfaces | A class can only inherit one abstract class |
2.8Cloneable Interface and deep copy
Code + Memory analysis chart
//cloneable And deep copy
class Money implements Cloneable{
public double money = 99.99;
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class Person implements Cloneable{
public int age = 10;
public Money m = new Money();
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class Test{
public static void main(String[] args) throws CloneNotSupportedException {
Person person1 = new Person();
Money tmp = new Money();
tmp = (Money)person1.m.clone();
Person person2 = (Person)person1.clone();
person2.m = tmp;
System.out.println(person1.m.money);
System.out.println(person2.m.money);
person2.m.money = 9.9;
System.out.println("+======================+");
System.out.println(person1.m.money);
System.out.println(person2.m.money);
}
public static void main2(String[] args) throws CloneNotSupportedException {
Person person1 = new Person();
Person person2 = (Person)person1.clone();
System.out.println(person1.m.money);
System.out.println(person2.m.money);
person2.m.money = 9.9;
System.out.println("+====================+");
System.out.println(person1.m.money);
System.out.println(person2.m.money);
}
public static void main1(String[] args) throws CloneNotSupportedException {
Person person1 = new Person();
Person person2 = (Person)person1.clone();
System.out.println(person1.age);
System.out.println(person2.age);
person2.age = 99;
System.out.println("+====================+");
System.out.println(person1.age);
System.out.println(person2.age);
}
}
边栏推荐
- 几道高频的JVM面试题
- WSL common commands
- Record: I accidentally wrote a recursion next time
- How to ensure data consistency between MySQL and redis?
- One article to get UDP and TCP high-frequency interview questions!
- 微信小程序开发心得
- Interview Essentials: talk about the various implementations of distributed locks!
- First acquaintance with C language (Part 1)
- 记录:Navicat Premium初次无法连接数据库MySQL之解决
- 2年经验总结,告诉你如何做好项目管理
猜你喜欢
Several high-frequency JVM interview questions
MySQL shutdown is slow
[算法] 剑指offer2 golang 面试题1:整数除法
平衡二叉树详解 通俗易懂
2-year experience summary, tell you how to do a good job in project management
系统设计学习(一)Design Pastebin.com (or Bit.ly)
The earth revolves around the sun
MySQL 三万字精华总结 + 面试100 问,吊打面试官绰绰有余(收藏系列
面试必备:聊聊分布式锁的多种实现!
Ten minutes to thoroughly master cache breakdown, cache penetration, cache avalanche
随机推荐
面试必备:聊聊分布式锁的多种实现!
2年经验总结,告诉你如何做好项目管理
[算法] 剑指offer2 golang 面试题1:整数除法
[algorithm] sword finger offer2 golang interview question 9: subarray with product less than k
MySQL shutdown is slow
The port is occupied because the service is not shut down normally
10 minutes pour maîtriser complètement la rupture du cache, la pénétration du cache, l'avalanche du cache
3月15号 Go 1.18 正式版发布 了解最新特色以及使用方法
Basic DOS commands
系统设计学习(三)Design Amazon‘s sales rank by category feature
最短Hamilton路径 (状压DP)
TYUT太原理工大学2022软工导论简答题
How do architects draw system architecture blueprints?
2-year experience summary, tell you how to do a good job in project management
几道高频的JVM面试题
Exception: ioexception:stream closed
Tyut Taiyuan University of technology 2022 introduction to software engineering
Fairygui bar subfamily (scroll bar, slider, progress bar)
What are the functions and features of helm or terrain
GPS高程拟合抗差中误差的求取代码实现