Abstract - An abstract class is a class that can not be instantiated but that can contain code. - It is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards. - A class only inherit from one abstract class. - In an abstract class some methods can have implementation. - An abstract class has access modifiers. - An abstract class can define data members. - When we create an abstract class, we are creating a base class that might have one or more completed methods but at least one or more methods are left uncompleted
and declared abstract. - The purpose of an abstract class is to provide a base class definition for how a set of derived classes will work and then allow the programmers to fill the
implementation in the derived classes. - If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly. - An abstract class can have fields and constrants defined - If various implementations are of the same kind and use common behaviour or status then abstract class is better to use.
Interface - An interface only contains method definitions but does not contain any code. - It is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. - A class can implement more than one interface.Interfaces are used to implement multiple inheritance. - In an Interface class , all methods are abstract - there is no implementation. We need to implement all the methods defined in the interface. - An Interface class,has no access modifiers for the functions, properties etc everything is assumed as public - Interface cannot define data members. - Interfaces are used to define the peripheral abilities of a class. In other words both Human and Tree can implement the IAge interface. - When we create an interface, we are basically creating a set of methods without any implementation that must be overridden by the implemented classes - If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method. - No fields can be defined in interfaces - If various implementations only share method signatures then it is better to use Interfaces.
There are some set of similarities between an abstract class an interface like both can have abstract methods,but opposed to interface an abstract class can also have non abstract methods. It is better to use abstract class when it has a behaviour common to derived classes, but if we have different behavior for sub classes we go for an interface as by creating an interface we can move our implemtation to any class.An abstract class enforces certain behaviour for sub classes.Some of the differnces between an abstract class and interface are listed below: A class can inherit multple interfaces whereas it can inherit only a single abstarct class. No implementation is provided in interface on the other hands an abstract class can contain implementations for certain methods along with abstarct methods. It is difficult to track down all the implemnetations incase a new method is added to an interface whereas we have have an option to provide default implementation for any new method added in abstarct class. Interface is used to implemnt the logic whereas abstract class is used to extend it. All the methods in an interface are public by default wheras abstract class can have non public methods.
Abstract classes – Can only be extended but not instantiated. When abstract methods are extended, derived class must provide implementation for all the abstract methods. Abstract class can have abstract methods and non-abstract methods. Derived class can only derive one abstract class or one non-abstract class. Interface class – Interfaces are pure abstract class and contains only abstract method (i.e methods will have only method signatures and not implemented). Interfaces can only be implemented by the derived class. Derived class can implement any no of interface.
This group is an initiative from "Vidya Vrat Agarwal" a .NET Passionate. This group is created with a vision to encourage and help people learn Microsoft .NET technology. This group also provides a platform to share ideas, thoughts and experiences an individual may have while learning and implementing .NET technologies.
5 comments:
Abstract
- An abstract class is a class that can not be instantiated but that can contain code.
- It is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.
- A class only inherit from one abstract class.
- In an abstract class some methods can have implementation.
- An abstract class has access modifiers.
- An abstract class can define data members.
- When we create an abstract class, we are creating a base class that might have one or more completed methods but at least one or more methods are left uncompleted
and declared abstract.
- The purpose of an abstract class is to provide a base class definition for how a set of derived classes will work and then allow the programmers to fill the
implementation in the derived classes.
- If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.
- An abstract class can have fields and constrants defined
- If various implementations are of the same kind and use common behaviour or status then abstract class is better to use.
Interface
- An interface only contains method definitions but does not contain any code.
- It is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments.
- A class can implement more than one interface.Interfaces are used to implement multiple inheritance.
- In an Interface class , all methods are abstract - there is no implementation. We need to implement all the methods defined in the interface.
- An Interface class,has no access modifiers for the functions, properties etc everything is assumed as public
- Interface cannot define data members.
- Interfaces are used to define the peripheral abilities of a class. In other words both Human and Tree can implement the IAge interface.
- When we create an interface, we are basically creating a set of methods without any implementation that must be overridden by the implemented classes
- If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method.
- No fields can be defined in interfaces
- If various implementations only share method signatures then it is better to use Interfaces.
There are some set of similarities between an abstract class an interface like both can have abstract methods,but opposed to interface an abstract class can also have non abstract methods.
It is better to use abstract class when it has a behaviour common to derived classes, but if we have different behavior for sub classes we go for an interface as by creating an interface we can move our implemtation to any class.An abstract class enforces certain behaviour for sub classes.Some of the differnces between an abstract class and interface are listed below:
A class can inherit multple interfaces whereas it can inherit only a single abstarct class.
No implementation is provided in interface on the other hands an abstract class can contain implementations for certain methods along with abstarct methods.
It is difficult to track down all the implemnetations incase a new method is added to an interface whereas we have have an option to provide default implementation for any new method added in abstarct class.
Interface is used to implemnt the logic whereas abstract class is used to extend it.
All the methods in an interface are public by default wheras abstract class can have non public methods.
Abstract classes –
Can only be extended but not instantiated.
When abstract methods are extended, derived class must provide implementation for all the abstract methods.
Abstract class can have abstract methods and non-abstract methods.
Derived class can only derive one abstract class or one non-abstract class.
Interface class –
Interfaces are pure abstract class and contains only abstract method (i.e methods will have only method signatures and not implemented).
Interfaces can only be implemented by the derived class.
Derived class can implement any no of interface.
Post a Comment