Learn more about abstract classes and interfaces

Source: Internet
Author: User
Tags modifier
Abstract class 1.abstract class is the golden mean between generic classes and Interfaces 2. If a class contains one or more abstract methods, the class must be declared abstract, or the compiler will error 3. You cannot create an object of an abstract class, even though there is no abstract method 4 in this abstract class. If you want to create an object of this new class if you are inheriting a subclass from an abstract class, then the new subclass implements all the abstract methods in that class. Otherwise, the subclass must also be an abstract class 5 decorated by the abstract keyword. An abstract class can also contain no abstract methods; it is useful to have a class but you do not need to have an abstract method, and you want to prevent any instances of this class from being created.
The Interface 1.interface is a more pure abstraction than the abstract concept, and is not implemented in any of the interfaces; The interface allows the creator to determine the method name, argument list, return value type, but no method definition in the method body 2.interface does not require an access modifier , because regardless of whether you add the public keyword, the definition of the method in interface is also required to be public; Whether you add an abstract keyword or not, the method definition in interface is both default and abstract, which can be useful. The modifier for a field value in 3.interface is also required to be public static final, so for example, in interface, the string Name= "Zhang" is defined, even if there is no modifier public static The final modification so that name is public and final; 4. A derived class can inherit only one class if it inherits from a class that is not an interface, and the rest of the other can inherit is an interface that can be followed by any number of interface in the implements key. These interface are separated by commas, and you can also convert the derived class up to any interface 5. If a class inherits both the extends keyword and the Implements keyword to implement the interface, first write the extends Inherit class and then write implements implementation interface, or compiler error;
6. In the actual development we want to prefer interfaces rather than abstract class, preferred class rather than interface,interface or abstract use needs to have a good reason, otherwise it is only the most useful in code refactoring 7. Inheritance is very tolerant of adding new methods to new interfaces, and it is also common to generate new interfaces with several interfaces;

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.