About an issue where any object in Java is cast to an interface type

Source: Internet
Author: User

Any object in Java is strongly converted to an interface type without compiling errors

 Public class Implements eatable{    publicstaticvoid  main (String args[]) {        = (drinkable) New Apple ();    }} Interface eatable{} Interface drinkable{}

Classes in Java can implement interfaces much more, and the Java compiler cannot tell if the class implements the interface so there are no compile errors.

But a class in Java is strongly moved to another class, and if the class is not its parent, the compilation error is reported because the classes in Java are single-inherited and can be judged by the compiler.

 Public class extends eatable{    publicstaticvoid  main (String args[]) {        ( drinkable) New   Apple ();  Error inconvertible Types:can not cast Apple to drinkable    }}class  eatable{}class
     drinkable{}

About an issue where any object in Java is cast to an interface type

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.