What is the difference between an interface and an abstract class?

Source: Internet
Author: User
Tags define abstract

What is the difference between an interface and an abstract class?
What is the difference between an interface and an abstract class?

What is the basis for using interfaces and abstract classes?

The concepts of interfaces and abstract classes are different. The interface is the abstraction of actions, and the abstract class is the abstraction of the source.

The abstract class indicates what this object is. The interface indicates what the object can do. For example, men and women (if they are classes ......), Their abstract class is human. It means they are all people.

People can eat, dogs can also eat, you can define "eat" as an interface, and then let these classes implement it.

Therefore, in advanced languages, a class can only inherit one class (abstract class) (just as humans cannot be both biological and non-biological ), however, multiple interfaces (meal and walking interfaces) can be implemented ).

First, the interface is a variant of the abstract class, and all the methods in the interface are abstract. Abstract classes are classes that declare the existence of methods rather than implementing them.
Second, the interface can be inherited more than the abstract class.
Third, the interface definition method cannot be implemented, while the abstract class can implement some methods.
Fourth, the basic data type in the interface is static, but the image is not.

When you focus on the essence of a thing, use abstract classes; when you focus on an operation, use interfaces.

Abstract classes have far more functions than interfaces. However, it is costly to define abstract classes. Because in advanced languages (also in actual design), each class can only inherit one class. In this class, you must inherit or compile

All commonalities. Although the interface is much weaker in function, it only describes an action. In addition, you can implement multiple interfaces in a class at the same time. This will reduce the difficulty in the design phase.

Reprinted: What is the difference between interfaces and abstract classes?


What is the difference between an abstract class and an interface?

Abstract class members can have access level, while all interface members are public level
Abstract classes can contain fields, but interfaces cannot,
Abstract classes can inherit interfaces, but interfaces cannot inherit abstract classes.
Abstract class members can have specific implementations, but interfaces cannot.
The abstract subclass can selectively implement the abstract methods of its base class, while the subclass of the interface must implement

What is the difference between interfaces and abstract classes in java?

1. An abstract class is a class. Its subclass cannot inherit other classes, but it can implement one or more interfaces. An interface is not a class. Its subinterfaces can inherit multiple interfaces.
2. abstract classes can have methods without abstract modification, while interfaces can only have abstract methods, that is, methods must be abstract modified.
3. abstract classes can implement interfaces, but interfaces cannot inherit or implement abstract classes.

Some people say that an interface is similar to an abstract class, which means that only the method header is provided by the interface, and the abstract class has the same situation.
But in fact, the difference is very large. Interfaces provide a solution that is inconvenient for classes in JAVA that cannot be inherited, improving flexibility. At the same time, connection is more conducive to program expansion.

To understand them, you 'd better take a look at the event listener and adapter related content in JAVA graphic interface programming.

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.