Distinguish between interfaces and abstract classes in Java

Source: Internet
Author: User

Abstraction is one of the major features of object-oriented design, and in the Java language is abstract classes and interfaces. The two are very similar, and it is difficult to distinguish between many beginners and even those who have been in touch with Java for a few years. Today, when I look at the related code of the collection class, I find that there are some interfaces such as List,Map , etc., some of which are abstract classes such as abstractlist , Abstractset and so on, because the two are not very clear, more confused. So today the author in this article is mainly introduced under the difference between the two.

Abstract class

Abstract methods

Using the abstract modifier is only a method that the declaration does not implement, called an abstract method. such as:

abstract void fun ();

Abstract class

classes that use the abstract modifier are called abstract classes. such as:

Abstract class classa{}

A class that has an abstract method must be an abstract class. Abstract classes can have no abstract methods. Abstract classes, like normal classes, can have member variables and ordinary member methods.

Because abstract classes contain methods that are not specifically implemented, you cannot create instance objects with abstract classes! Of course, there may be a class, only with the abstract decoration, but there is no abstraction, this is also an abstract class, is not instantiated. But why design an abstract class if there is no abstract method? So you don't have to dig into it.

Role

So it can be seen that abstract classes exist for inheritance. If an abstract class is defined but not inherited, it is of no use. Of course, after inheritance, it is necessary to implement all methods that are not implemented in the parent class, otherwise it is not allowed (except for abstract classes that are inherited by abstract classes, which can not be implemented at this time).

And the difference between ordinary classes

1. The abstract method must be public or protected(because if you are private, you cannot inherit from the quilt class, Subclasses will not be able to implement this method), which by default is public.

2. Abstract classes cannot be used to create objects;

3. The Kawai class inherits the abstract class, and the subclass must implement the abstract method of the parent class. Otherwise subclasses must also be defined as abstract classes.

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

Interface

interface is a very The type of degree abstraction, which is more abstract than the abstract class. There can be variables and methods in the interface. However, the variables in the interface are implicitly specified as public static final private error), It also means that the variable must be initialized. The methods in the interface are implicitly specified as public abstract Method (with other keywords, such as private , Span style= "FONT-FAMILY:CALIBRI;" >protected static ,  , final

And all of the methods in the interface cannot have a concrete implementation, and the methods in the interface must be abstract methods.

Also, when a non-abstract class implements an interface, all methods must be implemented. Otherwise, the class must be an abstract class, or an interface.

Abstract class and interface comparisons

The difference at the grammatical level

interfaces can only have Public Abstract method, the method type of abstract class is more, can have common method;

the member variables in the interface can only be Public static Final type, abstract class of variable type more, can have ordinary variables;

the interface cannot contain static code blocks and static methods, while abstract classes can have static code blocks and static methods;

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

The difference at design level

1) Abstract classes are abstractions of things (abstractions of classes), and interfaces are abstractions of behavior.

For example, defining Bird is an abstract class that implements it with a variety of birds. Defining an interface is fly, and various birds can implement this interface. In layman's terms: inheritance is a "yes" relationship, while an interface implementation is a "no" relationship. Birds inherit the Bird, it is doomed to its species, and whether to implement the fly interface, just think it has this feature (behavior).

2) Abstract class is a template design, interface is a code of conduct, a radiation design.

For example, the same method, through the abstract class template design, when modifying the method, only need to modify in the template, all subclasses can be updated; After the interface design, when the method is modified, it needs to be modified in all the implementation classes before it can be updated and independent of each other.

The connection between the two uses

1. if the interface inherits, it must inherit the interface. cannot be achieved. Cannot inherit abstract class

2. Abstract classes can implement interfaces, can inherit abstract classes, cannot inherit interfaces

Originally from:Codecloud

Distinguish between interfaces and abstract classes in Java

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.