Java's tracing style

Source: Internet
Author: User
Tags expression inheritance

The most noteworthy part of inheritance is that it does not provide a method for the new class. Inheritance is an expression of the relationship between a new class and the underlying class. You can summarize the relationship by saying that the new class belongs to a type of an existing class.
This expression is not only a kind of visualization interpretation of inheritance, which is directly supported by language. As an example, consider a basic class called instrument, which is used to represent musical instruments, and another derived class called Wind. Since inheritance means that all the methods of the underlying class can also be used in derived classes, any message we send to the underlying class can also be sent to the derived class. If the instrument class has a play () method, then wind equipment will also have this method. This means that we can be sure that a wind object is also a type of instrument. The following example shows how the compiler provides support for this concept:

: Wind.java
//Inheritance & upcasting
import java.util.*;

Class Instrument {public
  void Play () {}
  static void tune (instrument i) {
    //...
    I.play ();
  }

Wind objects are instruments
//because they have the same interface:
class Wind extends instrument {
  PU Blic static void Main (string[] args) {wind
    flute = new Wind ();
    Instrument.tune (flute); Upcasting
  }
}///:~

The most interesting of this example is undoubtedly the tune () method, which accepts a instrument handle. In Wind.main (), however, the tune () method is invoked by assigning a wind handle to it. Because Java is particularly strict with type checking, you may find it strange why receiving one type of method can also receive another type. However, we must realize that a wind object is also a instrument object. and for a instrument (musical instrument) that is not in wind, no method can be invoked by tune (). In tune (), the Code applies to instrument and anything derived from instrument. Here, the behavior of converting from a wind handle to a instrument handle is called "Tracing the shape".

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.