The difference between inheritance and polymorphism

Source: Internet
Author: User

1, what is inheritance, the characteristics of inheritance.

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

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

In the inheritance relationship. The parent class and subclass need to satisfy the is-a relationship. Subclasses are parent classes.

Terms that represent the parent class and subclasses: Parent and subclass, superclass and subclass, base class, and derived class, they represent the same meaning.

2, why need to inherit. When should you inherit.

Using inheritance can effectively implement code reuse and avoid the occurrence of duplicate code.

When two classes have the same characteristics (properties) and behavior (methods), the same part can be extracted and placed in a class as the parent class, and the other two classes inherit the parent class.

Inheritance implements the object-oriented principle: Write once,only once (write once, and write once

3, how to achieve inheritance.

In the Java language, the extends (extended) keyword is used to indicate that a class 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 redefine the properties of the parent class, and the methods that override the parent class can get different functionality from the parent class.

4, what is the method rewrite.

If a method defined in a subclass whose name, return type, and argument list matches exactly the name, return type, and argument list of a method in the parent class, it can be said that the subclass's method overrides the parent class's method.

Method rewriting in different classes is a necessary condition for the realization of polymorphism.

5, the use and location of the Super keyword, super keyword calls the parent class's construction method, the Super keyword invokes the parent class method.

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

If the method of the parent class is overridden in a subclass, the method of the parent class can be invoked 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+");

}

Sub class:

Public xinxin2 (String name,string Sex)

{

To invoke the constructor method of the parent class

Super (Name,sex);

}

public void Hello () {

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

Methods to invoke the parent class

Super.hello ();

}

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

Because when you create an object, you need to create the parent class object, and then create the subclass object.

Note: When you create an object, you create the parent class object, and you create the subclass object. If the constructor method that invokes the parent class is not displayed, the parameterless constructor method of the parent class is automatically invoked.

6, all classes of the eldest (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, then the class inherits directly from the object class.

7, what is polymorphic.

Polymorphism is characterized by many forms, and has many ways of implementation. Or polymorphism is characterized by the ability to perform multiple forms. Or the same implementation interface, using different instances to perform different operations.

8, why the need to use polymorphism. The benefits of polymorphism.

can enhance the scalability and maintainability of the program, making the code more concise.

Not only can reduce the workload of coding, but also greatly improve the maintainability and scalability of the program.

9, how to achieve polymorphism.

As a general practice, write a method that only receives the parent class as a parameter, and writes code that only deals with the parent class. When this method is invoked, a different subclass object (new object) is instantiated.

More specifically:

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

(2), the parent class type as the parameter type, the parent class and its subclass object are transferred as parameters.

(3), at run time, the method is determined dynamically based on the type of object that is actually created.

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 is closely related to inheritance and method rewriting, we receive the parent class type as a parameter in the method, and the various methods of the parent class type are called 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 based on the type of object actually created (when a method override exists).

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.