Java-polymorphism-01

Source: Internet
Author: User

Object-oriented features: data abstraction, inheritance, and polymorphism.

Data abstraction refers to abstract data type (ADT), which contains three meanings: encapsulation, information/implementation hiding, and State persistence (participate in blog: Java-object-oriented meaning ).

Polymorphism is separated by "what to do" and "how to do" (there are multiple ways to achieve "how to do", which is also known as polymorphism ), from another perspective, the interface and implementation (or base class and sub-class or sub-class and sub-class) are separated.

PS: Actually, "base classes and subclasses" can be understood as "interfaces and implementations.

"Encapsulate" to create a new data type by combining features and behaviors; "hide" to separate interfaces and implementations by private; the role of polymorphism is to eliminate the coupling relationship between types (even if the same method in different classes has no coupling relationship with each other, participate in the blog: Java-Examples of finding the polygon area using object-oriented meanings ).

Inheritance allows an object to be treated as its own type or its base type. This capability is extremely important because it allows processing of multiple types (exported from the same base class) as the same type. The same Code can run on these different types without any difference. The polymorphism method allows one type to show the differences with other similar types (that is, the differences between implementation methods for the same implementation goal, the difference is based on the behavior of the method), as long as they are all exported from the same base class.

 

Further on upward Transformation

An object can be used either as its own type or as its base class. This method regards reference to an object as reference to its base class as "upward transformation ".

/***//**
* Title: Notes to play on musical instruments <br>
* Description: This example explains the concept of "upward transformation" without any functional interfaces. <br>
* Company: Augmentum Inc <br>
* Copyright: (c) 2008 thinking in Java <br>
* @ Author forest he
* @ Version 1.0
*/
Package com. Augmentum. foresthe;
Class note ...{
Private string notename;
Private note (string notename)... {This. notename = notename ;}
Public String tostirng ()... {return notename ;}
Public static final note
Middle_c = new note ("middle_c "),
C_sharp = new note ("c_sharp "),
B _flat = new note ("B _flat ");
}
/** // * Note class is an enumeration class that contains a fixed number of optional unchanged objects. Note class cannot produce other objects, because its constructor is private */

Class instrument ...{
Public void play (note Note)... {system. Out. println ("intrument. Play ()" + note. tostirng ());}
}
Class wind extends instrument ...{
Public void play (note Note)... {system. Out. println ("Wind. Play ()" + note. tostirng ());}
}
Class Music ...{
Public static void tune (Instrument instrument)... {instrument. Play (note. middle_c );}
Public static void main (string [] ARGs )...{
Wind flute = new wind ();
Tune (flute); // upcasting
}
}

The music. Tune () method accepts an instrument reference and any class exported from the instrument. In Main (), this occurs when a wind reference is passed to the tune () method, without any type conversion (the instrument interface must exist in Wind ). Converting from wind to instrument may reduce the number of interfaces, but it will not be narrower than all interfaces of instrument.

It seems more intuitive for the tune () method to directly accept a wind reference as its own parameter. However, an important problem caused by this is: if you do this, you need to write a new tune () method (such as stringed string and brass) for each type of instruments in the system ). Therefore, we only receive base classes as parameters, rather than export classes.

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.