JAVA basics-inheritance, javainheritance

Source: Internet
Author: User
Tags export class

JAVA basics-inheritance, javainheritance

1. Inheritance

1. class: the basic unit in programming languages. Encapsulate data and functions together. Classes are used to model objects of the same type.

2. The parent class contains the data domains and Methods shared by all its sub-classes (subclass. That is, the subclass inherits the data domains and methods of the parent class. You can also add new data domains and methods to subclass.

3. You can create a parent class to represent the core concepts of some objects in the system, and export other types from the parent class to indicate different methods that the core can be implemented.

4. The private member subclass of the parent class is not accessible. Private data domains can be accessed and modified through the common accessors (getName ()/modifier (setName () of the parent class.

5. The subclass copies the interface of the parent class, so all messages that can be sent to the parent class object can also be sent to the subclass object. The subclass has the same type as the parent class.

6. The two methods can make the parent class and subclass different:

1) Add a new method directly to the subclass;

2) change the behavior of the existing parent class method-overiding the method of the base class.

7. is-a and is-like-:

1) is-a: the Child class has the same interface as the parent class, that is, the Child class and the parent class are of the same type. For example, a circle is a ry. To determine whether the class is inherited, determine whether it can use is-a to describe the relationship between classes and make it practical.

2) is-like-a: a new interface element is added to the subclass, that is, the interface is extended, and the parent class cannot access the newly added method. The export class is like a base class.

8. Java does not allow multiple inheritance. A java class can only be directly inherited from a parent class-single inheritance ). Multi-inheritance can be implemented through interfaces.

Ii. Role of super

1. Call the constructor of the parent class

Super ()/super (parameters) must appear in the first row of the subclass constructor. This is the only way to explicitly call the parent class constructor. A syntax error occurs when the name of the parent class constructor is called in the subclass. Constructor can be used to construct an instance of a class. Unlike attributes and methods, the constructor of the parent class is not inherited by the quilt class. They can only be called by super in the constructor of the subclass.

Note: constructor chaining: In any case, when constructing an instance of a class, the constructor will call the constructor of all the parent classes along the inheritance chain. When constructing a subclass object, the subclass constructor first calls its parent class constructor before completing its own tasks. If the parent class inherits from other classes, the parent class constructor calls its parent class constructor before completing its own tasks. This process continues until the last constructor of this inheritance system is called.

Public class Apple extends Fruit {}

Class Fruit {

Public Fruit (String name ){

........

}

}

Apple does not have an explicitly defined constructor. Therefore, Apple's default constructor without parameters is called implicitly. Apple is a subclass of Fruit, so Apple's default constructor automatically calls the non-parameter constructor of Fruit. Fruit does not have a construction method without parameters, because Fruit explicitly defines the construction method. Therefore, the program cannot be compiled. It is best to provide a no-argument Constructor (if needed) for each class, so as to expand the class and avoid errors.

2. Call the method of the parent class

Super. Method Name (parameter)

(If the parent class method is overwritten, super is required to call the parent class method. If the parent class method is not overwritten, super is not required because the parent class method has been inherited by the quilt class)

Method overriding)

1. Subclass inherits the method from the parent class. Sometimes, the subclass needs to modify the implementation of the method defined in the parent class, which is called method override.

2. super. super... method name () is a syntax error. Only super. Method Name () is allowed ()

3. Only when the instance method is accessible can it be overwritten. The private method of the parent class cannot be overwritten. If the method defined in the subclass has the same name as the private method in the parent class, the two methods do not matter.

4. Static methods can be inherited but cannot be overwritten. If the static method in the parent class is overwritten by the quilt class, the static method defined in the parent class will be hidden. The hidden static method can be called by the parent class name. static method.

Iv. Object-like objects

1. Every class in Java comes from the java. lang. Object Class. It is important to be familiar with the methods provided by the Object class. In this way, you can use them in your own class.

2. toString () method: return the class name @ the hex memory address of the object

3. equals () method: Use = to check whether two referenced variables point to the same object. The format of the override equals method is fixed to equals (Object obj)

5. polymorphism)

1. Subclass objects can be used wherever parent class objects are used, which is polymorphism. Polymorphism means that variables of the parent type can reference objects of the Child type.

6. dynamic binding

1. dynamic binding mechanism: Suppose the object o is an instance of Class C1, C2,..., Cn-1, Cn. Where C1 is a subclass of C2,... the Cn-1 is a subclass of Cn. If o calls a Method p, the Java Virtual Machine will find the implementation of Method p in Class C1, c2... Cn in sequence until it is found. Once an implementation is found, stop searching and call the implementation found for the first time.

VII. final

The final modifier indicates that a class is the ultimate and cannot be the parent class. The final modification method cannot be overwritten by the quilt class. The variable in the final-modified method is a constant.

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.