Showing posts with label abstract class. Show all posts
Showing posts with label abstract class. Show all posts

Wednesday, September 7, 2011

C# and .NET interview questions: - Differences between abstract class and interface?

Differences between abstract class and Interface is as below:-
Implementation - In Abstract class some methods in abstract classes can have implementation and In Interface all methods, function, properties in interfaces have to empty compulsorily.
Scenario- In Abstract class abstract classes are used when we want to share common functionality in parent child relationship and In Interface - Interfaces are used to define contract, enforce standardization, decoupling and dynamic polymorphism.

Variable declaration-
In Abstract class we can declare variables and In Interface we cannot do that.
Inheritance vs Implementation-Abstract classes are inherited and Interface are implemented.

Watch video on regular expression with practical demonstration as follows: -




Get more materials on important Dotnet interview questions

Regards,

See more stuffs on author’s blog for Most asked Dotnet interview questions

Wednesday, July 6, 2011

.NET and OOPS interview questions - What are abstract classes?

In the following manner we can describe abstract class: -

  • We 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.

  • In VB.NET, abstract classes are created using “MustInherit” keyword.In C# we have “Abstract” keyword.

  • Abstract classes can have implementation or pure abstract methods, which should be implemented in the child class.
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:


Please click here to see more .NET/OOPS interview questions

Regards,
Visit Authors blog for more .NET and OOPS interview questions

Monday, February 7, 2011

.Net and ASP.NET Interview Questions- Why can't we instantiate an abstract class?

.Net and ASP.NET Interview Questions- Why can't we instantiate an abstract
class?


Answers:-
This is a nice .NET interview question. Abstract class is a half defined class and there is not point in creating a object of half a defined class. So you need to define a full implementation by creating a child class and you can then create the object of the child class.

Click here for 32 basic .NET interview questions and answers


Wednesday, December 1, 2010

C# and .NET OOP (Object oriented  programming) interview questions - Abstract classes and interfaces.


C# and .NET OOP (Object oriented programming) interview questions
-
Abstract classes and interfaces.


I have yet to remember a .NET interviewer who never asked about abstract classes, interfaces and object oriented interview questions . I am putting forward questions which comes around abstract classes and interfaces again and again...Hope every one benefits.
Normally the C# interviewer starts with.....

What is abstract class ?
Abstract class is a base class or a parent class. Abstract classes can have empty abstract methods or it can have implemented methods which can be overridden by child classes.

The next question i expected
was on interfaces and yes there it comes.


What are interfaces?
Interface is a contract class with empty methods , properties and functions. Any class which implements the interface has to compulsory implement all the empty methods , functions and properties of the interface.
Now the 1000% sure
question was bound to come difference between them...



What's the difference between abstract class and interface?
There are many differences, below are some key two differences :-

  • Abstract class are base class or parent class while interfaces are
    contracts.

  • Abstract class can have some implemented methods and functions while
    interfaces methods and functions are completely empty.

  • Abstract classes are inherited while interfaces are implemented.

  • Abstract classes are used when we want to increase reusability in
    inheritance while interfaces are used to force a contract.

Can we create a object of abstract class or interface?
No we can not.
Now the practical
question..


In what scenarios will you use a abstract class and in what scenarios will
you use a interface?

If you want to increase reusability in inheritance then abstract classes are
good. If you want implement or force some methods across classes must be for
uniformity you can use a interface. So to increase reusability via inheritance
use abstract class as it is nothing but a base class and to force methods use
interfaces.
Really friends having C# experience is one thing and cracking
Dot net and C# interviews is a different ball game all together .