Knowledge points for advanced concepts in Java inheritance, classes

Source: Internet
Author: User

1. meaning of inheritance

In object-oriented programming, you can create a new class by extending an existing class and inheriting the class's properties and behavior, which is called Inheritance (inheritance).

2. Advantages of inheritance

A Reusability of code

B Subclasses can extend the properties and methods of the parent class

C Properties and methods of the parent class can be used for subclasses

D Designing your application makes it easier

3. How can I tell if a class has an inheritance relationship?

Class-To-Class relationships: A. Has-a (combined relationship)

B. is-a (Inheritance relationship)

4. implementation of inheritance in Java (keyword: extends)

keyword extends after the class name that appears in the class declaration, extends followed by the name of the class to inherit. For example: public class Salary extends emp{

}

5. inheritance has a single and transitive nature

6. root class of all classes object (base class, Super Class)

public class Emp extends Object{

}

method signatures in the object class:

A Public final Class GetClass ()

B public int Hashcode () The method returns the hash code of the object.

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 () The method returns the string representation of the object.

F. protected void Finalize () throws Throwable: This method is called when an object is about to be garbage collected.

G Public final void Wait () throws Interruptedexception: Thread

H. Public final void Notify (): Thread

7. Method Overrides (method overrides appear in subclasses, subclasses override methods of the parent class, construction methods cannot be inherited)

A Method overrides meaning: Subclasses can override methods inherited from a parent class, allowing subclasses to add or change the behavior of a method in a parent class.

B The subclass overrides the rules that the parent class should follow:

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

b The access modifier for the subclass must be not less than the access modifier in the parent class. (Access adornments in subclasses are not more restrictive than in the parent class)

C Exceptions overridden in subclasses cannot throw more exceptions than the parent class.

8. Super keyword

A The subclass uses the keyword super to invoke the overridden method in the parent class (Super refers to the parent object)

B Using super to call the constructor of the parent class, if the constructor does not use the Super keyword, then the compiler will automatically add a non-parametric super () to call the parent class constructor

9. final keyword

A Final variable cannot be changed

b Final class cannot be inherited

C The final method can no longer be overridden

10. Access Modifiers

This class

Non-homogeneous in the same package

Different packages

Sub-class

public  

Y

Y

Y

protected

y

y

 

Y

default (not written)

Y

Y

 

Private

Y

One . Package

A Meaning: Encapsulation is the technique of making member variables in a class private and providing public methods to access these member variables.

B Encapsulation uses getter,setter,

For example: public int getId() {

}

public void setId(int id) {

This.id = ID;

}

C. Package Advantages:

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

B. Classes can have a holistic control over what is stored in other member variables

Users of Class C do not need to know how a class stores data

Static members: Members with static decoration, static can be shared, access to static members to use the class name. 。 Static methods cannot access non-static members.

Static initialization blocks:

For example: public class rad{

static {

Static code blocks appear before the constructor

}

}

Non-static (member code block) (instantiation of the initial block):

For example: public class rad{

{

Instantiating an initial block

}

}

The statements in the instantiated initial block are executed before the subclass constructor call, after any parent class constructor call.

Internal classes: Classes defined inside a class, the class class where the inner class resides is called an external class.

A Features of the Inner class:

    1. Stand-alone class file
    2. also need to compile
    3. can also produce objects

B Static Inner class:

Definition method: Class outer{

Static Class inner{

Static inner classes cannot access external members unless the external member is also a static member

}

}

Static inner classes Create objects: a. With an external class. B. Importing the package containing the inner Class C. with fully qualified name

C member Inner class:

Definition method: Class outer{

Class inner{

member inner class can access to external members

}

}

member Inner class creation object: Outer Outer = new Outer ();

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

D Local inner class: can only be used inside a method

Definition method: Class outer{

Void FD () {

Class inner{

Local inner class

}

}

New Inner (). FD ();

}

E. Anonymous inner class: No Name

Formal definition: The name of the new class or interface () {

The body of an anonymous inner class

}

Knowledge points for advanced concepts in Java inheritance, classes

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.