Four basic characteristics of java class

Object-oriented features: abstraction , encapsulation , inheritance , polymorphism .

abstract
  Abstract class: A class containing abstract methods is called an abstract class, but it does not mean that there can only be abstract methods in the abstract class. It can have ordinary member variables and methods just like ordinary classes.

  1. Abstract classes cannot be instantiated. Subclasses of abstract classes must give concrete implementation of abstract methods in the abstract class, unless the subclass is also an abstract class
  2. Abstract classes do not necessarily contain abstract methods, but classes with abstract methods must be abstract classes
  3. The abstract method in the abstract class is just a statement, does not contain the method body, that is, it does not give the specific implementation of the method, that is, the specific function of the method
  4. Constructor, class methods (methods decorated with static) cannot be declared as abstract methods
  5. Classes that are defined as abstract need to be inherited by subclasses, but classes that are modified to final cannot be inherited and rewritten. The two cannot be used together for modification.

  Interface: Interface is a special abstract class. The methods in the interface are abstract methods, abstract is not written by default, so abstract methods in abstract classes cannot use access modifiers. The members declared in the interface default to static final members (whether it is a basic data type or a reference type) and must be initialized. Characteristics of the interface:

  1. Every method in the interface is also implicitly abstract, and the methods in the interface will be implicitly designated as public abstract (only public abstract, other modifiers will report errors)
  2. The interface can contain variables, but the variables in the interface will be implicitly designated as public static final variables (and can only be public, modified with private will report compilation errors)
  3. The methods in the interface cannot be implemented in the interface, and the methods in the interface can only be implemented by the class that implements the interface.

The difference between interface and abstract class:

  1. Methods in abstract classes can have method bodies, but methods in interfaces cannot
  2. Member variables in abstract classes can be of various types, while member variables in interfaces can only be of public static final type (implicit public)
  3. The interface cannot contain static code blocks and static methods (methods decorated with static), and abstract classes can have static code blocks and static methods. [After JDK1.8, there can be static methods and method bodies in the interface]
  4. A class can only inherit an abstract class, but a class can implement multiple interfaces
  5. Abstract classes have constructors, while interfaces have no constructors.

Package : Refers to a method of packaging and hiding the implementation details of an abstract functional interface.

inherit
Inherited features:

  1. Subclasses have non-private attributes and methods of the parent class
  2. Subclasses can have their own properties and methods, that is, subclasses can extend the parent class
  3. Subclasses can implement parent class methods in their own way
  4. Java inheritance is single inheritance
  5. Improved coupling between classes

Polymorphism: Assigning a subclass object to a parent class reference will produce polymorphism. For example: Father son = new Son () The object son at this time can only call the methods of Father (refer to those methods that the subclass overrides or inherits from the parent class), but cannot call some methods defined by itself. Because it emphasizes in polymorphism: when writing a java program, a reference type variable can only call a variable of its compile-time type, not a variable of its run-time type.
  Three necessary conditions for polymorphism: 1, inheritance 2, rewrite 3, parent class reference points to
  the role of subclass object polymorphism:

  1. It is not necessary to write function calls for each subclass, you can directly view different subclasses as the parent class, shield the differences between subclasses, and improve the common rate / reuse rate of the code
  2. The parent class reference can call the functions of different subclasses, improving the scalability and maintainability of the code

  The polymorphic manifestation can be the rewriting or overloading of the parent class method. [Overload refers to a method that can have the same modifier, return value, method name, but different number or order of parameters in a class. Rewriting refers to the method of rewriting the parent class after the subclass inherits the parent class. Rewriting is the subclass rewriting the implementation process of the parent class's allowed access method, and neither the return value nor the formal parameters can be changed. ] Polymorphic implementation: 1, rewrite; 2, interface; 3, abstract class and abstract method.

Published 162 original articles · praised 58 · 90,000 views

Guess you like

Origin blog.csdn.net/ThreeAspects/article/details/105569484