Monday 12 September 2011

Inheritence

It is one of the commonly used features in OOPS to avoid the code duplication. It
implements code reutilization in class declaration. Let us take an example and
discuss how the code reutilization is achieved with inheritence. Normally we
create a singe class to represent an entity and its operations. Look at the
following example

Class Employee
Public EmpId As Integer
Private Sal As Double = 0
Public Basic As Double
Public Allowance As Double
Public Deducions As Double
Public FirstName As String
Public LastName As String
Public Address As String
Public Pincode As String
Public Sub DisplayInfo()
Dim msg As String
msg = FirstName & " " & LastName & vbCrLf
msg = msg & Address & vbCrLf
msg = msg & "PIN – " & Pincode
Msgbox(msg)
End Sub
Public ReadOnly Property Salary() As Double
Get
Return Sal
End Get
End Property
Public Sub ProcessSalary()
Sal = Basic + Allowance - Deductions
End Sub
End Class
Employee Class
class Employee
{
public int EmpId;
private double Sal = 0;
public double Basic;
public double Allowance;
public double Deducions;
public string FirstName;
public string LastName;
public string Address;
public string Pincode;
public void DisplayInfo()
{
string msg;
msg = FirstName + " " + LastName + vbCrLf;
msg = msg + Address + vbCrLf;
msg = msg + "PIN – " + Pincode;
MesssgeBox.Show(msg);
}
public double Salary
{
get
{
return Sal;
}
}
public void ProcessSalary()
{
Sal = Basic + Allowance – Deductions;
}
}
In the above example, employee class contains methods and properties defined
in its structure. Employee object is an instance.



Class Customer
Public CustId As Integer
Public DebitBalance As Double
Public FirstName As String
Public LastName As String
Public Address As String
Public Pincode As String
Public Sub DisplayInfo()
Dim msg As String
msg = FirstName & " " & LastName & vbCrLf
msg = msg & Address & vbCrLf
msg = msg & "PIN – " & Pincode
End Sub
Public ReadOnly Property Debit() As Double
Get
Return DebitBalance
End Get
End Property
End Class
class Customer
{
public int CustId;
public double DebitBalance;
public string FirstName;
public string LastName;
public string Address;
public string Pincode;
public void DisplayInfo()
{
string msg;
msg = FirstName + " " + LastName ;
msg = msg + Address ;
msg = msg + "PIN – " ;
}
public double Debit()
{
get
{
return DebitBalance;
}
}
}
Customer class contains methods and properties defined in its structure.
Customer object is an instance.
In these two classes, you might have observed the person identification is same
in both Employee and Customer class. it means firstname, lastname, address
and pincode variable members and displayInfo method is same in both the
classes.
So this common information can be isolated and written in separate class and
inherited into the respective employee and customer class. it is shown in the
following example.

Base Class:-
Class Person
Public FirstName As String
Public LastName As String
Public Address As String
Public Pincode As String
Public Sub DisplayInfo()
Dim msg As String
msg = FirstName & " " & LastName & vbCrLf
msg = msg & Address & vbCrLf
msg = msg & "PIN – " & Pincode
Msgbox(msg)
End Sub
End Class
class Person
{
public string FirstName;
public string LastName;
public string Address;
public string Pincode;
public void DisplayInfo()
{
string msg;
msg = FirstName + " " + LastName;
msg = msg + Address ;
msg = msg + "PIN – " + Pincode;
MessageBox.Show(msg);
}
}



Derived Class:-

Class Customer
Inherits Person
Public CustId As Integer
Public DebitBalance As Double
Public ReadOnly Property Debit() As Double
Get
Return DebitBalance
End Get
End Property
End Class
class Customer:Person
{
public int CustId ;
public double DebitBalance;
public double Debit()
{
get
{
return DebitBalance;
}
}
}


Derived Class:-
Class Employee
Inherits Person
Public EmpId As Integer
Private Sal As Double = 0
Public Basic As Double
Public Allowance As Double
Public Deductions As Double
Public ReadOnly Property Salary() As Double
Get
Return Sal
End Get
End Property
Public Sub ProcessSalary()
Sal = Basic + Allowance - Deductions
End Sub
End Class
Employee Person Class Employee Object
Class +
class Employee: Person
{
public int EmpId ;
private double Sal = 0;
public double Basic ;
public double Allowance;
public double Deductions;
public double Salary
{
get
{
return Sal;
}
}
public void ProcessSalary()
{
Sal = Basic + Allowance – Deductions;
}
}
In the above mentioned example Person class holds the common data
(Firstname, Lastname… etc) and method displayInfo(). It has been inherited in
both employee and customer class. So these two achieves the same functionality
of what we have seen before inheritance. By this point we conclude inheritance
implements reuse of the same code with multiple classes.
One more advantage with inheritance is extensibility of the of the derived class
code. It means the employee and customer class be extended by including its
own methods and properties with the person (Inherited) class. Here the extended
members in Employee class are EmpId, Allowance, ProcessSalary method and
Salary property. The same thing follows in customer class with CustId,
DebitBalance and Debit property.
You might have observed the keywords Base class and Derived class in the
above session. Let us see what it means.
Base class:- A class which contains common properties and methods that can
shared with other classes by inheritance is called Base class. Ex:- Person class
Derived class:- A class which inherits the base class is knows as Derived class.
ex:- Employee class and Customer class.
Implementation:- A derived class can inherit only one base class. its shown in
the above examples, ie., employee class inherits person class and customer
class inherits person class.
You can inherit the base class into derived class using Inherits keyword.
ex:-
Class Employee
Inherits Person
:
:
End Class
class Employee:Person
{
:
:
}
Protected Keyword:- We have already seen the usage of Public and Private
keyword.
As we know, all the Public keyword declarations in the class will be accessed by
the object users and the derived class (the class which inherits the base class).
Private keyword declarations can be accessed only within the class (it means
the class in which the declaration is done).
You may think why this Protected keyword declaration is required.
Its functionality is a hybrid of public and protected keyword. So, its very important
in class inheritance, because in the situation where the data is to be
communicated only between the base and derived classes irrespective of the
external object user (means the end user) the protected keyword is used
Let us take an example and see how it will be used
Base Class:-
Class Person
Public FirstName As String
Public LastName As String
Public Address As String
Public Pincode As String
Protected DateOFBirth As DateTime
Public Sub DisplayInfo()
Dim msg As String
msg = FirstName & " " & LastName & vbCrLf
msg = msg & Address & vbCrLf
msg = msg & "PIN – " & Pincode
msg = msg & "Date of Birth : " & DateOFBirth.ToString
End Sub
End Class
class Person
{
public string FirstName ;
public string LastName ;
public string Address;
public string Pincode;
protected DateTime DateOFBirth ;
public void DisplayInfo()
{
string msg;
msg = FirstName + " " + LastName;
msg = msg + Address ;
msg = msg + "PIN – " + Pincode;
msg = msg + "Date of Birth : " + DateOFBirth.toString;
}
}
The Protected variable dateofbirth is accessed in the displayinfo method of the
base class itself.
Derived Class:-
Class Employee
Inherits Person
Public EmpId As Integer
Private Sal As Double = 0
Public Basic As Double
Public Allowance As Double
Public Deducions As Double
Public ReadOnly Property Salary() As Double
Get
Return Sal
End Get
End Property
Public Sub ProcessSalary()
Sal = Basic + Allowance – Deductions
End Sub
Public ReadOnly Property Age() As Integer
Get
Dim personAge As Integer
personAge = Date.Now.Subtract(DateofBirth).Days
Return personAge
End Get
End Property
End Class
class Employee: Person
{
public int EmpId ;
private double Sal = 0;
public double Basic;
public double Allowance;
public double Deducions;
public double Salary
{
get
{
return Sal;
}
}
public void ProcessSalary()
{
Sal = Basic + Allowance – Deductions;
}
public int Age
{
get
{
int personage;
personAge = Date.Now.Subtract(DateofBirth).Days;
return personage;
}
}
}
As in the same way of base class the protected variable dateofbirth of the base
class is accessed in the derived class. So the protected variable in the base
class looks like a private variable for the derived class and cannot be accessed
by its object users (means outside the class environment).
Instantiation of the Derived Class :- After declaration of the derived class we
can create the object instance of the derived class and use it for the specific task.
This is called Object Instantiation. With the instance of the derived class you can
access all the public properties and methods of both the base and derived
classes.
Let us take an employee class example.
Dim objEmployee1 As New Employee() 'Create an Instance of the
'Employee class
objEmployee1.EmpId = 100 'Derived Class member
objEmployee1.firstname = "Rama" 'Base Class member
objEmployee1.lastname = "S" 'Base Class member
objEmployee1.Address = "#8, Kalidasa road, Mysore"
'Base Class member
objEmployee1.pin = "570002" 'Base Class member
objEmployee1.Basic = 5000 'Derived Class member
objEmployee1.allowances = 4000 'Derived Class member
objEmployee1.Deductions = 1000 'Derived Class member
objEmployee1.ProcessSalary() 'Derived Class member
objEmployee1.DisplayInfo() 'Base Class member
Employee objEmployee1 = new Employee(); 'Create an Instance of the 'Employee
class
objEmployee1.EmpId = 100; 'Derived Class member
objEmployee1.firstname = "Rama"; 'Base Class member
objEmployee1.lastname = "S"; 'Base Class member
objEmployee1.Address = "#8, Kalidasa road, Mysore";
'Base Class member
objEmployee1.pin = "570002"; 'Base Class member
objEmployee1.Basic = 5000; 'Derived Class member
objEmployee1.allowances = 4000; 'Derived Class member
objEmployee1.Deductions = 1000 ; 'Derived Class member
objEmployee1.ProcessSalary(); 'Derived Class member
objEmployee1.DisplayInfo(); 'Base Class member
In the above code, object instance objEmployee of Employee class is created.
And then all the public members of both base and derived class are accessed
and manipulated.
System.Object:- This is the root class for all the objects in the .NET framework,
from which all the other classes are derived. It contains some basic methods and
properties, which can be accessed from all the object instances of the .NET
framework.
Look into the code which calls system.object methods.
Dim Obj As New System.Object()
Obj.ToString()
Obj.GetHashCode()
Obj.GetType()
Dim objEmployee As New Employee()
objEmployee.ToString()
objEmployee.GetHashCode()
objEmployee.Get

No comments:

Post a Comment