Java interface and abstract class understanding (New)

Source: Internet
Author: User

I. Characteristics of abstract classes and interfaces

A class that contains an abstract method is called an abstract class, but it does not mean that there can be only abstract methods in an abstract class, which, like normal classes, can also have member variables and ordinary member methods. Note that there are three main differences between abstract and ordinary classes:

1) The abstract method must be public or protected (because if you are private, you cannot inherit from the quilt class, the subclass cannot implement the method), and by default it is public.

2) Abstract classes cannot be used to create objects;

3) If a class inherits from an abstract class, the child class must implement the abstract method of the parent class. If the subclass does not implement an abstract method of the parent class, the subclass must also be defined as an abstract class.

  In other respects, there is no difference between an abstract class and an ordinary class.

Ii. the difference between an interface and an abstract class

 1, the grammatical level of the difference

1) Abstract classes can provide implementation details of member methods, and only public abstract methods (interfaces implicitly specified)exist in the interface;

2) member variables in abstract classes can be of various types, while member variables in an interface can only be public static final type (interfaces are implicitly specified);

3) The interface cannot contain static code blocks and static methods, while abstract classes can have static code blocks and static methods;

4) A class can inherit only one abstract class, while a class may implement multiple interfaces.

That is: All methods in an interface are abstract methods, and all member variables are static final variables.

 2. Differences at the design level

1) Abstract class is an abstraction of a thing, that is, an abstraction of a class, and an interface is an abstraction of a behavior . An abstract class is an abstraction of the whole class as a whole, including properties, behaviors, but an interface that abstracts the local (behavior) of a class. For a simple example, airplanes and birds are different kinds of things, but they all have a common denominator, that is, they can fly. Then in the design, the aircraft can be designed as a class airplane, the bird is designed as a class bird, but not the characteristics of the flight is also designed as a class, so it is only a behavioral characteristics, not a kind of abstract description of things. At this point the flight can be designed as an interface fly, including the method fly (), and then airplane and bird respectively according to their own needs to implement the fly this interface. Then as for the different types of aircraft, such as fighter jets, civil aircraft, such as direct inheritance airplane can, for birds is similar, different species of birds directly inherit the bird class can be. As can be seen from here, inheritance is a "is not" relationship, and the interface implementation is "there is no" relationship. If a class inherits an abstract class, then the subclass must be the kind of abstract class, and the implementation of the interface is there is no, with no relationship, such as whether the bird can fly (or whether it has the characteristics of flight), can fly can realize this interface, not flying will not implement this interface.

2) The design level is different, abstract class as the parent class of many subclasses, it is a kind of template design. And the interface is a kind of behavior specification, it is a kind of radiant design . What is a template design? The simplest example, we have used the template in ppt, if the template a designed ppt B and ppt c,ppt B and ppt C public part is the template A, if their public parts need to change, then only need to change the template A can be, do not need to re-ppt B and PPT c to make changes . and radiation design, such as an elevator are installed some kind of alarm, once to update the alarm, you must update all. That is, for an abstract class, if you need to add a new method, you can directly add a concrete implementation in the abstract class, the subclass can not be changed, but for the interface is not, if the interface has been changed, all implementations of this interface must be modified by the corresponding class.

Third, interface and abstract Java semantic differences, use the difference

Iv. when to use an interface, and when to use abstract classes

Java interface and abstract class understanding (New)

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.