Inheritance and polymorphism in Java

Source: Internet
Author: User

1. What are the characteristics of inheritance and inheritance?

Subclasses inherit the characteristics and behavior of the parent class, making the subclasses have various properties and methods of the parent class. Or subclass inherits a method from the parent class so that the child class has the same behavior as the parent class.

Features: In an inheritance relationship, the parent class is more generic and the subclass more specific. The parent class has more general characteristics and behaviors, and subclasses have some special characteristics and behaviors in addition to the characteristics and behaviors of the parent class.

In an inheritance relationship. The parent class and subclass need to satisfy the is-a relationship. The child class is the parent class.

Represents the terms of the parent and child classes: Parent and subclass, superclass and subclass, base class, and derived class, they represent the same meaning.

2. Why do I need to inherit? When should I inherit?

Using inheritance can effectively implement code reuse and avoid duplicate code occurrences.

When two classes have the same characteristics (attributes) and behaviors (methods), the same parts can be extracted and placed into a class as the parent class, and the other two classes inherit the parent class.

Inheritance implements an object-oriented principle: Write Once,only once (written once, and written once

3, how to achieve inheritance?

In the Java language, the extends (extended) keyword is used to represent a class that inherits another class.

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

Subclasses automatically inherit the properties and methods of the parent class, and the subclasses can define specific properties and methods. or subclasses to redefine the properties of the parent class, overriding the parent class's methods can get different functionality from the parent class.

4. What is method rewriting?

If a method is defined in a subclass whose name, return type, and argument list match exactly the name, return type, and argument list of a method in the parent class, the subclass's method overrides the parent class's method.

Methods override in different classes and are necessary for implementing polymorphism.

5. The use and location of the Super keyword, the Super keyword calls the constructor of the parent class, the Super keyword calls the method of the parent class?

In the constructor method of the subclass, the constructor method of the parent class is called with the Super keyword.

If a method of the parent class is overridden in a subclass, the parent class's method can be called through the Super keyword.

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 ");

}

Sub-class:

Public xinxin2 (String name,string Sex)

{

Calling the parent class's constructor method

Super (Name,sex);

}

public void Hello () {

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

Calling a method of the parent class

Super.hello ();

}

Location NOTE: The statement that calls the constructor method of the parent class (Super statement) must be the first statement in the constructor method.

Because when creating an object, you need to create the parent class object before creating the subclass object.

Note: When you create an object, the parent class object is created before the child class object is created. If the constructor method that calls the parent class is not displayed, the non-parametric construction method of the parent class is automatically called.

6, all kinds of Boss (ancestor) Object.

All classes inherit the Java.lang.Object class directly or indirectly, and the object class defines the same behavior that all Java objects have, and is the ancestor of all classes.

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

7, what is polymorphic?

Polymorphism is characterized by a variety of forms, with a variety of implementation methods. Or polymorphism is a feature that has the ability to express multiple forms. Or the same implementation interface, different instances are used to perform different operations.

about the Up and down transformation in the Java language

1. Transition up:---to the parent class

2. Transition down: Parent-to-child

Note: whether it's an upward transformation or is downward transformation, there must be an inheritance relationship between the two classes

benefits of using polymorphism:

1. Use polymorphism to reduce the coupling between code

2. Enhancement of Project expansion capability

the conditions for polymorphism are:

1. There are inheritance or implementation

2. A method of coverage or implementation

3. Parent class object (interface) to child class object

8, why need to use polymorphic? What are the benefits of polymorphism?

You can enhance the scalability and maintainability of your program to make your code more concise.

It can not only reduce the workload of coding, but also greatly improve the maintainability and extensibility of the program.

9, how to achieve polymorphism?

The general practice is to write a method that only receives the parent class as an argument, and the code written only deals with the parent class. When this method is called, a different subclass object (new object) is instantiated.

To say more specifically:

(1), subclasses override methods of the parent class. Enables subclasses to have different method implementations.

(2), the parent class type as the parameter type, the parent class and its child class object as parameters.

(3), runtime, according to the actual type of object created dynamically decide to use that method.

At run time, the Java virtual Opportunity determines which method to use based on the type of object that is actually created. This is generally referred to as dynamic binding.

10, polymorphic Summary: Polymorphism and inheritance, method rewriting is closely related, we receive the parent class type as a parameter in the method, the various methods of calling the parent class type in the method implementation. When the handle class is passed as a parameter to this method, the Java virtual opportunity invokes the corresponding method in the subclass (when there is a method override) based on the object type actually created.

Inheritance and polymorphism in Java

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.