JavaSE - Detailed Explanation of Abstract Classes and Interfaces

ced485cbb11e458d81a746890b32cf3f.gif

Author: Rukawa Maple Knock Code

Blog Homepage: Rukawa Kaede's Blog

Column: Learn java with me

Quote: Stay hungry stay foolish

If you want to do good things, you must first sharpen your tools. Let me introduce you to a super powerful tool for winning offers from big manufacturers - Niuke.com

Click to register for free and brush the questions with me  

Table of contents

1. Abstract class

1.1 Understanding abstract classes

1.2 Summary of characteristics of abstract classes

1.3 An important role of abstract classes

2. Interface

2.1 How to understand the interface?

2.2 Use of the interface

2.3 Interface Features

2.4 Implementing multiple interfaces

2.5 Inheritance between interfaces

3. Abstract classes and interfaces


1. Abstract class

1.1 Understanding abstract classes

Concept: When a class does not have enough information to describe a concrete object, the class is an abstract class

Syntax: A class modified by abstract is called an abstract class, and a method modified by abstract in an abstract class is called an abstract method, and an abstract method does not need to give a specific implementation body

Format:

Example: 

abstract class Shape{
    abstract public void draw();
    abstract void calcArea();
    
    public double getArea(){
        return area;
    }
    protected double area;
}

An abstract method has no body

Abstract classes can also add common methods, properties, and constructors

1.2 Summary of characteristics of abstract classes

1. Abstract classes cannot be instantiated

2. Abstract methods cannot be private modified

3. Abstract methods need to be inherited by subclasses, so they cannot be modified by final and static

Error: 

Illegal combination of modifiers: 'abstract' and 'final' 

Illegal combination of modifiers: 'final' and 'abstract'

4. The abstract class must be inherited, and the subclass must override the abstract method of the parent class, otherwise the subclass is also an abstract class

abstract class Shape{
    abstract public void draw();
    abstract void calcArea();

    public double getArea(){
        return area;
    }
    protected double area;
}
class Circle extends Shape{
    private double r;
    final private static double PI = 3.14;

    public Circle(double r) {
        this.r = r;
    }

    @Override
    public void draw() {
        System.out.println("圆形:r"+r);
    }

    @Override
    void calcArea() {
        area = PI*r*r;
    }
    @Override
    public double getArea() {
        return super.getArea();
    }
}

5. Abstract classes do not necessarily contain abstract methods, and classes with abstract methods must be abstract classes

6. There can be a constructor in the abstract class for the subclass to initialize the member variables of the parent class when creating an object

1.3 An important role of abstract classes

After inheriting a class, if it is a common class, it will not report an error without overriding the method. If it inherits an abstract class, it will report an error without overriding the parent class method. Using the compiler's verification, it is very useful in actual development. significance

2. Interface

2.1 How to understand the interface?

An interface is a public standard of behavior. When it is implemented, as long as it conforms to the standard, it can be used universally.

USB, power sockets, etc. of electronic products

In Java, an interface can be seen as: a common specification of multiple classes, a reference data type

 grammar:

The definition of an interface is basically the same as that of a class, keyword: interface

1. When creating an interface, the name of the interface generally starts with a capital letter I

2. The name of the interface generally uses the word "adjective" part of speech

public interface Inter {
       //抽像方法
        void method();
}

2.2 Use of the interface

The interface cannot be used directly, you need to define a concrete class to implement all the abstract methods in the interface

keyword: implements

Note: There is an inheritance relationship of extends between subclasses and parent classes, and an implementation relationship of implements between classes and interfaces

Format:

public class 类名称 implements 接口名称 {
        //...
}

2.3 Interface Features

1. The interface type cannot be instantiated directly, it is the new object

2. The methods in the interface will be implicitly designated as public abstract, and other modifiers will report errors

 3. The methods in the interface are implemented by the class that implements the interface

 4. When rewriting the methods in the interface, you cannot use the default access permission modification

Error: 'openDevice()' in 'A' conflicts with 'openDevice()' in 'USB'; attempt to assign weaker access ('package-private'); 'public'

5. The variables in the interface will be implicitly designated as public static final variables

6. Interfaces cannot have static code blocks and constructors

7. After the interface is compiled, the suffix format of the bytecode file is .class

8. The class must implement all abstract methods in the interface, otherwise the class must be set to an abstract class

 Error: Class 'A' must be declared abstract, or implement abstract method 'USB()' in 'USB'

2.4 Implementing multiple interfaces

Inheritance is single inheritance, but a class can implement multiple interfaces

See an example:

public class Test {
    public static void main(String[] args) {
        dragon drag = new dragon("dragon");
        drag.fly();
        drag.swim();
    }

}

//定义一个动物类
class Animal{
    protected String name;

    public Animal(String name){
        this.name = name;
    }
}
//定义接口

interface IFlying{
    void fly();
}

interface ISwimming{
    void swim();
}
//定义鸟类

class Bird extends Animal implements IFlying{
    public Bird(String name) {
        super(name);
    }

    @Override
    public void fly() {
        System.out.println(name+"正在用翅膀飞");
    }
}
//定义鱼类

class Fish extends Animal implements ISwimming {
    public Fish(String name) {
        super(name);
    }

    @Override
    public void swim() {
        System.out.println(name+"正在用尾巴游");
    }
}
//定义龙类

class dragon extends Animal implements ISwimming,IFlying{
    public dragon(String name) {
        super(name);
    }

    @Override
    public void fly() {
        System.out.println(name+"正在腾云驾雾");
    }

    @Override
    public void swim() {
        System.out.println(name+"正在海里游");
    }
}

 Note: If a class implements multiple interfaces, it must implement all abstract methods, otherwise it must be set to an abstract class

2.5 Inheritance between interfaces

Classes and classes are single-inherited, and interfaces can be multi-inherited

keyword: extends

interface IAmphibious extends IFlying,ISwimming{
    @Override
    void fly();
    @Override
    void swim();
}
class dragon1 extends Animal implements IAmphibious{

    public dragon1(String name) {
        super(name);
    }
    @Override
    public void fly() {
        //...
    }
    @Override
    public void swim() {
        //...
    }
}

Note: Inheritance between interfaces is equivalent to merging multiple interfaces

3. Abstract classes and interfaces

Core difference : An abstract class can contain common methods and fields, such common methods and fields can be used directly by subclasses (without overriding), while interfaces cannot contain common methods, and subclasses must override all abstract methods

The existence of abstract classes is to allow the compiler to better verify❌

Syntax difference:

1. The member variables of an abstract class are unlimited, and the member variables in the interface can only be of public static final type

2. Abstract classes can provide implementation details of member methods, and only public abstract methods can exist in interfaces

3. Abstract classes can have static code blocks and static methods, and interfaces cannot contain static code blocks and static methods

4. A class can only inherit one abstract class, but a class can implement multiple interfaces

"The sharing of this issue is here, remember to give the blogger a three-link, your support is the biggest driving force for my creation!

ced485cbb11e458d81a746890b32cf3f.gif

 

Guess you like

Origin blog.csdn.net/chenchenchencl/article/details/126244071