It 18 Palm Job _java Foundation fifth day _ Static code block, class inheritance and interface

Source: Internet
Author: User

1. Text describes the role of static code blocks.

A: Static code blocks are commonly used to perform initialization of class properties and are executed 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?

Execution order: (Priority from high to low.) Static code block >mian method > construct code block > Construct method. and location Independent.

The construction code block executes every time the object is created.



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

A static code block is loaded by default when the class loads, Class.forName (string name) or Class.forName (string Name,boolean initialize,classloader loader), and the latter can control

Whether a static code block is run when the class loads.


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

Class Person

{

private String name;


Public String GetName ()

{

return name;

}


public void SetName (String name)

{

THIS.name = name;

}


}


Class Mans extends Person

{


}


Class Woman extends Person

{


}


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

The abstract method is an abstract-modified method that declares only the returned data type, the method name, and the required parameters, without a method body.

Classes that are decorated with abstract are abstract classes. All classes have constructors, can also be overloaded, and abstract classes are classes, so are the same.


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

Abstract + static is an illegal modifier combination, static can be accessed through the class name of public properties, abstraction of direct access, meaningless.

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

Abstract + private is an illegal modifier combination. Abstract classes require subclasses to inherit and implement, and private cannot inherit.


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

Super refers to the parent class.

This refers to the class itself.



8. Defining the interface

1.IWhite

2.IRich

3.IBeanti

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

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


Interface Iwhite

{

Abstract public void skinwhite ();

}


Interface Irich

{

Abstract public void rich ();

}


Interface Ibeanti

{

Abstract public void Beanti ();

}


Interface WRB extends Iwhite,irich,ibeanti

{

}


Class Earchricher

{

public void marring (WRB W)

{

System.out.println ("marring");

}

}


Class Interfacedemo

{

public static void Main (String [] args)

{

WRB Womenstar = new WRB () {

public void Skinwhite ()

{

System.out.println ("I ' m while");

}


public void Rich ()

{

System.out.println ("I ' m rich");

}

public void Beanti ()

{

System.out.println ("I ' m Beanti");

}

};

Earchricher RichMan = new Earchricher ();


Richman.marring (Womenstar);


}

}


It 18 Palm Job _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.