An abstract class:
Abstract class concept:
In Java, you can define a method without a method body. The concrete implementation of the method is completed by the subclass, which is called abstract method. The class containing abstract method is abstract class.
Classes that are usually decorated with abstract in programming statements are abstract classes. An abstract class is incomplete and can only be used as a base class. In the object-oriented approach, abstract classes are primarily used for type hiding and acting as global variables.
Abstract class features:
1) Abstract classes and methods must be modified with the keyword abstract. 2) Abstract classes do not necessarily have abstract methods, but classes with abstract methods must be abstract classes. 3) Abstract classes cannot be instantiated
Syntax format:
Modifier abstract Return value type method name ([parameter list]);
[public] abstract class name {// Can define all contents of the class // can define all contents of the interface}Copy the code
Abstract class declaration:
Abstract classes can have no abstract methods; But once a class has abstract methods, that class must be declared abstract.
The main differences between abstract classes and ordinary classes:
1) The abstract method must be either public or protected (because if it is private, it cannot be inherited by subclasses and subclasses cannot implement the method), and public by default. 2) Abstract classes cannot be used to create objects; 3) If a class inherits from an abstract class, the subclass must implement the abstract methods of the parent class. If a subclass does not implement the abstract methods of its parent class, it must also be defined as an abstract class.
Interface:
In software engineering, interface refers to a method or function that is called by others.
Syntax: modifier interface Interface name {[constant]; [Abstract method]; }
[public] interface interface name {// define constant public static final = value; // Define abstract method public abstract return value type method name (datatype parameter name,...) ; }Copy the code
The default access modifier is public. All methods in the interface cannot have a concrete implementation. When a class implements an interface, it needs to implement all abstract methods in the interface; otherwise, it needs to set the class as an abstract class. Interfaces can include constants, and the default public static final interface can also implement inheritance, and can inherit multiple parent interfaces
Similarities between abstract classes and interfaces:
1) Cannot be instantiated;
2) contains unimplemented method declarations;
3) Derived classes must implement unimplemented methods, abstract classes are abstract methods, and interfaces are all members (not only methods but other members).
The difference between abstract classes and interfaces:
1) Abstract classes can provide implementation details of member methods, while interfaces can only have public abstract methods;
Member variables in an abstract class can be of various types, while member variables in an interface can only be of public static final type.
3) Interfaces cannot contain static code blocks and static methods, whereas abstract classes can have static code blocks and static methods;
4) A class can inherit only one abstract class, while a class can implement multiple interfaces.
5) An abstract class is an abstraction of a thing, that is, an abstraction of a class, while an interface is an abstraction of behavior.
The inner class:
A class defined inside another class.
Inner classes can be divided into:
1) Member inner class:
- Define the location of the member (attribute, method) of the external class
- There are four access modifiers you can use to modify inner classes
- We can define attributes and methods inside an inner class:
public class Outer { private int a = 1; Class Inner{public void print(){// Access the private property of the external class system.out.println (a); }}}Copy the code
Create an inner class object: First create an outer class object, then use the outer class object to create an inner class object
Outer out = new Outer(); Outer.Inner in = out.new Inner(); in.print();Copy the code
2) Static inner class:
-
Static inner classes are inner classes preceded by static
-
Static inner classes do not need to create external objects
-
Only static properties or methods of an external class can be accessed
-
To access a static member of a static Inner class: outter.inner.
public class Outer { private int a = 1; private static int b = 2; // Static class Inner{public void print(){system.out.println (b); }}}Copy the code
3) Local inner class:
-
Local inner classes can only be defined for use in methods
-
Local inner classes cannot contain static variables
-
Local inner classes can contain local variables that are final in nature:
public class Outer { private int a = 1; Private void print(final int b){// Partial Inner class {public void print(){system.out.println (a); System.out.println(b); }}}}Copy the code
4) anonymous inner class :(a special case of local inner class)
- The anonymous inner class does not know the class name, so it cannot define a constructor and can only use the default constructor with no arguments.
- When used only once, use anonymous inner classes
- An anonymous inner class must know its parent class or the interface that it implements
The meaning of using inner classes:
1) Enhanced encapsulation, hide the inner class in the outer class, do not allow other classes to access the inner class
2) Added a maintainability to the code
3) The inner class can directly access the members of the outer class