Java Fundamentals 13 - Abstract Classes, Methods and Interfaces

An abstract class

1. What is an abstract class?

Let's first take a look at how Baidu explains it:

Abstract classes are often used to represent the abstract concepts derived from the analysis and design of the problem domain. They are abstractions of a series of concrete concepts that look different but are essentially the same.

In fact, the class itself is an abstract concept, but it is also a concrete thing, so it is called a concrete class. The abstract class seems to be to further extract and divide the commonalities in the classes that we artificially divide, and further abstract. so that it does not have the characteristics of instantiating an object, it is a class that cannot be initialized .

2. Why have abstract classes? What's the point of it?

Let's look at such a picture.

We can create a cat object, we can create a dog object, but if we create a feline object or a canine object, which animal should we create?

Are you going to create a cat or a lion? Is it to create a dog or a wolf? A little bigger, if we create animal objects, that's it, so many animals, which one is created? Is it possible to create a hybrid species?

So, from here, we may have a little sense of what is an abstract class and why there is an abstract class concept, because whether it is a feline class or a canine class, objects should not be created, they should not be initialized .

But we must have them to build the tree, to inherit and generate polymorphism, which reflects the object-oriented idea of ​​java, so what should we do?

Then we only need to restrict them, only their subclasses, and concrete classes can create objects, right? This is also the purpose of abstract classes, preventing classes that should not create objects from creating objects .

3. Definition of abstract classes and methods

Abstract classes and methods are defined with the  abstract keyword.

The definition format is as follows:

Abstract class:

abstract class class name {

}

Abstract method: public abstract return value type method name (parameter);

 

public  abstract  class Employee {
 // abstract class modified by abstract keyword 
    private String name;
     private  int    number;
     private String job;
     private  double salary;
     private  double bounes;
 // Abstract method, no method body! 
    public  abstract  void work();
     public  abstract  void welfare();
}

Notice:

①Abstract methods must be written in abstract classes, but abstract methods are not necessarily written in abstract classes.

②Any subclass must override the abstract method of the parent class, or declare itself as an abstract class.

③Construction methods, class methods (methods decorated with static) cannot be declared as abstract methods.

public  abstract  class MaintainDepartment extends Employee{
 // This is a sub-abstract class of Employee 
    String CheckIn;
 // It can rewrite the abstract method in the parent class and write it as an implementation class 
    public  void work() {
        System.out.println( "Maintenance work" );
    }
    public void welfare() {
        System.out.println("撸猫");
    }
// You can also not rewrite the abstract method, and the subclass of MaintainDepartment can implement this method 
    public  abstract  void skill();
}

4. Significance and benefits of abstract methods

The meaning of the abstract method is that we put the inheritable method body in the parent class, but the parent class has no way to make any common program code that is meaningful to the subclasses. At this time, we use the abstract method, even if the method cannot be implemented. content, it is still possible to define a common protocol for subclasses.

The benefit of abstract methods is polymorphism. We can add new subclasses to the program without having to rewrite or modify the program to handle these types.

2. Interface

1. What is an interface?

Interface (English: Interface), in the JAVA programming language, is an abstract type and a collection of abstract methods. Interfaces are usually declared as interfaces. A class inherits the abstract methods of the interface by inheriting the interface.

The interface only describes the methods it should have, and does not have a specific implementation. The specific implementation is done by the implementation class of the interface (equivalent to a subclass of the interface). This separates the definition and implementation of the function and optimizes the program design.

For example: the power plug of the computer is an interface, it is to realize the function of charging, so we don't have to care about how to charge (current, voltage...), as long as the power cord is inserted into this interface, the function of charging can be realized. .

2. Why have an interface?

Let's look at a problem first, classes can only be single-inherited, because multiple inheritance may cause the problem of " deadly squares (squares that look like playing cards)"

Therefore, in order to solve the problem of multiple inheritance without causing the problem of deadly blocks , the interface appeared gorgeously on our stage.

 How does the interface solve this problem?

It makes all methods abstract! In this way, subclasses must implement this method, so the virtual machine will not be confused about which inherited version to use when executing.

3. Definition of the interface

The interface keyword is required when defining an interface.

Note: The .java file is still where the interface is defined , although the .class file will still be generated after compiling with the interface keyword when declaring it . This allows us to think of an interface as a special class that contains only functional declarations.

public interface interface name {

abstract method 1;

abstract method 2;

abstract method 3;

}

public interface HighPoint {
public void autoPark();
public void autoRun();
}

4. The class implements the interface

The relationship between a class and an interface is an implementation relationship, that is, a class implements an interface. The implemented actions are similar to inheritance, but the keywords are different, and the implementation uses implements .

After the class implements the interface, the class will inherit the abstract method in the interface. At this time, the class needs to rewrite the abstract method to complete the specific logic.

public class NewPhone extends Phone implements IPlay{
    public void Call() {
        System.out.println( "Keyboard" );
    }
    public void SendMessage() {
        System.out.println( "Key" );
    }
    public void PlayGame() {
        System.out.println( "Honor of Kings" );
    }
}

5. Similarities and differences between interfaces and abstract classes

Same point :

  • All are located at the top of inheritance and are used to be implemented or inherited by other classes;
  •  Neither can directly instantiate an object ;
  • All contain abstract methods , and their subclasses must override these abstract methods;

Difference :

  •  Abstract classes provide implementation for some methods , avoid subclasses from repeatedly implementing these methods, and improve code reuse; interfaces can only contain abstract methods;
  • A class can only inherit one direct parent class (possibly an abstract class), but it can implement multiple interfaces; (interfaces make up for Java's single inheritance)
  •  The abstract class is what you should have in this thing, and the inheritance system is an is..a relationship
  •  Interfaces are extras in this thing, inheritance is a like..a relationship

Choice of both :

 Interfaces are preferred , and abstract classes are used as little as possible;

Abstract classes are used only when it is necessary to define the behavior of subclasses and provide common functions for subclasses;

6. Member characteristics of the interface

  • Variables can be defined in the interface, but the variable must be modified by a fixed modifier, public static final , so the variables in the interface are also called constants, and their values ​​cannot be changed. We will explain the static and final keywords later.
  • Interfaces can define methods, and methods also have fixed modifiers, public abstract
  • Interfaces cannot create objects.
  • Subclasses must override all abstract methods in the interface before subclasses can be instantiated. Otherwise the subclass is an abstract class.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324938858&siteId=291194637