“This is the fifth day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

An upward transformation of an object

Definition: If B is A subclass or indirect subclass of A, create an object of class B and assign A reference to this object to A reference of class A

A a; B b=new B(); // A A =new B(); a=b;Copy the code

Objects on the transformation and run-time polymorphism If a class has a lot of subclasses, and these subclasses override the parent class method of an instance, take a class to create the object reference into the superclass object, get the object on the object of a transformation, the transformation on the object in the calling this instance methods Could have polymorphism

The purpose of defining an abstract class is to create a common template for other classes to inherit from

(1) An abstract class cannot create an object with the operator new. • If you want to create an object, you must subclass it, and that subclass creates the object

(2) Abstract methods can have abstract methods and ordinary methods. (3) Abstract methods can have abstract methods

(3) If a class contains abstract methods, the class must be abstract

(4) If a nonabstract class is a subclass of an abstract class, it must implement all of the parent class’s abstract methods

(5) If an abstract class is a subclass of another abstract class, the subclass may or may not implement the parent class’s abstract methods

Final Class Final classes cannot be inherited, that is, member methods that cannot have subclasses modified by final cannot be overridden

Concepts of interfaces

The ▮ interface can be used like a class to abstract a concept

▮ can use the interface program framework design, without concern about the details of the implementation, to eliminate the details of the framework design interference

▮ Through interfaces, you can implement the multi-inheritance mechanism of classes that the Java language does not have. A class can implement multiple interfaces

Declaration of interfaces

▮ interface is declared by using the keyword interface. The interface body contains constant definition and method declaration

▮ Methods in an interface are public and abstract by default if not specified

Interface implementation and use

▮ interfaces can inherit from one another, with the extends keyword. For example, if A is an interface, you can define A new interface that inherits from A in the following way.

▮ An interface can inherit from multiple interfaces. Note: Classes cannot inherit more than one. Multiple inheritance of an interface does not conflict with single inheritance of a class.

▮ A class implements one or more interfaces by using the keyword implements declaration. When implementing multiple interfaces, separate the interface names with commas (,)

▮ If a class implements an interface, then the class must implement all the methods of that interface

▮ If not specified, methods in an interface are assumed to be public and abstract by default. Classes implementing interface methods must use the public modifier

Constants defined in the ▮ interface can be shared by all classes that implement the interface

The callback interface

▮ interface callback is to point to: can use an interface class created by the object reference is assigned to the interface declaration interface variable, then the interface variables can invoke the class implements the interface method, when the interface variables called the class implements the interface method, is to inform the corresponding object call interface method, a process known as callback interface

▮ Interface callback is a manifestation of polymorphism. Different classes may have different functions when implementing the same interface. Therefore, interface callback may have different behaviors

Abstract class versus interface class

(1) Abstract classes can have abstract methods and ordinary methods, and all methods in the interface must be abstract

(2) Abstract classes can have constants and variables, but interfaces can not have variables

(3) A class can implement multiple interfaces, but can only inherit one abstract class

(4) The interface and the class that implements it do not constitute the inheritance system of the class, while the abstract class belongs to the inheritance system of a class

The inner class

(1) A class declared in a class is called an inner class

(2) Classes that contain inner classes are called nested classes

(3) A class treats its inner class as its own member

(4) The member variables of the embedded class are still valid in the inner class

(5) A method in an inner class can also call a method in an embedded class

(6) Class variables and methods can not be declared in the inner class body

(7) An embedded class can take the object declared by the inner class as a member of the embedded class

💢 declare the inner class in the class body. Suppose you have a Car class, and one of its methods requires a class People. You can define an internal People class in the Car class

class Car{ double weight; People driver; Car(double weight, int age){ this.weight = weight; driver = new People(age); } // Inner class People{int age; People(int age){ this.age = age; }Copy the code

💢 Declare inner classes in a method inside a method, sometimes a class is needed to support the algorithm, but this class is only needed in the method, not elsewhere; Or some other place with the same name; Or there are similar functions of the class, but want a different name; These cases require the creation of an inner class in the method.

Anonymous classes associated with the class a subclass object creation and subclass classes define class called anonymous classes at the same time Anonymous class is A subclass, due to the unknown is available, so you can’t use anonymous classes statement object, but you can use anonymous classes to create objects assume A is A class that has the following code is to use A subclass of class A (anonymous classes) to create objects format is as follows:

New A () {... }Copy the code

Anonymous classes can inherit methods of A class, they can override methods of A class Anonymous classes are inner classes that create objects directly in A class and the reference to an anonymous object must be passed to A matching parameter so let’s say f(A A) is A method, and the parameter A is an object of class A, An anonymous object can be passed to parameter A when calling method f() in the following format:

F (new A () {... });Copy the code

Anonymous classes associated with interfaces

Assuming Comparable is an interface, Java allows you to create an anonymous object directly with the interface name and a class body that is considered to be the body of the class implementing Comparable without the class declaration. That is, the anonymous class has the following format:

New Comparable () {... }Copy the code

Assuming f(Comparable X) is a method whose parameters are interface variables, an anonymous object can be passed to parameter x when the method f() is called as follows:

F (new Comparable () {... });Copy the code