.NET and OOPS interview questions - How will you define abstract classes?
Shivprasad Koirala
Abstract class is used to define common functionalities in a distributed environment.
Abstract Class: Abstract class is a base class from which a derived classes can get inherited. If we want a method to be overridden in all child classes then we have to declared that method as abstract method. If a class contains at least one abstract method then that class must be declared as abstract class. Abstract class can contain both abstract and non abstract methods. Abstract class can implement more than one interface. The implementation of abstract methods should be provided in the class that is inheriting abstract class.
The abstract keyword is used to create abstract classes. An abstract class is incomplete and hence cannot be instantiated. An abstract class can only be used as base class. An abstract class cannot be sealed. Abstract class may contain abstract members, but not mandatory. A non abstract class derived from an abstract class must provide implementation for all inherited abstract members.If a class inherit from an abstract class there are 2 options available for that class1) Provide implementation for all the abstract members inherited from the base abstract class2) If the class does not wish to provide implementation for all the abstract members inherited from the abstract class, then the class has to be marked as abstract.
Abstract classes are one of the essential behaviors provided by .NET. Commonly, you would like to make classes that only represent base classes, and don't want anyone to create objects of these class types. You can make use of abstract classes to implement such functionality in C# using the modifier 'abstract'.______________________________________________________________Abroad Consultants in India
Abstract Classes
An abstract class is the one that is not used to create objects. An 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 it's own, it must be inherited. Like interfaces, abstract classes can specify members that must be implemented in inheriting classes. Unlike interfaces, a class can inherit only one abstract class. Abstract classes can only specify members that should be implemented by all inheriting classes.
More Details : Abstract
From interview point of view, just saying using “Must Inherit” keyword is more than enough to convince that you have used abstract classes. But to clear simple fundamental let us try to understand the sample code. There are two classes one is “ClsAbstract” class and other is “ClsChild” class. “ClsAbstract” class is a abstract class as you can see the mustinherit keyword. It has one implemented method “Add” and other is abstract method, which has to be implemented by child class “Multiply Number”. In the child class, we inherit the abstract class and implement the multiply number function.
Definitely, this sample does not take out actually how things are implemented in live projects. You put all your common functionalities or half implemented functionality in parent abstract class and later let child class define the full functionality of the abstract class. Example we always use abstract class with all my SET GET properties of object in abstract class and later make specialize classes for insert, update, delete for the corresponding entity object.
Public MustInherit Class ClsAbstract ‘Use the mustinherit class to declare the class as abstract Public Function Add(ByVal intnum1 As Integer, ByVal intnum2 As Integer) As Integer Return intnum1 + intnum2 End Function ‘Left this second function to be completed by the inheriting class Public MustOverride Function MultiplyNumber(ByVal intnum1 As Integer, ByVal intnum2 As Integer) As Integer End Class
Public Class ClsChild Inherits ClsAbstract ‘ class child overrides the Multiplynumber function Public Overrides Function MultiplyNumber(ByVal intnum1 As Integer, ByVal intnum2 As Integer) As Integer Return intnum1 * intnum2 End Function End Class
Following is the output snapshot for the above code:
My attitude towards abstract class has been that i put all my common functionality in abstract class.
Following you can also view video on different types of collections available in .NET: