(Memories from last season:
1. final keywords
· Retouch classes cannot be inherited
The retouch method cannot be overridden
The modified variable is a constant, the overall constant (publicstaticfinal)
2. Generic classes and interfaces
Generic class: a class that includes only one generic method. The generic method only needs to be declared and does not need to be implemented. There must be subclasses
Interface: A class that only includes general methods and overall constants - an interface, which must also have subclasses
In practice, a class seldom inherits a fully implemented class, basically it inherits the general class and implements the interface.
Top knowledge points this season:
1. The polymorphism of the target
2. instanceof keywords
3. Object class
target polymorphism
be careful:
In order to clarify the concept, the general class inheritance relationship is now used first.
Upward transformation:
classA
{
publicvoidfun1()
{
System.out.println(Class A ===gt;publicvoidfun1());
}
publicvoidfun2()
{
//The fun2 method calls the fun1 method
this.fun1();
}
}
classBextendsA
{
//Override the fun1() method in class A
publicvoidfun1()
{
System.out.println(Class B ===gt;publicvoidfun1());
}
publicvoidfun3()
{
System.out.println(Class B ===gt;publicvoidfun3());
}
}
publicclassDemo01
{
publicstaticvoidmain(Stringargs)
{
Bb=newB();
Aa=newA();
b.fun1();
a.fun2();
b.fun3();
}
}
The target polymorphisms are now transforming each other on top of each other~
classA
{
publicvoidfun1()
{
System.out.println(Class A ===gt;publicvoidfun1());
}
publicvoidfun2()
{
//The fun2 method calls the fun1 method
this.fun1();
}
}
classBextendsA
{
//Override the fun1() method in class A
publicvoidfun1()
{
System.out.println(Class B ===gt;publicvoidfun1());
}
publicvoidfun3()
{
System.out.println(Class B ===gt;publicvoidfun3());
}
}
publicclassDemo02
{
publicstaticvoidmain(Stringargs)
{
//declare a parent class target
Aa=null;
//newB() is the conversion of the subclass target to the parent class target
a=newB();
a.fun1();
}
}
Now let's see which class method is called by a.fun1()~
classA
{
publicvoidfun1()
{
System.out.println(Class A ===gt;publicvoidfun1());
}
publicvoidfun2()
{
//The fun2 method calls the fun1 method
this.fun1();
}
}
classBextendsA
{
//Override the fun1() method in class A
publicvoidfun1()
{
System.out.println(Class B ===gt;publicvoidfun1());
}
publicvoidfun3()
{
System.out.println(Class B ===gt;publicvoidfun3());
}
}
publicclassDemo02
{
publicstaticvoidmain(Stringargs)
{
//declare a parent class target
Aa=null;
//newB() is the conversion of the subclass target to the parent class target
//After the subclass target is transformed to the parent class target, the method called must be an overridden method
a=newB();
a.fun1();
a.fun2();
}
}
After the subclass target is transformed to the parent class target, the method called must be the overridden method, which is the upward transformation of the target~
Downcast:
classA
{
publicvoidfun1()
{
System.out.println(Class A ===gt;publicvoidfun1());
}
publicvoidfun2()
{
//The fun2 method calls the fun1 method
this.fun1();
}
}
classBextendsA
{
//Override the fun1() method in class A
publicvoidfun1()
{
System.out.println(Class B ===gt;publicvoidfun1());
}
publicvoidfun3()
{
System.out.println(Class B ===gt;publicvoidfun3());
}
}
publicclassDemo03
{
publicstaticvoidmain(Stringargs)
{
//declare a parent class target
Aa=null;
//newB() is the conversion of the subclass target to the parent class target
//After the subclass target is transformed to the parent class target, the method called must be an overridden method
a=newB();
a.fun1();
a.fun2();
a.fun3();
}
}
Now let's see if we can call a.fun3() ha~
The program prompts that the fun3() method cannot be found. There is no fun3() method in class A. If we must call it, we must use downcasting~
classA
{
publicvoidfun1()
{
System.out.println(Class A ===gt;publicvoidfun1());
}
publicvoidfun2()
{
//The fun2 method calls the fun1 method
this.fun1();
}
}
classBextendsA
{
//Override the fun1() method in class A
publicvoidfun1()
{
System.out.println(Class B ===gt;publicvoidfun1());
}
publicvoidfun3()
{
System.out.println(Class B ===gt;publicvoidfun3());
}
}
publicclassDemo03
{
publicstaticvoidmain(Stringargs)
{
//declare a parent class target
Aa=null;
//newB() is the conversion of the subclass target to the parent class target
//After the subclass target is transformed to the parent class target, the method called must be an overridden method
a=newB();
// Downward transformation can be performed, and the mandatory method is required~
Bb=(B)a;)