Java Core Technical notes for busy people (4, inheritance and reflection)

Source: Internet
Author: User

Inheritance is the process of creating a new class based on an existing class. (the instance variable and the static variable are collectively referred to as the domain, the domain, method, nested class, interface in the class are collectively referred to as class members)

Reflection mechanism: The ability to find classes and their members while the program is running

The abstract method is not implemented, and the abstract class cannot be instantiated.

Subclasses cannot directly access private instance variables of the parent class.

Unlike this reference, super is not a reference to an object, but rather bypasses the dynamic lookup method and invokes instructions for a particular method.

When overriding a method, you can change the return type to a subtype (the covariant return type is allowed)

When you overload a method, the child class method is at least as visible as the parent class method. The parent class method is public, and the subclass method must also be declared as public.

1, sub-class construction

Because the child class's constructors cannot access the private variables of the parent class, they must be initialized by the constructor of the parent class.

1  Public Manager (String name,double  salary) {2     Super(name, salary); 3     Bonus = 0; 4 }

2, the parent class assigns the value

Assigning a child class object to a parent class variable is legal

1 New Manager (); 2 Employee Empl = boss;

  ※ When the method is called on the parent class variable. A method of a subtype is called, even if the type is a parent type. When the method is called, the virtual opportunity looks at the object's actual type and locates the version of the method. This process is called dynamic method lookup .

  With dynamic method lookups, you can write the actions that the parent class consents to, returning different results (the same code, returning different results) depending on the subclass.

In Java, the parent class assignment applies equally to arrays. The Java array is covariant.

1 New Manager[10]; 2 employee[] Empls = bosses;    // Legal. 3 New Employee ()    // compile pass, run-time error, parent class cannot be placed in the actual subclass array, arraystoreexception

The child class is placed in the parent class object, and only methods of the parent class can be called.

 new   Manager ()  2  Empl.setbonus (100); //  compile error, no Setbonus method in parent class  3  //   You can convert a parent class reference to a subclass reference  5  if  (empl  Manager) { Span style= "color: #008080;" >6  Manager Mgr = (manager) Empl;  7  Mgr.setbonus (100 8 } 

When a method is declared final, the subclass cannot overwrite it. Final does not improve efficiency, modern virtual opportunities are inferred for automatic inline (http://blog.csdn.net/zq602316498/article/details/40266633?utm_source=tuicool &utm_medium=referral)

3. Abstract methods and classes

A class can define methods that are not implemented, forcing subclasses to implement methods, and the classes to which they belong are called abstract methods and classes. Must be modified with an abstract.

Abstract classes can have non-abstract methods.

  ※ Unlike interfaces, abstract classes can have instance variables and constructors. An interface can define constants, public static final int MAX = 10;

  It is not possible to construct an instance of an abstract class. However, you can have a variable of type abstract class, provided that the variable refers to an object of a specific subclass .

4. Protected type

Classes under the same package can be accessed, and sub-classes under different packages can be accessed.

  

Java Core Technical notes for busy people (4, inheritance and reflection)

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.