Java inheritance, knowledge points of Advanced Concepts of classes, and java knowledge points

Source: Internet
Author: User
Tags format definition

Java inheritance, knowledge points of Advanced Concepts of classes, and java knowledge points

1.Inheritance

In object-oriented programming, you can create a new class by extending an existing class and inheriting its attributes and behaviors. This method is called inheritance ).

2.Benefits

A. code reusability

B. Subclass can extend attributes and methods of the parent class.

C. attributes and methods of the parent class can be used for subclass

D. It is easier to design applications.

3.How can I determine whether a class has an inheritance relationship?

Class-class relationship: a. has-a (composite relationship)

B. is-a (inheritance relationship)

4.Java(Keyword: extends)

 The keyword extends appears after the class name when the class is declared. extends is followed by the name of the class to be inherited. Example: public class Salary extends Emp {

}

5.Inheritance has a single nature and transmission

6.Root class Object of all classes (base class, superclass)

Public class Emp extends Object {

}

Method signature in the Object class:

A. public final Class getClass ()

B. public int hashCode () This method returns the object's hash code.

C. public boolean equals (Object x) This method can detect whether two objects are equal.

D. protected Object clone () throws CloneNotSupportedException

E. public String toString () This method returns the String representation of the object.

F. protected void finalize () throws Throwable: This method is called when the object is to be reclaimed.

G. public final void wait () throws InterruptedException: Thread

H. public final void y (): Thread

7.Method rewriting (method rewriting appears in the subclass. The subclass overrides the method of the parent class, And the constructor cannot be inherited)

A. Meaning of method Rewriting: A subclass can override methods inherited from the parent class to allow the subclass to add or change the behavior of methods in the parent class.

B. The sub-class overrides the rules to be followed by the parent class:

A. the method name, return value type, and parameter list of the subclass method must be the same as that in the parent class.

B. The access modifier of the subclass must not be smaller than the access modifier in the parent class. (The access modifier in the subclass cannot be stricter than that in the parent class)

C. Exceptions repeated in child classes cannot throw more exceptions than the parent class.

8.SuperKeywords

A. The subclass uses the keyword "super" to call the method of rewriting in the parent class ("super" refers to the parent class object)

B. Use super to call the constructor of the parent class. If the constructor does not use the super keyword, the compiler automatically adds a super () without parameters to call the parent class constructor.

9.FinalKeywords

A. The final variable cannot be changed.

B. The final class cannot be inherited.

C. The final method cannot be rewritten.

10. Access Modifier 

 

This class

Different classes in the same package

Different packages

Subclass

Public

Y

Y

Y

Y

Protected

Y

Y

 

Y

Default (do not write)

Y

Y

 

 

Private

Y

 

 

 

 

11.Encapsulation

A. Meaning: encapsulation is to make all member variables in the class private and provide public methods to access these member variables.

B. encapsulate getter, setter,

Example: public int getId (){

}

Public void setId (int id ){

This. id = id;

}

C. Advantages of encapsulation:

A. The member variables of the class can be read-only or write-only.

B. The class can have an overall control over the content stored in other member variables.

Class c users do not need to know how classes store data.

12.Static members: static modified members can be shared. To access static members, useClass Name .. Static methods cannot access non-static members.

13.Static initialization block:

Example: public class Rad {

Static {

// The static code block appears before the constructor

}

}

14.Non-static (member code block) (instantiate the initial block ):

Example: public class Rad {

{

// Instantiate the initial block

}

}

Instantiate the statements in the initial block after any parent class constructor calls them and runs them before the subclass constructor calls them.

15.Internal class: the class defined inside the class. The class of the internal class is called an external class.

A. Features of internal classes:

B. static internal class:

Definition method: class Outer {

Static class Inner {

// The static internal class cannot access external members, unless the external member is also a static member

}

}

Static internal class creation object: a. use external class. B. Import the package where the internal class is located. c. Use a fully qualified name.

C. member Internal classes:

Definition method: class Outer {

Class Inner {

// The member's internal class can access external members

}

}

Member internal class creation object: Outer outer = new Outer ();

Outer. Inner inner = outer. new Inner ();

D. Local internal class: it can only be used inside the Method

Definition method: class Outer {

Void fd (){

Class Inner {

// Local internal class

}

}

New Inner (). fd ();

}

E. Anonymous internal class: No Name

Format definition: new Class or Interface Name (){

// Anonymous internal class subject

}

 

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.