Course Assignments Five

Source: Internet
Author: User

1. why the constructor of a subclass must call the parent class before it is run construction method? Can you turn around? Why can't it be reversed? Because the subclass inherits the parent class, it defaults to the public member methods and public member variables that contain the parent class, and these methods and variables are not repeated in the subclass. If the parent class is not initialized when the subclass is initialized, what happens when the parent class method or variable is called by the subclass? Of course, the exception is thrown! Therefore, the Java virtual opportunity initializes the subclass's parent class by default when the subclass is initialized. And it is a layer of upward progression! 2. Please Write your own code to test the following features : In a subclass, to invoke the overridden method in the parent class, you can use the Super key word.
 Packagetest;classplus1{ Public intAddinti) {returnI+1;} }classPlus2extendsplus1{ Public intAddintj) {returnJ+10;}  Public intADD1 (intK) {return Super. Add (k);} } Public classfruit{ Public Static voidMain (String args[]) {Plus2 F=NewPlus2 (); System.out.println (F.add (1)); System.out.println (F.ADD1 (1)); }

Course Assignments Five

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.