Java inheritance and polymorphism, Java inheritance Polymorphism

Source: Internet
Author: User

Java inheritance and polymorphism, Java inheritance Polymorphism

 

1. What are the characteristics of inheritance?

Subclass inherits the features and actions of the parent class, so that the child class has various attributes and methods of the parent class. Or subclass inherits the method from the parent class, so that the child class has the same behavior as the parent class.

Feature: In the inheritance relationship, the parent class is more general and the Child class is more specific. The parent class has more general features and behaviors, while the Child class not only has the features and behaviors of the parent class, but also has some special characteristics and behaviors.

In the inheritance relationship. The parent class and subclass must satisfy the is-a relationship. Subclass is the parent class.

Terms used to represent Parent and Child classes: Parent and Child classes, superclasses and child classes, base classes, and derived classes. They represent the same meaning.

2. Why inheritance? When should I inherit?

Inheritance can effectively reuse code to avoid repeated code.

When two classes have the same features (attributes) and behavior (methods), the same parts can be extracted and put into a class as the parent class. The other two classes inherit the parent class.

Inherits the object-oriented principle: write once, only once (write once, and write once)

3. How to Implement inheritance?

In java, the extends keyword is used to indicate that one class inherits another class.

Only some common attributes and methods are defined in the parent class.

The subclass automatically inherits the attributes and methods of the parent class. The subclass can define specific attributes and methods. Or the subclass can redefine the attributes of the parent class, and override the parent class method to obtain different functions from the parent class.

4. What is method rewriting?

If the name, return type, and parameter list of a method defined in the subclass exactly match the name, return type, and parameter list of a method in the parent class, you can say, the subclass method overrides the parent class method.

Method rewriting in different classes is a necessary condition for implementing polymorphism.

5. The usage and position of the super keyword. The super keyword calls the constructor of the parent class, and the super keyword calls the method of the parent class?

In the constructor of the subclass, the constructor of the parent class is called using the super keyword.

If the child class overrides the method of the parent class, you can use the super keyword to call the method of the parent class.

Parent class:

Private String name;

Private String sex;

Public xinxin1 (String name, String sex)

{

This. name = name;

This. sex = sex;

}

Public void hello (){

System. out. println ("Hi! I am "+ name +" I am "+ sex +" child ");

}

Subclass:

Public xinxin2 (String name, String sex)

{

// Call the constructor of the parent class

Super (name, sex );

}

Public void hello (){

System. out. println ("I'm new !");

// Call the method of the parent class

Super. hello ();

}

Note: The super statement that calls the constructor of the parent class must be the first statement in the constructor.

When creating an object, you need to create a parent class object before creating a subclass object.

Note: When creating an object, first create a parent class object and then create a subclass object. If the constructor of the parent class is not displayed, the non-argument constructor of the parent class is automatically called.

6. The boss (ancestor) Object of all classes.

All classes inherit the java. lang. Object Class directly or indirectly. The Object class defines the same behavior of all java objects and is the ancestor of all classes.

If a class does not use the extends keyword, the class directly inherits from the Object class.

7. What is polymorphism?

Polymorphism features multiple forms and implementation methods. Or polymorphism is characteristic of the ability to present multiple forms. Or use different instances to perform different operations on the same implementation interface.

8. Why do I need to use polymorphism? What are the benefits of polymorphism?

It can enhance the scalability and maintainability of the program and make the code more concise.

This not only reduces the coding workload, but also greatly improves the maintainability and scalability of the program.

9. How to Implement polymorphism?

The general practice is to write a method, which only receives the parent class as the parameter, and write the code to only deal with the parent class. When this method is called, different subclass objects (new object) are instantiated ).

More specifically:

(1) subclass override the parent class method. Sub-classes have different implementation methods.

(2) The parent class type is used as the parameter type, and the parent class and its subclass objects are transferred as parameters.

(3) During running, the method is dynamically determined based on the type of the actually created object.

At runtime, the Java Virtual Machine determines which method to use based on the type of the object actually created. This is generally called dynamic binding.

10. Summary of polymorphism: polymorphism is closely related to inheritance and method rewriting. we receive the parent class type as a parameter in the method and call various methods of the parent class type in the method implementation. When a subclass is passed as a parameter to this method, the Java Virtual Machine will call the corresponding method in the subclass based on the actual object type created (when there is a method rewrite ).

 

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.