What is the difference between an interface and ab

2018-12-31 02:16发布

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

30条回答
只若初见
2楼-- · 2018-12-31 02:22

Let's work on this question again:

The first thing to let you know is that 1/1 and 1*1 results in the same, but it does not mean that multiplication and division are same. Obviously, they hold some good relationship, but mind you both are different.

I will point out main differences, and the rest have already been explained:

Abstract classes are useful for modeling a class hierarchy. At first glance of any requirement, we are partially clear on what exactly is to be built, but we know what to build. And so your abstract classes are your base classes.

Interfaces are useful for letting other hierarchy or classes to know that what I am capable of doing. And when you say I am capable of something, you must have that capacity. Interfaces will mark it as compulsory for a class to implement the same functionalities.

查看更多
余生无你
3楼-- · 2018-12-31 02:23

Some important differences:

In the form of a table:

Difference

As stated by Joe from javapapers:

1.Main difference is methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can have instance methods that implements a default behavior.

2.Variables declared in a Java interface is by default final. An abstract class may contain non-final variables.

3.Members of a Java interface are public by default. A Java abstract class can have the usual flavors of class members like private, protected, etc..

4.Java interface should be implemented using keyword “implements”; A Java abstract class should be extended using keyword “extends”.

5.An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.

6.A Java class can implement multiple interfaces but it can extend only one abstract class.

7.Interface is absolutely abstract and cannot be instantiated; A Java abstract class also cannot be instantiated, but can be invoked if a main() exists.

8.In comparison with java abstract classes, java interfaces are slow as it requires extra indirection.

查看更多
像晚风撩人
4楼-- · 2018-12-31 02:23

When you want to provide polymorphic behaviour in an inheritance hierarchy, use abstract classes.

When you want polymorphic behaviour for classes which are completely unrelated, use an interface.

查看更多
姐姐魅力值爆表
5楼-- · 2018-12-31 02:23

I am constructing a building of 300 floors

The building's blueprint interface

  • For example, Servlet(I)

Building constructed up to 200 floors - partially completed---abstract

  • Partial implementation, for example, generic and HTTP servlet

Building construction completed-concrete

  • Full implementation, for example, own servlet

Interface

  • We don't know anything about implementation, just requirements. We can go for an interface.
  • Every method is public and abstract by default
  • It is a 100% pure abstract class
  • If we declare public we cannot declare private and protected
  • If we declare abstract we cannot declare final, static, synchronized, strictfp and native
  • Every interface has public, static and final
  • Serialization and transient is not applicable, because we can't create an instance for in interface
  • Non-volatile because it is final
  • Every variable is static
  • When we declare a variable inside an interface we need to initialize variables while declaring
  • Instance and static block not allowed

Abstract

  • Partial implementation
  • It has an abstract method. An addition, it uses concrete
  • No restriction for abstract class method modifiers
  • No restriction for abstract class variable modifiers
  • We cannot declare other modifiers except abstract
  • No restriction to initialize variables

Taken from DurgaJobs Website

查看更多
浪荡孟婆
6楼-- · 2018-12-31 02:24

The main point is that:

  • Abstract is object oriented. It offers the basic data an 'object' should have and/or functions it should be able to do. It is concerned with the object's basic characteristics: what it has and what it can do. Hence objects which inherit from the same abstract class share the basic characteristics (generalization).
  • Interface is functionality oriented. It defines functionalities an object should have. Regardless what object it is, as long as it can do these functionalities, which are defined in the interface, it's fine. It ignores everything else. An object/class can contain several (groups of) functionalities; hence it is possible for a class to implement multiple interfaces.
查看更多
梦寄多情
7楼-- · 2018-12-31 02:25

I don't want to highlight the differences, which have been already said in many answers ( regarding public static final modifiers for variables in interface & support for protected, private methods in abstract classes)

In simple terms, I would like to say:

interface: To implement a contract by multiple unrelated objects

abstract class: To implement the same or different behaviour among multiple related objects

From the Oracle documentation

Consider using abstract classes if :

  1. You want to share code among several closely related classes.
  2. You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).
  3. You want to declare non-static or non-final fields.

Consider using interfaces if :

  1. You expect that unrelated classes would implement your interface. For example,many unrelated objects can implement Serializable interface.
  2. You want to specify the behaviour of a particular data type, but not concerned about who implements its behaviour.
  3. You want to take advantage of multiple inheritance of type.

abstract class establishes "is a" relation with concrete classes. interface provides "has a" capability for classes.

If you are looking for Java as programming language, here are a few more updates:

Java 8 has reduced the gap between interface and abstract classes to some extent by providing a default method feature. An interface does not have an implementation for a method is no longer valid now.

Refer to this documentation page for more details.

Have a look at this SE question for code examples to understand better.

How should I have explained the difference between an Interface and an Abstract class?

查看更多
登录 后发表回答