Java object-oriented polymorphism and interfaces

Source: Internet
Author: User

One, polymorphic

1. By polymorphism, you can reduce the amount of code in a class to improve the scalability of your code. Inheritance is the basis of polymorphism, and there is no polymorphism without inheritance.

2. Conversion of the handle class to the parent class is called upward transformation, and the type conversion is automatic. Converting a parent class to a subclass is called a downward transformation and must be cast in a forced type.

3. A method called by a parent class reference variable after an upward transformation is a subclass that overrides or inherits a method of the parent class, and the parent class references a variable that cannot invoke a method that is unique to the subclass.

4. The sub-class-specific methods can be accessed after a downward transition. You must convert to the real subclass type that the parent class points to, or the type conversion exception classcasexception will occur.

The 5.INSTANCEFO operator is used to determine whether an object belongs to a class or implements an interface.

The 6.INSTANCEFO operator is used in conjunction with the coercion type conversion, first by instanceof to type judgment, and then by the corresponding coercion type conversion.

7. Using a parent class as a method parameter and using the parent class as the return value type is a common way to use polymorphism.

Example:

Subclass go to Parent class (convert up):

Pet pet=new Dog ("Europe and Europe", "Shelly Rui");

Pet.eat ();

(parent class goes to subclass) down translation:

Pet pet=new Pet ("Europe and Europe", "Shirley Rui");

Pet.eat ();

Dog dog= (dog) pet; Force type Convert Dog object

Second, the interface

1. The properties in the interface are all global static constant interface methods are global abstract methods, there is no construction method in the interface.

2. A class can inherit only one parent class, but multiple interfaces may be implemented. If a class implements an interface, it must implement all the methods in the interface, otherwise it must be an abstract class. Java achieves the effect of multiple inheritance by implementing an interface.

3. An interface represents a convention, and an interface represents a capability. The interface embodies the principle of convention and realization to separation.

4. Abstract class facilitates code reuse, and interfaces facilitate code maintenance.

5. Interface-oriented programming can reduce the coupling between the code, improve the scalability and maintainability of the code.

6. Interface-oriented programming means: When the system is developed, the main frame uses the interface, the interface forms the skeleton of the system, so that the replacement system can be realized by replacing the class that implements the interface.

The members of an interface in 7.c# are primarily methods, properties. The interface cannot contain constants, variables, construction methods, and any static members. It is illegal to display a specified modifier when defining an interface.

Java object-oriented polymorphism and interfaces

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.