What you need to know about abstract classes and interfaces (interview)

1. Abstract class:

(1) The class containing the abstract modifier is an abstract class. Add the abstract keyword before the method to indicate that this is an abstract method. At the same time, the abstract method has no method body (without {}, no specific code can be executed)

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

(2) The abstract class cannot create instance objects

Shape shape = new Shape(); 
// 编译出错
Error:(30, 23) java: Shape是抽象的; 无法实例化

(3) Abstract methods cannot be private

abstract class Shape {
    
     
    abstract private void draw(); 
} 
// 编译出错
Error:(4, 27) java: 非法的修饰符组合: abstractprivate

(4) The abstract class can contain other non-abstract methods, and it can also contain fields. The rules of this non-abstract method and ordinary method are the same, and can be overridden or directly called by subclasses

abstract class Shape {
    
     
    abstract public void draw(); 
    void func() {
    
     
        System.out.println("func"); 
    } 
} 
class Rect extends Shape {
    
     
 ... 
} 
public class Test {
    
     
    public static void main(String[] args) {
    
     
        Shape shape = new Rect(); 
        shape.func(); 
    } 
} 
// 执行结果
func

2. Interface:

(1) Interface (interface) can be said to be a special case of abstract class, all methods in the interface must be abstract. The method definition in the interface defaults to the public abstract type, and the member variable type in the interface defaults to public static final.
Interface cannot be instantiated separately

interface IShape {
    
     
    void draw(); 
    public static final int num = 10;
} 
class Cycle implements IShape {
    
     
    @Override 
    public void draw() {
    
     
        System.out.println("○"); 
    } 
}  
public class Test {
    
     
    public static void main(String[] args) {
    
     
        IShape shape = new Rect(); 
        shape.draw(); 
    } 
}

(2) Sometimes we need to make a class inherit from multiple parent classes. This matter in some programming languages through multiple inheritance is implemented in a way.
But in Java only supports single inheritance, a class can only extends a parent class, but you can implement multiple interfaces simultaneously , multiple inheritance can achieve a similar effect.
Now we use classes to represent a group of animals:

// 定义一个父类 动物
class Animal {
    
     
    protected String name; 
    public Animal(String name) {
    
     
        this.name = name; 
    } 
}

// 定义三个接口
interface IFlying {
    
     
    void fly(); 
} 
interface IRunning {
    
     
    void run(); 
} 
interface ISwimming {
    
     
    void swim(); 
}

// 创建一个子类 猫
class Cat extends Animal implements IRunning {
    
     
    public Cat(String name) {
    
     
        super(name); 
    } 
    @Override 
    public void run() {
    
     
        System.out.println(this.name + "正在用四条腿跑"); 
    } 
}

// 创建一个子类 鸭子
class Duck extends Animal implements IRunning, ISwimming, IFlying {
    
     
    public Duck(String name) {
    
     
        super(name); 
    } 
    @Override 
    public void fly() {
    
     
        System.out.println(this.name + "正在用翅膀飞"); 
    } 
    @Override 
    public void run() {
    
     
        System.out.println(this.name + "正在用两条腿跑"); 
    } 
    @Override 
    public void swim() {
    
     
        System.out.println(this.name + "正在漂在水上"); 
    } 
}

The benefits of polymorphism make programmers forget about types. . With the interface, the user of the class does not need to pay attention to the specific type, but
only pays attention to whether a certain class has a certain ability.

(3) Inheritance between interfaces: An
interface can inherit an interface to achieve the effect of reuse. Use the extends keyword

interface IRunning {
    
     
    void run(); 
} 
interface ISwimming {
    
     
    void swim(); 
} 
// 两栖的动物, 既能跑, 也能游
interface IAmphibious extends IRunning, ISwimming {
    
     
} 
class Frog implements IAmphibious {
    
     
 ... 
}

3. The difference between abstract class and interface:

The core difference:
abstract classes can contain ordinary methods and ordinary fields. Such ordinary methods and fields can be used directly by subclasses (without rewriting), while interfaces cannot contain ordinary methods. Subclasses must rewrite all abstract methods.
( The meaning of abstract classes is to allow the compiler to better verify)

Abstract class interface
Ordinary class + abstract (non-abstract) method + construction method Abstract method + global variable
Various permissions public
Use extends to inherit abstract classes Use implements to implement interfaces
An abstract class can implement several interfaces Interfaces cannot inherit abstract classes, but interfaces can use extends to inherit multiple parent interfaces
A subclass can only inherit one abstract class A subclass can implement multiple interfaces

If you see the friends here, if you think it is useful to you, please like it, give it a attention, and then go! ! !

Guess you like

Origin blog.csdn.net/qq_45658339/article/details/109259398