Showing posts with label oops. Show all posts
Showing posts with label oops. Show all posts

Thursday, February 23, 2012

.NET interview questions: - Can you explain the advantages of using OOPS over functional programming?

In fact this one is the basic but very important .NET interview questions asked in almost every interview. An interviewer expects that every candidate should know and answer it. So if you are not sure about it so read the following answer thoroughly and get clear your fundamental related to OOPS.
  • Abstraction: - Abstraction filters un-necessary information from the user’s perspective. It just gives enough information to the user rather than giving him unnecessary information by which he can get confused. Abstraction can be different from different user perspective. For instance below figure 'Abstraction perspective' shows how a simple user looks at a television and how a TV mechanic looks at the television. From a simple user perspective he only needs to start, stop, change channels and control volume. But when a TV mechanic looks at the box he is concerned with details like CRT, Electrical Components, Wires, connection, cooling fan etc. So abstraction is about two things one is show only what is needed and second the need should be from end users perspective.


Figure: - Abstraction Perspective

Ok, now how does that fit in to the software world? When we design components we should know what type of user will be using the component. Figure ‘Abstraction in action’ shows how the customer components when has different view of abstraction for the internal and external developer.


Figure: - Abstraction in Action
  • Encapsulation: - Encapsulation separates an object interface from its implementation. It hides internal implementation details from the users. You can get a better feel by looking at the figure ‘Encapsulation and Abstraction’. It shows a simple stock class which allows a user to add and remove products from the stock. It also has a restock and alert in case products are out of stock. End user is only exposed to operations like ‘addstock’,’removestock’ and get the current level of the stock. He is completely isolated from auto functionalities like how the restock operation takes place. These operations are encapsulated from the end user perspective.
Note :- Abstraction means to expose the necessary functionalities while encapsulation means to hide the complexity. They both complement each other, but there is a huge difference in terms of thinking approach. In interviews ‘difference between abstraction and encapsulation’ is one of the favorites questions among interviewers. So understand the concept fundamentally as you can get in to long talks of confusion with the interviewer.


Figure: - Encapsulation and Abstraction
In java or C# encapsulation is achieved by using private, public and protected access modifiers.
  • Inheritance: - OOPS is all about mapping with real world. One of the relationships in real world is parent child relationship. Inheritance is a methodology by which you can form new classes using existing classes as base. For instance figure ‘Inheritance in action’ shows we have created a parent class called as ‘ClsDisplay’. We can inherit from ‘clsDisplay’ and create bold and italic display classes.

Figure: - Inheritance in action
  • Polymorphism: - Polymorphism means one object can exist in different forms. It refers to ability of the object to take different forms depending on situations. For instance we can use the ‘clsDisplay’ object to point to italic or a bold display object. So depending on the situation the parent class ‘clsDisplay’ can morph/change to any other form.

Figure: - Polymorphism in action
See more on OOPS in video from describing implementation of Aggregation, Association and Composition as follows: -



Visit to get more stuff on Dotnet interview questions and answers

Regards,

Also visit for more author’s other blogs on Most asked .NET 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