English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

7 answers

Abstract classes and interfaces are often compared and can sometimes do the same thing.

Abstract classes are base classes where the derived class must implement the methods of that abstract class. However, children of those 'derived' classes do not have to implement the methods of the original base class (abstract class). Furthermore, abstract classes form a 'is-a' relationship, meaning the derived classes "are" of type ...(abstract)...Ex: a Shapes abstract base class and a square derived class. A square is a shape.

Interfaces are different - they are simply guidelines and rules that must be followed by any and all classes that implement them. If you create a class that implements an interface, it must include all the methods in that interface. Then if you go to derive that class, you could overwrite the methods or leave them (as they will have already been implemented in the base class).

Interfaces are not a 'is-a' relationship. For example, you could have a Size interface. A square is not a size, but your square class must implement various size methods (volume, surface area).

In the long run, you will probably use interfaces more often if you are looking at polymorphism and attempting to force classes to include certain methods.

2007-06-21 03:15:06 · answer #1 · answered by Anonymous · 2 1

The Basic Difference is that - An Interface does not define any implementation...only the functions are declared..none of the functions can be defined...also in an interface only constant properties can be declared....whereas...An abstract class does not provide too much of implementation but still you can define the methods in an abstract class that could be common for all sub classes...also a class that once inherits any class ( abstract or normal) then it cannot inherit any other class..this means multiple inhritance not allowed in java......but many interfaces can be "implemented" by a single class...so multiple inheritance is supported by java but only through Interfaces....

2007-06-21 03:58:36 · answer #2 · answered by Anonymous · 2 0

An abstract class is a superclass of which one is not expected to create instances. Instead, it's just a placeholder for common functionality to be inherited by subclasses. Typically, subclasses of an abstract class are still associated by gen-spec .


An interface is similar to a class without instance and static variables (static final constants are allowed), and without method bodies (an abstract class). When a class implements an interface, it agrees to define all methods of that interface. A class can implement many different interfaces.

2007-06-21 03:11:20 · answer #3 · answered by sathiyendran a 3 · 1 0

It relies upon on the language you're asking approximately. normally an precis classification is comparable to an effortless classification different than which you're forbidden from instantiating it. to boot you could declare precis individuals interior the class that could desire to be overridden in derived classes. that's the precis individuals that make the class precis, some languages (like C++) don't have an precis key-word and purely artwork out "that's precis because of the fact that's have been given precis individuals". An interface in many languages purely is composed basically of precis individuals. in case you employ the terminology, an interface defines a behaviour contract while an precis classification can outline the two state and habit to boot to defining a behaviour contract for its descendants. usually the main severe difference between an precis classification and an interface is that a classification can derive from purely another classification (precis or in any different case) while it could enforce many interfaces.

2016-12-08 15:23:32 · answer #4 · answered by kostenbauber 4 · 0 0

The practical functional difference is:

* If you need a place to hang behavior that will be inherited by subclasses, you want an abstract class.

* If you want classes outside the class hierarchy to be imlementers (or "iskindof"), then you want an interface.

Interfaces are a sort of half-baked version of multiple inheritance, an attempt to give the benefits of MI without the problems that come with them.

2007-06-21 03:37:59 · answer #5 · answered by McFate 7 · 3 0

Interface = Class with dummy methods
Abstract class = Class which can be inherited

2007-06-21 03:14:18 · answer #6 · answered by Vega_cs 2 · 2 0

One promotes Inheritance whereas other doesn't. It has been explained well here - http://www.buggybread.com/2013/07/java-importance-of-abstract-classes-and.html

2014-09-21 04:19:04 · answer #7 · answered by vivek 1 · 0 0

fedest.com, questions and answers