Abstract method :
1)An abstract function has no implementation. It can only be declared. This forces the derived class to provide the implementation of it.
2) Abstract means we MUST override it.
3) An abstract member is implicitly virtual. Abstract can be called as pure virtual in some of the languages.
Virtual method :
1) Virtual Function has an implementation. When we inherit the class we can override the virtual function and provide our own logic.
2) We can change the return type of Virtual function while implementing the function in the child class(which can be said as concept of Shadowing).
3) Virtual means we CAN override it.
Example -
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AbstractAndVirtualMethod
{
abstract class Employee
{
public string FName { get; set; }
public string LName { get; set; }
public string Address { get; set; }
public string getEmployeeName()
{
return LName + ", " + FName;
}
public abstract decimal getSalaryMonthly();
public virtual string showMessage()
{
return "In Base abstract class";
}
}
class PermanentEmployee : Employee
{
public decimal TotalPackage { get; set; }
public override decimal getSalaryMonthly()
{
return TotalPackage / 12;
}
}
class ContractEmployee : Employee
{
public int TotalHours { get; set; }
public int PayHourly { get; set; }
public override decimal getSalaryMonthly()
{
return TotalHours * PayHourly;
}
public override string showMessage()
{
return "In derived abstract class";
}
}
class Program
{
static void Main(string[] args)
{
PermanentEmployee permanent = new PermanentEmployee();
permanent.FName = "Ashish";
permanent.LName = "Verma";
permanent.Address = "New Delhi";
permanent.TotalPackage = 650000;
Console.WriteLine("Employee showMessage " + permanent.showMessage());
Console.WriteLine("Employee FullName " + permanent.getEmployeeName());
Console.WriteLine("Employee Monthly salary " + permanent.getSalaryMonthly());
//Console.ReadLine();
ContractEmployee contract = new ContractEmployee();
contract.FName = "Ashish";
contract.LName = "Verma";
contract.Address = "New Delhi";
contract.PayHourly = 100;
contract.TotalHours = 30;
Console.WriteLine("Contract Employee showMessage " + contract.showMessage());
Console.WriteLine("Contract Employee FullName " + contract.getEmployeeName());
Console.WriteLine("Contract Employee Monthly salary " + contract.getSalaryMonthly());
Console.ReadLine();
}
}
}
1)An abstract function has no implementation. It can only be declared. This forces the derived class to provide the implementation of it.
2) Abstract means we MUST override it.
3) An abstract member is implicitly virtual. Abstract can be called as pure virtual in some of the languages.
Virtual method :
1) Virtual Function has an implementation. When we inherit the class we can override the virtual function and provide our own logic.
2) We can change the return type of Virtual function while implementing the function in the child class(which can be said as concept of Shadowing).
3) Virtual means we CAN override it.
Example -
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AbstractAndVirtualMethod
{
abstract class Employee
{
public string FName { get; set; }
public string LName { get; set; }
public string Address { get; set; }
public string getEmployeeName()
{
return LName + ", " + FName;
}
public abstract decimal getSalaryMonthly();
public virtual string showMessage()
{
return "In Base abstract class";
}
}
class PermanentEmployee : Employee
{
public decimal TotalPackage { get; set; }
public override decimal getSalaryMonthly()
{
return TotalPackage / 12;
}
}
class ContractEmployee : Employee
{
public int TotalHours { get; set; }
public int PayHourly { get; set; }
public override decimal getSalaryMonthly()
{
return TotalHours * PayHourly;
}
public override string showMessage()
{
return "In derived abstract class";
}
}
class Program
{
static void Main(string[] args)
{
PermanentEmployee permanent = new PermanentEmployee();
permanent.FName = "Ashish";
permanent.LName = "Verma";
permanent.Address = "New Delhi";
permanent.TotalPackage = 650000;
Console.WriteLine("Employee showMessage " + permanent.showMessage());
Console.WriteLine("Employee FullName " + permanent.getEmployeeName());
Console.WriteLine("Employee Monthly salary " + permanent.getSalaryMonthly());
//Console.ReadLine();
ContractEmployee contract = new ContractEmployee();
contract.FName = "Ashish";
contract.LName = "Verma";
contract.Address = "New Delhi";
contract.PayHourly = 100;
contract.TotalHours = 30;
Console.WriteLine("Contract Employee showMessage " + contract.showMessage());
Console.WriteLine("Contract Employee FullName " + contract.getEmployeeName());
Console.WriteLine("Contract Employee Monthly salary " + contract.getSalaryMonthly());
Console.ReadLine();
}
}
}
No comments:
Post a Comment