Abstract property

Rajamani
Posted by Rajamani under C# category on | Points: 40 | Views : 1198
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Abstactprogram
{
public abstract class Program
{
public Program()
{
}
private int age = 0;
// Normal property
public int Age_Normal
{
get
{
return age;
}
set
{
age = value;
}
}
// Abstract property
public abstract string Address_Abstract
{
get;
set;
}
// Normal Methods
public string GetName(string firstName, string lastName)
{
return "My Name is : " + GetName_Virtual(firstName, lastName);
}
public int Divide_NotAbstract(int a, int b)
{
return a / b;
}
// Abstract Methods
public abstract int Add_Abstract(int a, int b);
public abstract int Subtract_Abstract(int a, int b);

// Virtual method
public virtual string GetName_Virtual(string firstName, string lastName)
{
return firstName + " " + lastName;
}
}
public class DerivedClass : Program
{
private string address = string.Empty;
public DerivedClass()
{
}
// override the abstract property
public override string Address_Abstract
{
get
{
return address;
}
set
{
address = value;
}
}
// override the abstract method
public override int Add_Abstract(int a, int b)
{
return a + b;
}
public override int Subtract_Abstract(int a, int b)
{
return a - b;
}
// override virtual method
public override string GetName_Virtual(string firstName, string lastName)
{
return "(Overriden) Name: " + firstName + " " + lastName;
}
// hide normal method of the abstract class
public new string Divide_NotAbstract(int a, int b)
{
int d = a / b;
return "The division is: " + d.ToString();
}
// use abstract property to retrieve its value
public string GetAddress()
{
return Address_Abstract;
}


static void Main(string[] args)
{
DerivedClass c = new DerivedClass();
Console.Write("<b>Abstract method - Sum: </b>" + c.Add_Abstract(50, 30).ToString() + "<br />");
Console.Write("<b>Abstract method - Subtract: </b>" + c.Subtract_Abstract(50, 30).ToString() + "<br />");
Console.Write("<b>Virtual Method - GetName_Virtual: </b>" + c.GetName_Virtual("SHEO", "NARAYAN") + "<br />");
Console.Write("<b>Normal Public Method - GetName: </b>" + c.GetName("SHEO", "NARAYAN") + "<br />");
Console.Write("<b>Normal Public Method being hidden using new keyword - Divide_NotAbstract: </b>" + c.Divide_NotAbstract(50, 30).ToString() + "<br />");
Program p = new DerivedClass();
Console.Write("<b>Normal Public Method from Abstract Class - Divide_NotAbstract: </b>" + p.Divide_NotAbstract(50, 30).ToString() + "<br />");
c.Address_Abstract = "Sheo Narayan, Hitec City, Hyderabad.";
Console.Write("<b>Normal Public method accessing <br />overriden Abstract Property - GetAddress: </b>" + c.GetAddress() + "<br />");
Console.ReadLine();
}
}
}

Comments or Responses

Login to post response