Java Fundamentals Hardening 11: Two basic problems of polymorphism

Source: Internet
Author: User

1. Question 1th

1 classBase {2      Public voidmethod () {3System.out.print ("Base method"));4     }5 }6 7 classChildextendsBase {8      Public voidMethodB () {9System.out.print ("Child MethodB");Ten     } One } A  - classSample { -      Public Static voidMain (string[] args) { theBase base =NewChild (); - Base.methodb (); -     } -}

A. Base method

B. Child MethodB

C. Base method Child MethodB

D. Compilation errors

2. Question 2nd

1 classPerson {2     intA;3      Public intChangeintm) {4         returnm;5     }6 }7 8  Public classTeacherextendsPerson {9      Public intb;Ten      Public Static voidMain (string[] args) { OnePerson p =NewPerson (); ATeacher T =NewTeacher (); -         inti; -______//fill in the code here the     } -}

A.i=b;

B.i=a;

C.i=p.change (30);

d.i=t.b;

3.

Attention:

(1) For member variables: compile look left , run look left ;

(2) For member method: compile look left , run look right ;

(3) Question 1th: The parent class refers to a subclass that can only invoke a method inherited from a parent class or a subclass to override a method inherited from a parent class;

Question 2nd: Static methods cannot invoke non-static members, static members belong to classes, main belongs to subclasses, it is also methods, but static methods of static, only static variables can be called. Therefore,int A and int b

are not directly called directly in main .

Java Fundamentals Hardening 11: Two basic problems of polymorphism

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.