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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Manoj BhoirPosted May 7, 2015, 12:11 PM
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.
Vincent Maverick DuranoPosted May 5, 2015, 9:03 AM
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.
Nanhe SiddiquePosted May 5, 2015, 12:20 AM
http://www.codeproject.com/Questions/382073/Where-to-Use-Abstact-Class-and-Where-to-Use-Interf
Rajeesh MenothPosted May 4, 2015, 11:46 PM
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