10

From my experience I think the following is true. If I am missing a major point please let me know.

Interface:

Every single Method declared in an Interface will have to be implemented in the subclass. Only Events, Delegates, Properties (C#) and Methods can exist in a Interface. A class can implement multiple Interfaces.

Abstract Class:

Only Abstract methods have to be implemented by the subclass. An Abstract class can have normal methods with implementations. Abstract class can also have class variables beside Events, Delegates, Properties and Methods. A class can only implement one abstract class only due non-existence of Multi-inheritance in C#.

So even that difference doesn't explain the question

1) What if you had an Abstract class with only abstract methods? How would that be different from an interface?

2) What if you had a Public variable inside the interface, how would that be different than in Abstract Class?

So any explanation will be vary help full.

2
  • Not sure what you mean by two. Jul 23, 2013 at 12:44
  • 2
    you can have protected abstact methods in abstract classes Jul 23, 2013 at 12:46

12 Answers 12

4

Besides the technical differences it is mainly the intension of your design that should lead you to the decision to use one or the other:

Interfaces define the public API of the classes implementing them. Your goal of using an interface should be to show the usage of the classes that implement it. It is no side effect but a central design goal that a class can implement different interfaces to show the different roles it can act in.

An abstract class should implement some basic algorithm or common behaviour. It is mainly to join the common functionality of the subclasses in one place. Its purpose is to define the internal usage or flow and not the public interface. If you want to publish the usage of an abstract class it should implement a separate interface.

So:

1) An abstract class with only public abstract methods does not make any sense when you use the guidelines above. An abstract class can define protected abstract methods to define a flow or algorithm. But that is not possible with an interface.

2) Aditionally to the public properties abstract classes can define protected instance variables and therefor have many more usage scenarios (see explanation above).

EDIT: The "java" tag was removed by the author. I tried to make this as general as possible and it should be true for both java and C#

1
  • The "java" tag was removed by the author. I tried to make this as general as possible and it should be true for both java and C# Jul 23, 2013 at 13:41
3

In Java:

An abstract class can implement an interface.

An interface cannot extend an abstract class.

BTW: Strangely - an abstract class can implement and interface without actually doing so.

interface I {
  public String hello ();
}

interface J {
  public String goodbye ();
}

abstract class A implements I, J {
  @Override
  abstract public String hello ();
}

class B extends A {

  @Override
  public String hello() {
    return "Hello";
  }

  @Override
  public String goodbye() {
    return "goodbye";
  }

}
0
3

All the variables of an Interface are by default public and static, you can not have a only public variable in an interface, whereas in an Abstract class you can declare a public variable.

If a class extends an Abstract class there is no any contract between them. Class which extends it may or may not override the abstract methods, however in case of interface there is a strict contract between the interface and the class that implements it, i.e the class will have to override all the method of that interface. So from the abstract method point of view they appears to be same, but are having completely different properties and advantages.

4
  • 1
    The "contract" between them is called the Liskov Substitution Principle, and is one of the most basic principles of good OO design. That basically says that if B inherits from A, then a B is an A, and has a responsibility to maintain all the properties that are provable about As.
    – cHao
    Jul 23, 2013 at 13:02
  • its not compulsory to implement all methods of interface..you can make the class abstract and implement interface partially
    – Anirudha
    Jul 23, 2013 at 13:13
  • Agree, but what about concrete classes? Jul 23, 2013 at 13:16
  • In Java it is not possible what you say: A concrete class have to implement all abstract methods. Is it possible in C# not to implement an abstract method from an inherited abstract class leaving it "empty"? Waht would be the sense in doing that? A user of your concrete class relies on the functionality that was granted by the class you inherited from. Jul 23, 2013 at 13:33
2

While your question indicates it's for "general OO", it really seems to be focusing on .NET use of these terms.

  • interfaces can have no state or implementation
  • a class that implements an interface must provide an implementation of all the methods of that interface
  • abstract classes may contain state (data members) and/or implementation (methods)
  • abstract classes can be inherited without implementing the abstract methods (though such a derived class is abstract itslef)
  • interfaces may be multiple-inherited, abstract classes may not (this is probably the key concrete reason for interfaces to exist separately from abtract classes - they permit an implementation of multiple inheritance that removes many of the problems of general MI).

As general OO terms, the differences are not necessarily well-defined. For example, there are C++ programmers who may hold similar rigid definitions (interfaces are a strict subset of abstract classes that cannot contain implementation), while some may say that an abstract class with some default implementations is still an interface or that a non-abstract class can still define an interface.

Indeed, there is a C++ idiom called the Non-Virtual Interface (NVI) where the public methods are non-virtual methods that 'thunk' to private virtual methods:

http://www.gotw.ca/publications/mill18.htm http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Non-Virtual_Interface

1
  • 2
    This answer restates the OP's original knowledge of the problem and completely ignores the actual question.
    – mao47
    Jul 23, 2013 at 13:03
2

What if you had an Abstract class with only abstract methods? How would that be different from an interface?

  • You can implement multiple interfaces but extend only one class
  • Abstract class are more immune to changes then interface becuase if you change an interface it would break the class implementing it.
  • Interface can have only static final fields..Abstract class can have any type of fields.
  • interface don't have constructor but abstract class can have it

But java docs say this

If an abstract class contains only abstract method declarations, it should be declared as an interface instead.

1
  • The general OO guidelines apply to each OO language and most of the technical specifications are the same. The short cite of @Anirudh is (despite its source) language agnostic and holds true for C# as well. Jul 23, 2013 at 13:46
2

Even if all the methods in today's version of an abstract class are abstract, future version of the class could add virtual or non-virtual methods without forcing modifications to implementations nor recompilation of consumers. By contrast, adding any member to an interface will generally require all classes which implement the interface be modified to implement that member, and both implementations and consumers will generally have to be recompiled regardless of whether the change added anything that wasn't already implemented.

The fact that abstract changes may be changed without breaking implementations or consumers is a big advantage in favor of abstract classes. On the other hand, an abstract class will force any implementing class to derive from it alone and no other class. By contrast, an interface will pose almost restrictions on what its implementers are allowed to inherit or derive from. That is a big advantage in favor of interfaces.

Because abstract classes and interfaces each have definite advantages, there are times when either may be better than the other. Conceptually, it would be possible to add a couple features to the way interfaces work that would give them the advantages presently enjoyed only by abstract classes, but I know of no particular plans to do so.

1

Your class can extends only one abstract class and implements many interfaces.

0
1

Well, in an abstract class you could also have fields, and auto-properties wouldn't need to be reimplemented. You can also specify access specifiers that aren't public. Also, it has better scalability (e.g. you can use [Obsolete] to mark an old implementation, and then make the new one call the old one by default). Also, it would stop you from having any more inheritance of classes. Another thing is that you can set static fields in abstract classes.

Also, interfaces are usually something that performs an action, while classes are about being that.

2
  • You mean "make the old one call the new one by default", don't you? The other way around this makes no sense. Jul 23, 2013 at 13:19
  • @MarcvonRenteln No, if it's not implemented it just falls back on the old one. Jul 23, 2013 at 13:21
1
*1) What if you had an Abstract class with only abstract methods? How would that be different from an interface?*

By default the methods in an interface are 'public abstract' and the abstract class will also have the abstract methods as 'public abstract'. If the abstract class contains only abstracts methods then it's better to make it an interface.

*2) What if you had a Public variable inside the interface, how would that be different than in Abstract Class?*

Interfaces can't have variables. If you meant properties, events, delegates etc... they would be by default 'Public'. If nothing is specified in the abstract class it would be 'Private'(In regards to members of the interface/abstract class only).

0

An interface is used when you want your class to be able to do something.

Your class extends an abstract class when there is a 'is a' relationship.

There is a semantic difference.

0

In case of abstract class.

class Dog : abstractAnimal

When we create object of Dog, we will have to create object of abstractAnimal to, it will lead to extra object creation.

In case of interface.

class Dog : IAnimal

When we create object of Dog, we will not be creating any extra object of anything.

0

In that case you can say:

1) We can specify different access modifier to methods present in class, but we can't change access modifier of Interface member.

2) Derived class from abstract will not have a compulsion of implementation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.