Inheritance in java: relationships between parent and child classes

Source: Internet
Author: User

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 
  1. Public class B {
  2. int a = 1;
  3. int b = 2;
  4. void F1 () {
  5. System.out.println ("b.f1 ()");
  6. }
  7. void F2 () {
  8. System.out.println ("b.f2 ()");
  9. }
  10. }

C.java [java]View PlainCopy  
  1. Public class C extends B {
  2. int a = 3;
  3. @Override
  4. void F1 () {
  5. System.out.println ("c.f1 ()");
  6. }
  7. void F3 () {
  8. System.out.println ("c.f3 ()");
  9. }
  10. public static void main (string[] Args) {
  11. b b = new C (); Parent class reference to child class object
  12. B.F1 (); The//subclass overrides the method, so the parent class reference calls the new method
  13. B.f2 (); The//subclass does not overwrite the method, so the parent class reference calls the old method
  14. ///b.f3 (); This line removes the comment error, the parent class reference cannot access the child class new definition method
  15. System.out.println (b.a); The//subclass overrides the property, but the parent class reference still accesses the old property
  16. System.out.println (b.b); //subclass does not overwrite this property, parent class accesses old property
  17. System.out.println ();
  18. c C = new C (); The subclass reference points to its own object
  19. C.F1 (); The//subclass overrides the parent class method, so the new method is called
  20. C.f2 (); The//subclass does not overwrite the parent class method, so the old method is called
  21. C.F3 (); //subclass calls its own new defined method
  22. System.out.println (c.a); //subclass overrides this property, so access the new property
  23. System.out.println (c.b); //subclass does not overwrite this property, so access the old property
  24. }
  25. }

Output: [html]View PlainCopy 
    1. C.F1 ()
    2. B.f2 ()
    3. 1
    4. 2
    5. C.F1 ()
    6. B.f2 ()
    7. C.F3 ()
    8. 3
    9. 2

Inheritance in java: relationships between parent and child classes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.