You are on page 1of 8

BCA .

NET

Note: Below contents are from slides and there are only code snippets given for just
reference. You need to consider your implementation as full code.

Contents are just points. They will act as guidelines to expand while writing answers.

Inheritance
• Major feature of OOPs
• Hierarchical Structure
• What is the advantage of Inheritance in code?
– Reusability
– Extensibility
<expand the points>

Employee Class
Data Members
• empId - employee Id
• firstName - First name
• lastName - Last Name
• salary - monthly salary
• pf - provident fund
• Total salary - total salary
Methods
• Constructors
• Accept
• Display
• CalculateSalary
Manager Class

Data Members
• incentives - incentives
• Bonus - bonus
Methods
• Constructors
• Accept

Shilpa Mahangade Page 1


BCA .NET

• Display
• CalculateSalary

Constructor

• What is the purpose of constructor?

• What if constructor is not written in base class?

• What is the order of constructor call in case of inheritance?

class Employee
{

private int id;


private String name;
private double salary;
private static int EmployeeCount = 0;//static member variable

public Employee()
{
id = 0;
name = "";
salary = 0.0;
EmployeeCount += 1;

public Employee(int id, String name,double salary, Date JoiningDt, Department


department)
{
this.id = id;
this.name = name;
this.salary = salary;
EmployeeCount += 1;
}

public int Id
{
get { return id; }
set { id = value; }

Shilpa Mahangade Page 2


BCA .NET

public String Name


{
get { return name; }
set { name = value; }
}

public double Salary


{
get { return salary; }
set { salary = value; }
}

//Static method
public static void ShowCount()
{
Console.WriteLine("\nTotal number of Employees = " + EmployeeCount);
}

// String representation of an object


public override string ToString()
{
return "Employee Id = " + Id + "\nEmployee Name = " + Name + "Basic Salary =
"+Salary;
}

//Salary calculation
public virtual double CalculateSalary()
{
return Salary;
}
}
//Manger class Class inherited from Employee Class
class Manager:Employee
{
private double allowance;
private double TotalSalary=0.0;

//Calling base class No parameter Constructor


public Manager():base()
{
allowance = 0.0;
}

Shilpa Mahangade Page 3


BCA .NET

//Calling base class's parameterized Constructor


//and passing parameters of base class(Employee)
//Using Base class Constructor

public Manager(int id, String name, double salary, double all)


:base(id, name, salary)
{

this.allowance = all;
}

public double Allowance


{
get { return allowance; }
set { allowance = value; }
}
//Ovrriding CalculateSalary() method of base class(Enployee)
public override double CalculateSalary()
{
TotalSalary=base.Salary + Allowance;
return TotalSalary;
}

//Overriding ToString Method for string representation of an object


public override string ToString()
{
return base.ToString()+"Allowance = " + allowance + "Total Salary = "+ TotalSalary;
}
}
class Program
{
static void Main(string[] args)
{
/*Client code for testing objects of diffrent hirerachy*/

/*Manager manager = new Manager(102, "Rajiv", 20000, 2500);


Console.WriteLine(manager);
*/

/* dynamic data type governs method selection


Employee emp = new Manager(102, "Rajiv", new Date(25, 10, 2011), Department.HR,
20000, 5000, 2500);
Console.WriteLine("Salary : " + emp.CalculateSalary());

Shilpa Mahangade Page 4


BCA .NET

*/

}
}

Polymorphism (Late Binding)

Ability of different objects to respond to the same message in different ways is called
Polymorphism.

• Binding is an association of function calls to an object.


• Types
– Compile-time binding
• Simple method call using object
• Method Overloading
– Run-time binding or Late Binding or Dynamic Binding
• Late binding is achieved using virtual methods
• By overriding the virtual methods in derived class
• Polymorphism is achieved using virtual functions and inheritance.
• Dynamic data type governs method selection.

<example: in above code snippet>

<Difference between Method Overloading and Overriding>

Methods of Object Class


Super class
1. ToString()
2. Finalize()
3. Equals()
4. Clone()
5. GetHashCode()
6. GetType()
7. MemberwiseClone()
Abstract Class

• Abstract class cannot be initiated. It contains generic features of all derived classes.

Shilpa Mahangade Page 5


BCA .NET

– Can contain both abstract and non-abstract methods


– Abstract methods to be overridden by derived classes
//Declaring class as an abstract class
abstract class Shape
{
//Abstract method
private double area;
public abstract void CalculateArea();

public double Area


{
get { return area; }
set { area = value; }
}
}
class Triangle:Shape
{
//other code
public override void CalculateArea()
{
//using base class property Area
base.Area = 0.5 * TBase * THeight;
Console.WriteLine("Area of Triangle having Base = {0} and Height = {1} is {2}", T_Base,
T_Height, Area);
}

}
class Program
{
static void Main(string[] args)
{
/*Client code for testing diffrent shape objects*/

/*Object of an abstract class can not be created*/

//Shape s = new Shape();

/*Objects of concrete classes which is derived from


* Abstract class can be created
*
* uncomment the code below
*/

Shilpa Mahangade Page 6


BCA .NET

//Triangle triangle = new Triangle(200.00, 50.00);


//triangle.CalculateArea();

//Square square = new Square(120.00);


//square.CalculateArea();

Console.ReadLine();
}
}

Interface


Role based inheritance

Loosely coupled applications
– Common design across classes
Some rules

• Reference type
• Cannot contain data members
• No access specifiers
• Provider class to provide implementation for all the members
• Provider class can implement multiple interfaces
• But Provider class can inherit ONLY ONE CLASS
• Defined once and no variations to be done thereafter

namespace Interface_Demo
{
//declaring interface
interface IPrintable
{
void Print();
}

class Employee

Shilpa Mahangade Page 7


BCA .NET

{
//other code
//overriding interface method
#region IPrintable Members

public void Print()


{
Console.WriteLine(this); ;
}
}

class Date
{
//other code
public void Print()
{
Console.WriteLine(this);
}

#endregion
}
}

<Diff. between abstract classes and interfaces>

• What are the differences?

– Type of method

– Best suited for

– Mutable or immutable

Note:

These are just guidelines. You need to expand them more for answers according to marks.
You may prepare other examples for each topics for writing in exam.

Shilpa Mahangade Page 8

You might also like