OOPS
How to decide when to use abstract class and when to use interface
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Rajeesh MenothRank #118 · 18.4K reputationPosted May 4, 2015, 7:46 PM EDT
Abstract Class:
-Abstract class provides a set of rules to implement next class
-Rules will be provided through abstract methods
-Abstract method does not contain any definition
-While inheriting abstract class all abstract methods must be override
-If a class contains at least one abstract method then it must be declared as an "Abstract Class"
-Abstract classes cannot be instantiated (i.e. we cannot create objects), but a reference can be created
-Reference depends on child class object's memory
-Abstract classes are also called as "Partial abstract classes"
-Partial abstract class may contain functions with body and functions without body
-If a class contains all functions without body then it is called as "Fully Abstract Class" (Interface)
Interface:
-If a class contains all abstract methods then that class is known as "Interface"
-Interfaces support like multiple inheritance
-In interface all methods r public abstract by default
-Interfaces r implementable
-Interfaces can be instantiated, but a reference cannot be created
Nanhe SiddiqueRank #1,103 · 1.7K reputationPosted May 4, 2015, 8:20 PM EDT
http://www.codeproject.com/Questions/382073/Where-to-Use-Abstact-Class-and-Where-to-Use-Interf
Vincent Maverick DuranoRank #238 · 10.3K reputationPosted May 5, 2015, 5:03 AM EDT
An abstract class can't be instantiated directly. Therefore, to use its functionality it must be inherited by another class which defines the implementation of any abstract members. In this way it is similar to an interface. The difference between an interface and an abstract class is that an interface has zero implementation details. All members of an interface are abstract and must be implemented. An abstract class can have a combination of abstract and concrete members.
Manoj BhoirRank #599 · 4K reputationPosted May 7, 2015, 8:11 AM EDT
First check what is abstract class and interface.
http://www.codeproject.com/Articles/6118/All-about-abstract-classes
http://www.codeproject.com/Articles/18743/Interfaces-in-C-For-Beginners
Use an abstract class
-When creating a class library which will be widely distributed or reused—especially to clients, use an abstract class in preference to an interface; because, it simplifies versioning. This is the practice used by the Microsoft team which developed the Base Class Library. ( COM was designed around interfaces.)
-Use an abstract class to define a common base class for a family of types.
-Use an abstract class to provide default behavior.
-Subclass only a base class in a hierarchy to which the class logically belongs.
Use an interface
-When creating a standalone project which can be changed at will, use an interface in preference to an abstract class; because, it offers more design flexibility.
-Use interfaces to introduce polymorphic behavior without subclassing and to model multiple inheritance—allowing a specific type to support numerous behaviors.
-Use an interface to design a polymorphic hierarchy for value types.
-Use an interface when an immutable contract is really intended.
-A well-designed interface defines a very specific range of functionality. Split up interfaces that contain unrelated functionality.