Java Foundation Fifth Day _ Static code block, class inheritance and interface

Source: Internet
Author: User
Tags modifiers

1. Text describes the role of static code blocks.


A static block of code executes when the class is loaded and executes only once.

2. What is the order in which the code blocks and constructors are called? is the declaration position of the method relevant? Why? What is the purpose of building blocks of code?


Constructing code block invocation order takes precedence over the construction method, regardless of the declaration position of the method.

Building blocks of code: A. Common code that can hold multiple construction methods to increase the repeatability of the Code

B. Initialize the member variable

3. Will the static code block be executed when the class loads? How do I manually control class loading?


Static code blocks are executed by default when the class loads.

Manual control class loading uses Class.forName ("ClassName"), which executes the code block when the class is loaded by default.

4. Define the class Person,man,woman to form an inheritance relationship.


Class Persion {

}

Class Man extends persion{

}

Class Woman Extendspersion {

}

5. What is an abstract method? What is an abstract class? Do abstract classes have constructors and can they be overloaded?


Abstract method: A method that has no method body. You must use the abstract adornment.

Abstract classes: Classes that contain abstract methods, that is, abstract classes, and abstract classes must also use abstract adornments.

Abstract classes have constructors and can be overridden

6. What are the illegal combinations of abstract modifiers? and give a reasonable explanation?


Abstract + static is an illegal modifier combination. Static modifiers make it easier to access public properties, abstract direct access without any meaning

Abstract + final is an illegal modifier combination. Final retouching is the ultimate, cannot change, abstract need to instantiate, need to manipulate

Abstract +private is an illegal modifier combination. Abstract classes require subclass inheritance to instantiate, and private cannot inherit

7.super and this role as well as usage and precautions?


Super: Super class, similar in function to this.

Super (...): accesses the constructor of the superclass. can only be used on the first line. The default is super ();

This: a reference to the object itself.

This (...) Represents the constructor that invokes the current class. Can only be used in the first row. Default this ();

8. Defining the interface

(1) Iwhite

(2) Irich

(3) Ibeanty

(4) Define the class and implement the above three interfaces. (Womenstar)

(5) Define the Local tyrants class (Earchricher.marring (...))

Interface Iwhite {

public void Skinwhite ();

}

Interface Irich {

public void Walletrich ();    

}

Interface Ibeauty {

public void Facebeauty ();

}


Interface Whiterichbeautyextends Iwhite, Irich, ibeauty {

}

Class RichMan {

Public Voidmarry (whiterichbeauty woman) {

System.out.println ("findlove!");

}

}

Class Richmandemo {

public static Voidmain (string[] args) {

Whiterichbeauty womanstar= New Whiterichbeauty () {

public void Skinwhite () {

}

public void Walletrich () {

}

public void Facebeauty () {

}

};

RichMan man = Newrichman ();

Man.marry (Womanstar);

}

}


Java Foundation Fifth Day _ Static code block, class inheritance and interface

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.