When a parent class reference is directed to a child class object
1, the Kawai class overrides a method, the parent class references a new method that calls the subclass to redefine
2. The Kawai class does not overwrite a method, the parent class references the old method that calls the parent class itself
3. The Kawai class overrides a property, but the parent class reference still calls the old property of the parent class itself
4. The Kawai class does not overwrite a property, the parent class refers to the old property that invokes the parent class itself
5. The parent class reference cannot access the new defined method of the child class
When a subclass reference points to its own object
1. Kawai class overrides a method, the subclass references a new method that calls the subclass to redefine
2. The Kawai class does not overwrite a method, the child class references the old method that calls the parent class itself
3. Kawai class overrides a property, the subclass references the new attribute redefined by the calling subclass
4, the Kawai class does not overwrite a property, the subclass reference invokes the old property of the parent class itself
5. The subclass reference can access the new defined method of the child class
C. Sample code B.java
[java]View PlainCopy
- Public class B {
- int a = 1;
- int b = 2;
- void F1 () {
- System.out.println ("b.f1 ()");
- }
- void F2 () {
- System.out.println ("b.f2 ()");
- }
- }
C.java
[java]View PlainCopy
- Public class C extends B {
- int a = 3;
- @Override
- void F1 () {
- System.out.println ("c.f1 ()");
- }
- void F3 () {
- System.out.println ("c.f3 ()");
- }
- public static void main (string[] Args) {
- b b = new C (); Parent class reference to child class object
- B.F1 (); The//subclass overrides the method, so the parent class reference calls the new method
- B.f2 (); The//subclass does not overwrite the method, so the parent class reference calls the old method
- ///b.f3 (); This line removes the comment error, the parent class reference cannot access the child class new definition method
- System.out.println (b.a); The//subclass overrides the property, but the parent class reference still accesses the old property
- System.out.println (b.b); //subclass does not overwrite this property, parent class accesses old property
- System.out.println ();
- c C = new C (); The subclass reference points to its own object
- C.F1 (); The//subclass overrides the parent class method, so the new method is called
- C.f2 (); The//subclass does not overwrite the parent class method, so the old method is called
- C.F3 (); //subclass calls its own new defined method
- System.out.println (c.a); //subclass overrides this property, so access the new property
- System.out.println (c.b); //subclass does not overwrite this property, so access the old property
- }
- }
Output:
[html]View PlainCopy
- C.F1 ()
- B.f2 ()
- 1
- 2
- C.F1 ()
- B.f2 ()
- C.F3 ()
- 3
- 2
Inheritance in java: relationships between parent and child classes