Thinking of the method of overriding the parent interface in Java sub-interface method

Source: Internet
Author: User

Interface list in the inherited interface collection, interface collection there is a size () method, in the interface list and the size () method, what is the point? (cover a lot of ways, see)

Public interface List<e> extends collection<e>

1. Both the parent interface and the sub-interface are abstract (not implemented), and the subclass does not change the method of the parent class, which does not make sense at the functional level.
2. The subclass method cannot narrow the access rights of the overridden methods, but the methods inside the interface can only be public, so the child interface method overrides the parent interface method and cannot change the scope of the method.
The subclass method cannot throw more exceptions than the overridden method, so the sub-interface method overrides the parent interface method cannot change the method that can change the exception thrown by the method.

3. Sub-interface methods overriding the parent interface method is more about the readability of the class, reminding the consumer that this method is available here.

InterfaceA {voidMethod ()throwsException;}InterfaceBextendsA {voidMethod ();//there is no exception thrown here} Public classCImplementsB { Public Static voidMain (string[] args) {A a=NewC (); b b=NewC (); //catching exceptions for Class A method methods        Try{A.method (); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace ();    } b.method (); } @Override Public voidmethod () {System.out.println ("Method"); }}

Thinking of the method of overriding the parent interface in Java sub-interface method

Related Article

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.