Inheritance: Contents Are Just Points. They Will Act As Guidelines To Expand While Writing Answers
Inheritance: Contents Are Just Points. They Will Act As Guidelines To Expand While Writing Answers
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
• Display
• CalculateSalary
Constructor
class Employee
{
public Employee()
{
id = 0;
name = "";
salary = 0.0;
EmployeeCount += 1;
public int Id
{
get { return id; }
set { id = value; }
//Static method
public static void ShowCount()
{
Console.WriteLine("\nTotal number of Employees = " + EmployeeCount);
}
//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;
this.allowance = all;
}
*/
}
}
Ability of different objects to respond to the same message in different ways is called
Polymorphism.
•
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.
}
class Program
{
static void Main(string[] args)
{
/*Client code for testing diffrent shape objects*/
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
{
//other code
//overriding interface method
#region IPrintable Members
class Date
{
//other code
public void Print()
{
Console.WriteLine(this);
}
#endregion
}
}
– Type of method
– 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.