JAVA Learning-Inheritance, Polymorphism, Interface

recollections Learning methods should have bi...
recollections

Learning methods should have bigger problems, but more time to make up for them! Go on, you can do it!

Subsection Inheritance

1. Basic Meaning of Inheritance

To put it all aside, inheritance means that when new features are introduced, you need to write the previous code again, which is more cumbersome to invoke. It also feels like you have used an object-oriented idea. You can keep the code private by not showing you the code you used before, and then know that those functions have been implemented. Just extend the base classes and derived classes.It can be said that "the base class is a large category, and the derived class is a base class," and then the derived class is something unique to itself.

Subsection polymorphism

1. Basic meaning of polymorphism

Parent references point to subclass objects and follow a fixed formula. The advantage is that it is easy to access objects of a new specific class

2. General Summary of Polymorphism and Inheritance

package cn.java.test.demo3; public class TestMethod { public static void main(String[] args) { Fu1 fu = new Zi1(); //A parent reference calls a subclass object Fu2 fu2 = new Fu2();//Fu2 and Zi2 are inheritance relationships, and the object created here is Fu2 Zi2 zi2 = new Zi2();//Fu2 and Zi2 are inheritance relationships, and Zi2 objects are created here //First, you create a polymorphism where the parent reference points to the child object System.out.println(fu.num);//The result here is 10, where the variable is num inside the parent class of the call fu.method();//The result here is 30, which means that the method inside the subclass is first found and then the method directly calls the object inside the class System.out.println("Here's a gorgeous dividing line+++++++++++++++++++++++++++++++++++"); System.out.println("Here is the object that created the parent class"); System.out.println(fu2.num);//The input result here is 2, which means this is a member variable that calls the parent class fu2.method();//The result here is 2 to indicate that this is a member method that calls the parent class System.out.println("Here's the object that created the subclass"); System.out.println(zi2.num);//Member variables of subclasses are called here, and member variables of subclasses are used here zi2.method();//This calls a member method that exists in both the subclass and the parent class, where the member method in the subclass is used //Finally, you can get rules for calling member variables in polymorphic or inheritance /* The first one, called directly with the object name, and then output in the sout, is to see on the left, who objects use whose member variables, and if not, go up Second, there are output methods in the two classes, which are called indirectly. The output is a variable, depending on what keyword the current variable has, this is the variable of the current class, super is the object of the parent class */ zi2.methodShow();//When invoking member methods, look to the right, and who's new will use who's method fu2.methodShow();//Here is the member method of the parent class called, and the output is the parent class's fu.methodShow();//The use of polymorphic methods, where new is a subclass object of new, is the subclass method used here /* Tips for member variables and methods: Member variables: Compile to the left, run to the left, or view to the left. Explanation: First look at what kind of object it is, such as Fu3 Class and Zi3 have a common class with the same output However, the Fu3 and Zi3 classes have their own unique classes, which are now called using polymorphism */ Fu3 fu3 = new Zi3();//This is a polymorphic call, first the parent reference calls the subclass object //Here we call member variables, compile to see if there is any in Fu3? Yes, compile to see if there is no problem, so you can point out, run to see how many num s are in Fu3? 2, the result is 2. System.out.println(fu3.num); //Membership method is called here. Is there a methodMe method in Fu3 on the left? Yes, there is no problem compiling it, so you can point it out. Run to see what methodMe method is in Zi3? Yes, B, so the output is B. fu3.methodMe(); //Compile to see if there is a methodMeFu method in Fu3 on the left? Yes, there is no problem with compilation; Run to see if there is a methodMeFu method in Zi3 class; No, if there is no, look up to the parent class. If there is a parent class, it can run fu3.methodMeFu(); // fu3.methodMeZi();Why, error, during compilation, if there is no methodMeZi method in Fu3 class, the red line will be reported and there is no way to point through the object } }

Specifically summarized is how subclasses and parent classes are implemented in the case of polymorphism and inheritance when using member variables or member methods with duplicate names

Section Interface

1. Meaning of interface

In fact, it feels a little like inheritance, but there can be static method, default method, Abstract method, which is an abstract class itself, so after writing the abstract method, you can not add abstract keyword after public

2. Notice for interface

1. An interface itself cannot be used to create objects. Only an implementation class is created. Generally, implement is abbreviated by several letters after the class name. This is the implementation class of an interface, and then the object is created.

2. An implementation class implements multiple interfaces, each separated by commas

3. Static code blocks and construction methods cannot be created in interfaces. Variables are usually created with public static final modifiers and keywords. All variables are constants, called directly with class names, and the names of variables are capitalized.

4. The abstract method created in the interface must override the override in the implementation class. If not, the implementation class will be changed to an abstract class.

5. The default method, as defined in the interface, can have a body of methods and is not overridden

6. Implementation classes of interfaces can also inherit other methods, and methods that inherit have a higher priority in using renamed methods or variables

Subsection Static Code Block Supplement

1. Static Code Block

Static code blocks cannot be written in methods, they can only be written outside the method body

10 October 2021, 12:44 | Views: 6427

Add new comment

For adding a comment, please log in
or create account

0 comments