Java Advanced features note

Source: Internet
Author: User

Inherited

Access control:

Public: Access is unrestricted.

Protected(Protection): Because of inheritance, the same package can be accessed, different packages of the current subclass of the class can be accessed, different packages are not the subclass of the class is not accessible.

Friendly(default): Access is limited to this package.

Private: Access is limited to this class.

1. What can subclasses inherit from the parent class, and what cannot inherit ?

can inherit:

(1) all public members in the parent class (including member variables and member methods)

(2) all protected members in the parent class (including member variables and member methods)

(2.1) When the child parent is in the same package, the subclass inherits the protected member, allowing the same package to be accessed by other classes, similar to the default

(2.2) When the child parent is different from the package, the subclass inherits the protected member and cannot allow the same package to be accessed by other classes, like private

(3) all default members in the parent class (including member variables and member methods), which can be inherited when the subclass and parent are similar to the package, and cannot be inherited by different packages

Cannot inherit:

(1) all private members in the parent class (including member variables and member methods)

(2) all default members in the Heterogeneous parent class (including member variables and member methods)

2. What is the relationship between a subclass and the parent class's construction method (can the constructor inherit)?

(1). When a subclass object is produced, the parent class is called without a parameter constructor (unless otherwise specified), and then the subclass constructor method is called

(2). in a subclass construction method, you can use super () to indicate that a constructor of the parent class is called,Super must be the first executable statement in the subclass construction method, and it does not mean that only one construct of the parent class can be called, because multiple constructs are writable in the subclass. Multiple times super ().

(3). subclasses cannot inherit the constructor of the parent class, and there is no overriding problem.

3. can a subclass inherit from a parent class be overwritten in a subclass?

Can be rewritten, called method overrides or method overrides.

Method Overloading: The method name is the same, the parameter list is different, and the return value type can be different.

Method overrides (Method overrides): The polymorphism between subclasses and the parent class, which is redefined by the child class for the function of the parent class. A method defined in a subclass has the same name and parameters as the parent class, called overriding (The return value type must be the same at this time, otherwise an error will be given). 1, if the method is overridden, the new method overrides the parent class method, can call the original method super. 2, the subclass of access adornment permissions can not be lower than the parent class.

polymorphic:

(1) inheritance, (2) method rewriting, and (3) when the operation is based on the actual type of object created dynamically determines which method to use.

Transition up: Parent class reference to child class object; Transform: Cast object of parent class to object of subclass

Abstract class :

1) keyword abstract definition; implementation:extends inheritance

2) Abstract class: Abstract for a class of classes , generally containing abstract methods, using the abstract keyword to modify

3) Abstract classes can only be used to inherit, not directly produce abstract class objects, there may be a construction method (when a class common)

4) After a class inherits an abstract class, the class is still abstract if it does not override the abstract method in the abstract class

5) Abstract classes can have non-abstract methods, there can be no method, abstract class without an abstract method, you do not have to rewrite

6) Meaning: separation of the design and implementation of the method (interface is also, abstract, are not unique)

Interface

1) A generic term for a set of abstract methods defined using the interface keyword. Implementation:implements keyword

2) the properties of the interface definition are constants, equivalent to public static final

3) only constants and abstract methods

4) can have multiple interfaces

Note: (1) The method in the interface is the abstract public abstraction by default , and there is no ordinary method; (2 Class access interface must be overridden or defined as an abstract class

Inner class

With good separation, internal classes can access data from external classes without restrictions, including private properties and methods. And can have been public,private,static and other modifications. When a class does not have a static modifier, the inner class is called the member class.

member Inner class

Static Inner class

Anonymous inner class

Local inner class

Role

1. internal classes can be very good to implement the hidden. the generic non-intrinsic class is not allowed to have private and protected permissions, but the inner class can

2. Inner class has access to all elements of the perimeter class

3. However, multiple inheritance is achieved

4. you can avoid modifying the interface and implementing the invocation of two methods of the same name in the same class.

Java Advanced features note

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.