[The Road to becoming a King of Java] Chapter 16: Java SE (Object-Oriented Programming - Abstract Classes, Interfaces)

Objectives of this section

abstract class

interface

1. What is an abstract class

There is no actual working method, we can design it as an abstract method, and the class that contains the abstract method is called an abstract class.

Example:

abstract class shape{
    public abstract void draw();//抽象方法
}

1. A class that contains abstract methods is called an abstract class.

2. What is an abstract method: a method without concrete implementation is modified by the abstract keyword.

Difference between abstract class and ordinary class

1. An abstract class cannot be instantiated.

 2. Because they cannot be instantiated, abstract classes can only be inherited. An ordinary class inherits an abstract class, then in this ordinary class, all abstract methods of this abstract class need to be rewritten

 3. Abstract classes can also contain the same members and methods as ordinary classes

 4. The biggest role of abstract classes is to be inherited.

abstract class Shape{
    public int a;
    public void func(){
        System.out.println("测试普通方法");
    }
    public abstract void draw();//抽象方法
}
class Rect extends  Shape{
    @Override
    public void draw() {
        System.out.println("❀");
    }
}

public class Test {
    public static void drawMap(Shape shape){
        shape.draw();
    }
    public static void main(String[] args) {
        Shape shape = new Rect() ;
        drawMap(new Rect());

    }
}

 5. If an abstract class A inherits an abstract class B, then the abstract class A may not implement the abstract methods of the abstract parent class B

6. Combined with point 5, when class A is inherited by a common class again, the abstract methods in the two abstract classes A and B must be rewritten 

bstract class Shape{
    public int a;
    public void func(){
        System.out.println("测试普通方法");
    }
    public abstract void draw();//抽象方法
}
abstract class A extends Shape{
    public abstract void func();

}
class B extends A{
    @Override
    public void func() {
        
    }

    @Override
    public void draw() {

    }

}
class Rect extends  Shape{
    @Override
    public void draw() {
        System.out.println("❀");
    }
}

public class Test {
    public static void drawMap(Shape shape){
        shape.draw();
    }
    public static void main(String[] args) {
        Shape shape = new Rect() ;
        drawMap(new Rect());

    }
}

Why use abstract classes 

The scenario of using an abstract class is like the above code. The actual work should not be done by the parent class, but by the subclass. Then if you accidentally use the parent class by mistake, the ordinary class compiler will not report an error. . But if the parent class is an abstract class, it will prompt an error when instantiating it, let us find the problem as soon as possible

2. What is an interface

An interface is a further step of an abstract class. An abstract class can also contain non-abstract methods and fields. The methods contained in an interface are abstract methods, and fields can only contain static constants

 grammar rules

In the example of printing graphics just now, our parent class Shape does not contain other non-abstract methods, and can also be designed as an interface

interface IShape { 
 void draw(); 
} 
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(); 
 } 
} 

Define an interface using interface

The methods in the interface must be abstract methods, so abstract can be omitted

The methods in the interface must be public, so public can be omitted

Cycle uses implements to inherit the interface. At this time, the meaning of the expression is no longer "extension", but "implementation"

When calling, you can also create a reference to an interface, corresponding to an instance of a subclass.

Interfaces cannot be instantiated individually.

Extends vs implementations

Expansion means that there is already a certain function, and further expansion of the function.

Implementation means that there is currently nothing and needs to be constructed from scratch 

Interfaces can only contain abstract methods. For fields, interfaces can only contain static constants (final static).

interface IShape { 
 void draw(); 
 public static final int num = 10; 
} 

 The public, static and final keywords can be omitted. The omitted num still represents the public static constant

hint:

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

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

3. It is stipulated in the Ali Coding Specification that no modification symbols should be added to the methods and attributes in the interface, so as to keep the code concise.

Implement multiple interfaces

Sometimes we need to make a class inherit from multiple parent classes at the same time. This is achieved in some programming languages ​​through multiple inheritance. However, Java only supports single inheritance, and a class can only extend one parent class. But Multiple interfaces can be implemented at the same time, and a similar effect of multiple inheritance can be achieved. Now we use classes to represent a group of animals.

class Animal { 
 protected String name; 
 
 public Animal(String name) { 
 this.name = name; 
 } 
} 

 In addition, we provide a set of interfaces, representing "flying", "running", and "swimming" respectively.

interface IFlying { 
 void fly(); 
} 
interface IRunning { 
 void run(); 
} 
interface ISwimming { 
 void swim(); 
} 

Next we create a few specific animal cats that can run.

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

Fish can swim.

class Fish extends Animal implements ISwimming { 
 public Fish(String name) { 
 super(name); 
 } 
 @Override 
 public void swim() { 
 System.out.println(this.name + "正在用尾巴游泳"); 
 } 
} 

Frog, can run and swim (amphibian)

class Frog extends Animal implements IRunning, ISwimming { 
 public Frog(String name) { 
 super(name); 
 } 
 @Override 
 public void run() { 
 System.out.println(this.name + "正在往前跳"); 
 } 
 @Override 
 public void swim() { 
 System.out.println(this.name + "正在蹬腿游泳"); 
 } 
}

There is also a magical animal, amphibious, called "duck"

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 above code shows the most common usage in Java object-oriented programming: a class inherits a parent class and implements multiple interfaces at the same time

The meaning of inheritance expression is is-a semantics, and the meaning of interface expression is to have xxx characteristics

A cat is an animal with the ability to run.

A frog is also an animal that can both run and swim

A duck is also an animal that can run, swim, and fly

What is the benefit of this design? Always keep in mind the benefits of polymorphism, so that programmers can forget about types. With an interface, users of a class don't have to pay attention to specific types, but only whether a class has a certain capability.

For example, now implement a method called "walk

public static void walk(IRunning running) { 
 System.out.println("我带着伙伴去散步"); 
 running.run(); 
} 

Inside this walk method, we don't care what kind of animal it is, as long as the parameters can run, that's fine

Cat cat = new Cat("小猫"); 
walk(cat); 
Frog frog = new Frog("小青蛙"); 
walk(frog); 
// 执行结果
我带着伙伴去散步
小猫正在用四条腿跑
我带着伙伴去散步
小青蛙正在往前跳

Even the parameter can not be "animal", as long as it will run

class Robot implements IRunning { 
 private String name; 
 public Robot(String name) { 
 this.name = name; 
 } 
 @Override 
 public void run() { 
 System.out.println(this.name + "正在用轮子跑"); 
 } 
} 
Robot robot = new Robot("机器人"); 
walk(robot); 
// 执行结果
机器人正在用轮子跑

 

Guess you like

Origin blog.csdn.net/m0_64397675/article/details/123738636