Abstract classes and Interfaces

Source: Internet
Author: User

Consider why these two technologies are needed first from the design requirements.

Abstract class, first class. A class is a definition of an object that indicates that an object has specific properties and behaviors, such as a dog barking and a dog barking as a specific behavior. Abstract classes, like their names, represent abstractions of objects, and ordinary classes are used to represent specific objects, such as dogs and cats, but the way they are called is different.        Because it is not possible to represent a specific object, it makes no sense to instantiate it. Abstract class Usage principles:
    • Abstract classes must not be instantiated (design requirements and syntax)
    • Abstract classes are bound to be inherited (design requirements)
    • Abstract classes generally contain abstract methods (design requirements)
    • Subclasses are still abstract classes (design requirements and syntax) if they do not have all the abstract methods of the Override abstract class
    • Abstract classes are modified with an abstract and cannot be present at the same time as final (design requirement syntax)
    • Abstract methods are not available with the private static final native (design requirement syntax) with abstract modification
It can be seen that the limitations of grammar are also designed to ensure the implementation of design requirements. Examples of use of abstract classes:
Abstract class Animal () {    abstractvoid  Cry ();} class extends Animal () {    @Override    void  Cry () {    }}classextends  Animal () {    @Override    void  Cry () {    }}

interface, which is used to represent the collection of behaviors. In a specific application, if a class inherits an abstract class, it means that the class is a special case of the abstract class, whereas a class implements an interface, which means that the class has the behavior defined in the interface.one is "yes" and one is "yes", which is why inheritance in Java is single-inheritance, and implementation can be multi-implementation.         For example, the door, some have a doorbell, some do not have a doorbell, this is some relationship, according to the doorbell this operation, the use of interface is more appropriate. Interface usage Rules:
    • The properties in the interface are all modified by public static final, so there is no need to display the write
    • The methods in the interface are all modified by public, so there is no need to display write
    • For a class to implement an interface, it is necessary to implement all the methods in this interface
    • A class can implement multiple interfaces at the same time
Examples of simultaneous use of interfaces and abstract classes:
Abstract classDoor () {Abstract voidopen ();}InterfaceAlarm () {voidalarm ();}classNormaldoorextendsDoor () {@Overridevoidopen () {}}classAlarmdoorextendsDoorImplementsAlarm () {@Overridevoidopen () {} @Overridevoidalarm () {}}



From for notes (Wiz)



Abstract classes and 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.