Java object-oriented programming: abstract class, interface usage example analysis

The content of this article:

  • Abstract class
  • interface
  • Similarities and differences between abstract classes and interfaces

Abstract class:

  • Although there is already a parent class, sometimes the parent class cannot directly describe some common attributes. For example, both mammals and humans are called. Generally speaking, the parent class of mammals does not accurately define the attribute of "call". Obviously, the subclass should decide how to "call", but the property of "call" is shared, then this property can be abstracted (abstract means no specific content), and the subclass can realize the specific content.

  • Abstract methods cannot be defined in ordinary classes, so abstract classes are defined.

  • Because the abstract method subclass must be rewritten, otherwise it will fail to run, so you can ensure that the subclass rewrites the abstract method.

  • An abstract class can have abstract methods or no abstract methods (abstract classes without abstract methods). When there are no abstract methods, subclass inheritance does not need to override methods.

  • Abstract classes cannot be instantiated, and objects cannot be created with the new keyword.

  • Only after the subclass covers all the abstract methods, the subclass is reified, and the subclass can create objects. If all abstract methods are not covered, the subclass is still an abstract class.

  • Abstract methods must be defined in abstract classes, and they all need to be modified by abstract. Abstract methods are only allowed to be declared, not to be defined. Therefore, there can be no braces. Abstract methods are also not allowed to be finalized.

  • Definition format of abstract class:

  • The definition format of the abstract method:

abstract class A{
 abstract void talk();//只能声明,不能具体定义
}

class B extends A{
 void talk() {
  System.out.println("run in B");//子类必须重写抽象方法
 }
}


public class Demo {

 public static void main(String[] args) {
  B b=new B();
  b.talk();

 }

}

interface:

  • Interfaces are also abstract, and the reasons for abstraction are similar to those for abstract classes. But the uses are different. The interface is generally used to define the uniform behavior of the class, while the abstract class is "inherited".

  • Class rewriting all abstract methods in an interface is called the implementation of the interface.

  • The problem solved by the interface: JAVA does not directly support multiple inheritance, but supports multiple implementations.

  • The interface body contains constant definitions and method declarations, and method definitions are not allowed.

  • The interface can be said to be a completely abstract abstract class.

  • The subclass needs to cover all the abstract methods in the interface before it can be instantiated, otherwise it is an abstract class.

  • If a class implements an interface, the class must define the methods declared in the interface. When implementing a method, the method name, return type, number of parameters, and parameter types must be consistent with the interface declaration.

  • In the interface, the modifier of the method is public abstract by default; the modifier of the constant in the interface is public static final by default; therefore, the subclass must use public to decorate the defined method.

  • The definition format of the interface:

  • Implementation of the interface:

package 接口;

interface A{
 public static final int size=100;
 public abstract void talk();
// public abstract void eat();
}
class B implements A{
 //实现A中的抽象方法
 public void talk() {
  System.out.println("B talk");
 }
}
public class Demo {

 public static void main(String[] args) {
  new B().talk();

 }

}

note:

  1. If a class declaration implements an interface, but does not implement all the methods of the interface, then the class must be an abstract class

  2. If a class does not fully implement all the methods in the interface, then this class must be an abstract class.

The similarities and differences between abstract classes and interfaces:

  • the same:

    • All abstract
    • Can not be instantiated
    • Can contain abstract methods, these abstract methods are used to describe the functions of the class, but do not provide specific implementation.
  • difference:

    • The interface is completely abstract
  • Concept design difference:

    • Abstract classes can have non-abstract methods, while interfaces are completely abstract. In fact, the difference between them can be understood as follows: A and B are both abstract classes, but because they are "parent classes", they generally have For some specific properties, it can be said that an abstract class is an abstraction of a parent class, but it cannot be separated from the relationship between the parent class and the child class; but the interface defines behavior, it does not have a strong parent-child relationship, it is only completely abstract Defines some behaviors as standards, just like various standards for components in a factory.
    • The variables and method modifiers in the interface all show that the interface is an "open and fixed behavior standard"

Some high-frequency interview questions collected in the latest 2020 (all organized into documents), there are many dry goods, including mysql, netty, spring, thread, spring cloud, jvm, source code, algorithm and other detailed explanations, as well as detailed learning plans, interviews Question sorting, etc. For those who need to obtain these contents, please add Q like: 11604713672

Guess you like

Origin blog.csdn.net/weixin_51495453/article/details/113898364