Oops: (B) What Is Object Oriented Programming ?
Oops: (B) What Is Object Oriented Programming ?
OOPS
(B) What is Object Oriented Programming ?
It is a problem solving technique to develop software systems.Its a technique to think real world in terms of objects.Object maps the software model to real world concept.These objects have responsibilities and provide services to application or other objects.
Abstraction It allows complex real world to be represented in simplified manner.Example color is abstracted to RGB.By just making the combination of these three colors we can achieve any color in world.Its a model of real world or concept. Encapsulation
1
The process of hiding all the internal details of an object from the outside world. Communication using messages When application wants to achieve certain task it can only be done using combination of objects.A single object can not do all the task.Example if we want to make order processing form. We will use Customer object , Order object , Product object and Payment object to achieve this functionality.In short these objects should communicate with each other.This is achieved when objects send messages to each other. Object lifetime All objects have life time.Objects are created , initialized , necessary functionalities are done and later the object is destroyed.Every object have there own state and identity , which differ from instance to instance. Class hierarchies (Inheritance and aggregation) Twist :- Whats difference between Association , Aggregation and Inheritance relationships? In object oriented world objects have relation and hierarchies in between them.There are basically three kind of relationship in Object Oriented world :-
Association
This is the simplest relationship between objects.Example every customer has sales.So Customer object and sales object have a association relation between them.
Aggregation
This is also called as composition model.Example in order to make a Accounts class it has use other objects example Voucher,Journal and Cash objects.So accounts class is aggregation of these three objects.
Inheritance
Hierarchy is used to define more specialized classes based on a preexisting generalized class.Example we have VEHICLE class and we can inherit this class make more specialized class like CAR, which will add new attributes and use some existing qualities of the parent class.Its shows more of a parent-child relationship .This kind of hierarchy is called inheritance. Polymorphism When inheritance is used to extend a generalized class to a more specialized class,it includes behavior of the top clas(Generalized class).The inheriting class often implement a behavior that can be somewhat different than the generalized class, but the name of the behavior can be same.It is important that a given instance of an object use the correct behavior, and the property of polymorphism allows this to happen automatically.
2
Public Class ClsChild Inherits ClsParent this is child and a special parse function is added which will also parse / Public Function ParseBackSlash() Dim PstrData As String PstrData = Me.Parse() PstrData = Replace(PstrData, /, ) Return PstrData End Function End Class
Above is the source code for ClsChild which does the remaining work.It adds extra functionality by parsing / junk characters of the data. Note:- Strdata was accessible only because it was defined as protected in the parent 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 it's 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. Note:- In order to understand the concept simple sample of add and multiply functionality is implemented in W indowsAbstract folder in CD.
From interview point of view just saying using MustInherit keyword is more than enough to convince that you have used abstract classes.But to clear simple fundamental lets 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 MultiplyNumber.In the child class we inherit the abstract class and implement the multiplynumber function. Definitely this sample does not take out actually how things are implemented in live projects.Basically you put all your common functionalities or half implemented functionality in parent abstract class and later let child class define the full functionality
4
of the abstract class.Example i 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 seconf 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
My attitude towards abstract class has been that i put all my common functionality in abstract class.
If a class implements a interface then it has to provide implementation to all its methods. In sample there are two files.One has the interface definition and other class implements the interface.Below is the source code IInterface is the interface and ClsDosomething implements the IInterface.This sample just displays a simple message box.
Public Interface IInterFace Sub DoSomething() End Interface Public Class ClsDoSomething Implements IInterFace Public Sub DoSomething() Implements WindowsInterFace.IInterFace.DoSomething MsgBox(Interface implemented) End Sub End Class
Interfaces do not come in inheriting chain , while abstract classes come in inheritance.
In the above there is a method called AddString() which adds a string to a listbox.You can also see a delegate declared as :Public Delegate Sub DelegateAddString() This delegate signature is compatible with the AddString method.When i mean compatibility that means that there return types and passing parameter types are same.Later in command click of the button object of the Delegate is created and the method pointer is received from AddressOf keyword.Then by using the Invoke method the method is invoked.
Public Class FrmWithEvents Inherits System.Windows.Forms.Form Private WithEvents mobjClsWithEvents As New ClsWithEvents() Private Sub FrmWithEvents_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub Private Sub mobjClsWithEvents_EventAddString(ByVal Value As String) Handles mobjClsWithEvents.EventAddString LstData.Items.Add(Value) End Sub
Private Sub CmdRunEvents_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdRunEvents.Click mobjClsWithEvents.AddString() End Sub End Class
Note:- In Sample CD W indowsShadowing is folder which has the sample code.If you run the program you can have two outputs one which shows a integer and other which shows a string.
Public Class ClsParent Public x As Integer End Class Public Class ClsShadowedParent Inherits ClsParent Public Shadows x As String End Class
Just imagine one of your clients doing this c.XyzCallback = null This will reset all your delegates to nothing and you have to keep figuring where the error is.
11
static variable.Even though the object is created and destroyed , the variable values does not change.It retains its old value.
Public Class ClsShared Shared intCount As Integer Public Function AddCount() As Integer intCount = intCount + 1 Return intCount End Function End Class Public Class FrmSharedClasses Inherits System.Windows.Forms.Form Private Sub CmdInstance1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdInstance1.Click Dim pobjClsShared As New ClsShared() MessageBox.Show(The count at this moment is & pobjClsShared.AddCount.ToString()) End Sub Private Sub CmdInstance2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdInstance2.Click Dim pobjClsShared As New ClsShared() MessageBox.Show(The count at this moment is & pobjClsShared.AddCount.ToString()) End Sub End Class
13
14
MessageBox.Show(Hi this is nested class) End Sub End Class End Class
This is the way we can instantiate the nested class and make the method call.
Dim pobjChildNested As New ClsNested.ChildNested() pobjChildNested.ShowMessage()
(I) In below sample code if we create a object of class2 which constructor will fire first ?
Public Class Class1 Sub New() End Sub End Class Public Class class2 Inherits Class1 Sub New() End Sub End Class * I leave this to the readers......
15
which can be overridden and clean up code for unmanaged resources can be put in this section.
16
IDisposable and override the Dispose method of IDisposable interface. Now once your class has exposed the Dispose method its the responsibility of the client to call the Dispose method to do the cleanup.
(A)How do I force the Dispose method to be called automatically, as clients can forget to call Dispose method?
Note :- I admire this question.
17
Call the Dispose method in Finalize method and in Dispose method suppress the finalize method using GC.SuppressFinalize. Below is the sample code of the pattern. This is the best way we do clean our unallocated resources and yes not to forget we do not get the hit of running the Garbage collector twice. Note:- It will suppress the finalize method thus avoiding the two trip. Public Class ClsTesting Implements IDisposable Public Overloads Sub Dispose()Implements IDisposable.Dispose ' write ytour clean up code here GC.SuppressFinalize(Me) End Sub Protected Overrides Sub Finalize() Dispose() End Sub End Class
(I)If we write a goto or a return statement in try and catch block will the finally block execute ?
The code in the finally always runs even if there are statements like goto or a return statements.
18
(A)What is Indexer ?
An indexer is a member that enables an object to be indexed in the same way as an array.
(A)In a program there are multiple catch blocks so can it happen that two catch blocks are executed ?
No once the proper catch section is executed the control goes to finally block.So there will not be any scenarios in which multiple catch blocks will be executed.
19