Mp.weixin.qq.com/s/gIqlS3pFx…
Basic concepts of abstract methods:
– Abstract methods mainly refer to methods that cannot be implemented concretely, which are embodied in the absence of method body and the use of abstract keyword modification;
Syntax format:
– Access control character abstract Return value type method name (parameter list);
Public abstract void cry();
Abstract class concept:
– Abstract classes are classes that cannot be concretely instantiated, that is, cannot construct objects and use the abstract keyword;
Matters needing attention:
(1) An abstract class can have member variables, constructors, and member methods.
(2) An abstract class may have an abstract method or no abstract method.
(3) Classes with abstract methods must be abstract classes, so strictly speaking, classes with abstract methods and the use of the abstract keyword can be considered abstract classes in the real sense.
Practical significance:
– The purpose of an abstract class is not to create objects but to be inherited. When a class inherits an abstract class, it must override the abstract method, otherwise the class becomes an abstract class.
Abstract classes are called template design patterns because they are mandatory and prescriptive for subclasses
Experience sharing:
– it is recommended to use in the future development of polymorphic the syntax of the format, the parent class reference direct call all methods must be in the parent class method, if want to change a subclass only need to amend the new key at the back of the types and the other position unchanged will begin immediately, thus improve the maintainability and extensibility of the code.
The disadvantage of this approach is that a superclass reference requires a cast if you want to call a method unique to the subclass.
2
Basic concepts of interfaces:
– An interface is essentially more abstract than an abstract class, in that all methods are abstract methods.
– The keyword for defining a class is class, and the keyword for defining an interface is interface.
The relationship between classes and interfaces:
The difference between abstract classes and interfaces:
(1) The class keyword is class, and the interface keyword is interface.
(2) Extends extends; implements implements;
(3) Abstract classes can have constructors, but interfaces do not;
(4) The inheritance abstract class supports single inheritance, while the implementation interface supports multiple implementations;
(5) Abstract classes can have member variables, but interfaces can only have constants;
(6) An abstract class can have member methods, while an interface can only have abstract methods;
(7) Adding methods to an abstract class can not affect the subclass, while adding methods to an interface usually affects the subclass;
(8) Add features from JDK1.8 to allow non-abstract methods in the interface, but need to use the default keyword;
3
Inner class basic concepts:
– When the definition of a class is placed inside the body of another class, that class is called an inner class, and the class in which the inner class resides is called an outer class.
– Contents of the class body: member variables, member methods, constructors, static members, construction blocks and static code blocks, inner classes.
Syntax format:
Basic functions:
– When a class exists only to serve a single class, the class can be defined as the inner class of the class served, and the inner class can directly access the outer class. Class without the need to provide public GET and set methods.
Basic classification:
Normal inner class – Puts the definition of one class directly into the body of another class.
Static inner classes – Inner classes decorated with the static keyword.
———— Only inner classes can be decorated with the static keyword.
Local inner class – Puts the definition of a class directly inside the method body.
————- only works in the method body.
Anonymous inner classes – Mainly inner classes that have no name.
4
Anonymous inner class:
Syntax format:
– Interface/superclass type Reference variable name = new interface/superclass type (){override method};
Experience sharing:
When a reference to an interface type is used as a parameter to a method, the argument can be passed in one of two ways:
A. A custom class implements the interface and creates objects of the class to be passed as arguments;
B. Use the syntax format of the anonymous inner class to get a reference to the interface as an argument;