Abstract classes and interfaces

Keywords: Java Back-end

1, Abstract class

1. A class containing abstract methods is called an abstract class

2. Abstract methods. A method without specific implementation is modified by abstract

eg:

abstract class Shape {
    public int a;
    public void func(){
        System.out.println("test");
    }
    public abstract void draw();
}

·Abstract is added before the draw method to indicate that it is an abstract method. At the same time, the abstract method has no method body (without {}, concrete code cannot be executed)

·For a class containing abstract methods, you must add the abstract keyword to declare that it is an abstract class

3. Non instantiatable

Create a new Shape in the main function. The compilation results are as follows:

 

    public static void main(String[] args) {
        Shape shape = new Shape() {
         
        }
    }

4. It cannot be instantiated, so it can only be inherited

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

From the code of the first point, we can see that abstract classes can contain the same member variables and methods as ordinary classes. Similar to ordinary classes, rules can be overridden or used directly by subclasses.

6. Ordinary classes inherit abstract classes. Ordinary classes need to override all abstract methods in abstract classes

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

  At this point, we define a class Rect( ♦), The class Rect inherits from the abstract class Shape through extengs.

At this point, we need to override the draw method in the abstract class.

7. Maximum function → inherited

8. If an abstract class a inherits an abstract class B, the abstract class A may not implement the abstract method of the parent class

abstract class A extends Shape{
    public abstract void funcA();
}

  At this time, we define a new abstract class A. class a inherits from the abstract class Shape, so class a can not override the abstract methods in Shape.

9. Combined with the eighth point, when class A is inherited by a common class C again, C must override the abstract methods in the two abstract classes a and B

class C extends A{
    @Override
    public void funcA(){

    }
    @Override
    public void draw(){
        
    }
}

  We define a new ordinary class C. class C inherits from abstract class A, and class a inherits from class Shape. At this time, class C needs to override the abstract methods in class A and class Shape.

10. Abstract classes cannot be final decorated, nor can abstract methods

Final modification represents non modifiable and non inheritable. Therefore, abstract classes cannot be modified by final, and abstract methods are similar.

2, Interface

1. Use interface to decorate

interface Name{

}

The code format is as above  

2. Ordinary methods in the interface cannot have specific implementation. If they have to be implemented, they can only be implemented through the keyword default

interface IShape{
    public abstract void draw();//Default public abstract
    default public void func(){
        System.out.println("Default method");
    }
}

 

3. There can be static methods in the interface

4. All the methods inside are public

5. Abstract methods are public abstract by default

interface IShape{
    void draw();//Default public abstract
    default public void func(){
        System.out.println("Default method");
    }
}

  Compared with the code in point 2, deleting public abstract before the abstract method can also compile successfully. The system defaults to public abstract.

6. An interface cannot be instantiated with the keyword new

  public static void main(String[] args) {
        IShape iShape = new IShape() {

        };
    }

 

7. The relationship between classes and interfaces is implemented through implements

interface IShape{
    public abstract void draw();//Default public abstract
   
}
class Flower implements IShape {
    @Override
    public void draw() {
        System.out.println("❀");
    }
}
class Triangle implements IShape{
    @Override
    public void draw(){
        System.out.println("△");
    }
}

  At this time, we write an interface IShape and two classes Flower and Triangle. As can be seen from the code snippet, both classes implement the interface through implements.

8. When a class implements an interface, it must override the abstract methods in the interface

  In the code snippet at point 7, we can see that the draw method in the interface IShape is overridden in both the class Flower and the class Triangle.

9. The members in the interface are modified by public static final by default

interface IShape{
    public abstract void draw();//Default public abstract
    default public void func(){
        System.out.println("Default method");
    }
}

10. When a class implements an interface and rewrites this method, it must be preceded by public

  Because the methods in the interface are public, and the modification scope cannot be narrowed during implementation, it can only be public

11. A class can inherit an abstract class or ordinary class through extensions, but can only inherit one. At the same time, multiple interfaces can also be implemented through implements

class Animal {
    protected String name;

    public Animal(String name) {
        this.name = name;
    }
    public void eat(){
        System.out.println(name+"eat");
    }
}
interface IJumping{
    void jump();
}
interface ISwimming{
    void swim();
}
class Flog extends Animal implements IJumping,ISwimming{
    public Flog(String name) {
        super(name);
    }
    @Override
    public void jump(){
        System.out.println(this.name+"Jumping");
    }
    @Override
    public void swim(){
        System.out.println(this.name+"Swimming");
    }
}

  At this time, we define an Animal class as the parent class and define two interfaces → IJumping and ISwimming. At this time, the subclass flow class inherits from the parent class Animal and implements IJumping and ISwimming interfaces. In the flow class, we can see that the abstract methods of the two interfaces are overridden.

 

12. Interface and relationship between interfaces

Interfaces and interfaces use extensions to operate. At this time, it means: expand

An interface B extends the functions of another interface A through extensions. At this time, when A class C implements this interface B, when rewriting, it is not only the abstract method of B, but also the functions extended from interface A

interface IJumping{
    void jump();
}
interface Irunning extends IJumping{
    void run();
}
class Cat extends Animal implements Irunning{
 public Cat(String name){
     super(name);
 }
    @Override
    public void jump() {
        System.out.println(this.name+"Jumping");
    }

    @Override
    public void run() {
        System.out.println(this.name+"Running");
    }
}
  public static void runFunc(Irunning irunning){
        irunning.jump();
        irunning.run();

    }
 runFunc(new Cat("Wang Yiming"));

At this time, the Irunning interface inherits from the IJumping interface and has its own abstract methods in the Irunning interface. At this time, a Cat class implements the Irunning interface, so it is necessary to rewrite the abstract methods in the IJumping interface and Irunning interface in the Cat class.

Then, declare a static runFunc method in the main function and a Cat instance through the keyword new. The output results are as follows:  

 

 

13. Expand knowledge  

The final keyword represents the final and unalterable

Four common uses:

① Can be used to decorate a class

Function: this class cannot have any subclasses

② Can be used to modify a method

Function: when the final keyword is used to modify a method, this method is the final method and cannot be overridden

③ Can be used to modify a local variable

Function: when fina keyword is used to modify a local variable, the local variable cannot be changed, "once assigned, it will remain unchanged for life".

④ Can be used to modify a member variable

Function: the member variable modified with final is immutable

Posted by scarlson on Tue, 30 Nov 2021 03:35:07 -0800