Java learning notes-inheritance and polymorphism (medium), java learning notes
1. Develop superclass through inheritance)
2. Use the super keyword to evoke the constructor of superclasses
3. overwrite the method in the superclass
4. differentiate override and overload
5. Explore the toString () Class in the Object class
6. Discovery of polymorphism and dynamic binding
7. Explain why downward transformation is necessary
8. Explore the equals method in the Object class
9. storage, retrieval, and implementation of ArrayLis objects
10. Use the ArrayList class to implement Stack
11. Use data and methods in the superclass, protected
12. Use the final module to disable coverage of classes and Methods
5. Explore the toString () Class in the Object class
Every class in Java is extended from the java. lang. Object class. If there is no special rule to inherit the class during definition, the default parent class is Object. For example
Public class ClassName and public classClassName extends Object
The two statements are the same. The String class and the StringBuilder class both come from the Object class and learn the toString () method.
public String toString()
The return value of toString is the string that describes the object. By default, the returned string is the class name of a series of objects. We usually need to rewrite toString when using it.
Note: You can also use an Object to evoke System. out. println (Object) or System. out. println (Object)
6. Discovery of polymorphism and dynamic binding
Three object-oriented features: encapsulation, inheritance, and polymorphism.
Two useful terms are introduced: subtype of the derived class and supertype of the super class. The subclass defines the subtype. For example, the original research circle is a ry. Circle is a geometry. Therefore, the GeomericObject class is the parent class, and the Circle class is a subclass. Therefore, the Circle class is the subtype of GeomericObject. GeomericObject is the supertype of Circle.
The inheritance relationship allows the subclass to inherit some features of the parent class and add new features. The subclass is a special parent class, And all instances of the subclass can be considered as instances of the parent class, otherwise. That is, we understand that the circle is a ry, but the ry is not a circle.
Dynamic binding
Define methods in the superclass and overwrite them in the subclass. Just as the previous toString () method has been defined in the Object class, write a method in GeometricObject to overwrite it, follow the following code:
Object O = new GeometricObject;System.out.println(o.toString());
In the above example, the toString () of the method called by o (). To study this problem, we can think of two aspects. For example, in our daily life, we often declare that "xxx is xxx, and XX statements are made, but what is actually ???"
An official statement will be issued when something affects an organization. So there are two ways to declare xxx. In fact, the idea in xxx and Java is that declare type and actual type are the Declaration type and actual type, the type of a variable must be declared as its declared type. In the above example, the O type is Object. If the actual type is encountered, the actual type is referenced according to the variable class, therefore, the actual type of O is the GeometricObject class, which is dynamic binding. (Dynamic system? Declare a class, and then implement it by its subclass polarity. The generated object is generated based on the subclass under specific circumstances. At this time, the object is bound to the subclass) in the future, many objects are generated by different subclasses. These objects are bound to their classes:
7. Explain why downward transformation is necessary
In the previous example, we used a more professional term to convert a certain xxx type to the next type and convert a variable from the original type to the actual type. Convert an object to an object in actual use. Just like the preceding statement: Object o = new student (); is implicit type conversion, which is also available in C language, such as the int a = (int) b; some type conversion. For the same value assignment operation, if it is a value assignment operation a = B; For the analogy, there is no object assignment-like Operation student B = o; then there is an error. Similar to the previously mentioned "XXX is XXX", this relationship is irreversible.
Consider the following example:
object myObject = new Circle(); if(myObject instanceof Circle){ System.out.println("The Circle diameter is "+((circle)myObject).getDiameter()); ...}
The myObject variable is declared as an Object class. The declaration type determines which method will match during compilation and myObject is used. getDiameter () will cause compilation errors because the Object class does not have the getDiameter method. During compilation, the compiler cannot find the matching method. Therefore, you must tell the compiler that xxx is a specific instance of xxX. To better understand the transformation, you can follow the ideas below: fruit: Apple and Orange, fruit -- superclass, Apple -- subclass, Orange -- subclass, and Apple is fruit, therefore, fruit variables can be safely used. However, fruit is not a fruit, so implicit type conversion is required during use.
8. Explore the equals method in the Object class
The equals method is also available in the Object class: The syntax is as follows:
Public boolean equals (Object o );
When using:
Object1.equals (object2 );
The default implementation is:
public boolean equals(Object obj){ return (this == obj); }
This implementation process checks whether the two parameters point to the same object. At this time, the = is used and can be redefined in the Self-written class.
Notes:
= The symbol applies to and compares two initial data types to determine whether the two variables have the same reference. The equals method is used to test whether the two objects have the same content. = The operator has a higher priority than the equals method, because = is used to check whether it points to the same object.
Note:
Using equals (SomeClassName obj) (that is, equals (Circle c) in subclass to overwrite common errors will be involved in future notes.