Chapter 8 Polymorphism
Static methods do not have polymorphism because they are associated with classes rather than individual objects.
In Java, except the final and static methods, all other methods are bound later.
The virtual functions in the constructor are dynamically associated, but in C ++ it is the tij book p163 of static Association.
Sub-classes cannot change the visibility of functions in the base class. If a function in the base class is public, the function still needs to be public when it is inherited.
Java se5 adds a return type, which allows the subclass to return the child type of the base class in the virtual function. This function is used to implement the protype design mode.
Public class grain
{
Public grain cloneobject ()
{
Return new grain ();
}
}
class wheat extends grain
{< br> Public wheat cloneobject ()
{< br> return New wheat ();
}< BR >}< br> downward Transformation:
Base B = new derive ();
(derive) B ). deriveclassfunction ();
If the transformation fails, a classcastexception exception is returned.
Chapter 9 Interfaces
Abstract abstract class or method keyword
Keyword of the interface class. The method in the interface can only be public, and only the prototype is not implemented.
All member variables in the interface are public static final by default.
Both the base class and subclass share the static variables of the base class.
Differences between abstract classes and interface classes:
Abstract classes can have function definitions. Interface classes cannot have function definitions.
Syntax for implementing multiple interfaces:
Class hero extends human implements fly, swim, fight {}
The member variables in the interface cannot be blank final, but can be initialized by a non-constant expression.
Public interface Rand {int val = Rand. nextint (10 );}
Chapter 10 Internal categories
Internal class objects can be directly created in non-static methods of external classes, but internal classes can only be created through external functions in addition to non-static methods of external classes.
Example:
Outter out = new outter ();
Outter. Inner in = out. getinner ();
. This. New
. This returns the reference of the external class in the internal class function.
Public class outter {
Public class inner {
Outter getoutter () {return outter. This}
}
. New creates an internal class object through reference of an external class object.
Outter out = new outter ();
Outter. Inner in = out. New inner ();
If the inner is a static internal class, you can directly create an internal class if the internal class is a static class.
Outter. Inner in = new outter. Inner ();
Classes in the scope and method are only available within the scope and cannot be accessed outside the scope.
Internal classes can directly access external member variables. However, when accessing an object defined externally, such as a parameter of an external class function, this parameter must be declared as final.
Anonymous class:
The anonymous class cannot have constructors, but the member variables of the anonymous class can be initialized through instance initialization {}.
The anonymous class is equivalent to the subclass of the specified class after new, and return new base () {void baseclassmethod () {}} is equivalent to returning a base subclass object. For more information, see exercise 12 p199.
Nested class:
Declare the internal class as static.
It differs from internal classes:
1. The internal class has a reference pointing to the external class, but the nested class does not. Therefore, the nested class cannot directly access non-static members or methods of the external class.
2. Internal classes cannot have static members or methods, but Nested classes can.
You can test Code In the nested class, you can delete the class file of the nested class when releasing the product. For example, outter $ inner. Class
Why Internal classes are required:
Each internal class can inherit from one (Interface) implementation independently. Therefore, no matter whether the peripheral class has inherited the implementation of a (Interface), it has no impact on the internal class.
The internal class effectively implements "Multi-inheritance ".