Return type of the overload method (new feature of JDK50)

Source: Internet
Author: User
Public class AgedP {

Public Object getSSS (){
System. out. println ("return Obj ");
Return null;
}

}

Public class Kinder extends AgedP {

Public String getSSS (){
System. out. println ("return String ");
Return "";
}

Public static void main (String [] args ){
Kinder kinder = new Kinder ();
Kinder. getSSS ();
AgedP kinder2 = new Kinder ();
Kinder2.getSSS ();
AgedP agedP = new AgedP ();
AgedP. getSSS ();
}

}

This code is compiled in the JDK1.5 environment and output after running:

Return String
Return String
Return Obj

Compilation errors occur in the JDK1.4 environment.

Conclusion:

In JDK1.5, the return type of the overload method in the subclass can be the subclass of the return type of the overload method.

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.