Q 1. What are abstract classes ?
Ans - Following are the features of a abstract class :-
- You can not create a object of abstract class.
- Abstract class is designed to act as a base class (to be inherited by other classes). Abstract class is a design concept in program development and provides a base upon which other classes are built.
- Abstract classes are similar to interfaces. After declaring an abstract class, it cannot be instantiated on its own, it must be inherited.
- Abstract classes can have implementation or pure abstract methods which should be implemented in the child class.
- In VB.NET abstract classes are created using "MustInherit" keyword.In C# we have "Abstract" keyword.
An abstract class only allows other classes to inherit from it and cannot be instantiated. When we create an abstract class, it should have one or more completed methods but at least one or more uncompleted methods and these must be preceded by the key word abstract. If all the methods of an abstract class are uncompleted then it is the same as an interface, but there is a restriction that it cannot make a class inherit from it, which means it can not work as a base class.
Q 2. What is a Interface ?
Ans - An interface is defined by the key word interface. An interface has no implementation; it only has the definition of the methods without the body. When we create an interface, we are basically creating a set of methods without any implementation. A class implementing an interface must provide the implementation of the interface members. All the methods and properties defined in an interface are by default public and abstract.
Interface is a contract that defines the signature of the functionality. So if a class is implementing a interface it says to the outer world, that it provides specific behavior. Example if a class is implementing Idisposable interface that means it has a functionality to release unmanaged resources. Now external objects using this class know that it has contract by which it can dispose unused unmanaged objects.
- Single Class can implement multiple interfaces.
- If a class implements a interface then it has to provide implementation to all its methods.
Q 3. What is the difference between abstract classes and interfaces?
Ans - Following are the differences between abstract and interfaces :-
- An abstract class may contain complete or incomplete methods. Interfaces can contain only the signature of a method but no body. Thus an abstract class can implement methods but an interface can not implement methods.
- An abstract class can contain fields, constructors, or destructors and implement properties. An interface can not contain fields, constructors, or destructors and it has only the property's signature but no implementation.
- An abstract class cannot support multiple inheritance, but an interface can support multiple inheritance. Thus a class may inherit several interfaces but only one abstract class.
- A class implementing an interface has to implement all the methods of the interface, but the same is not required in the case of an abstract Class.
- Various access modifiers such as abstract, protected, internal, public, virtual, etc. are useful in abstract Classes but not in interfaces.
- Abstract classes are faster than interfaces.
No comments:
Post a Comment