Explain about abstract property. Give an example?

 Posted by Tripati_tutu on 11/15/2010 | Category: C# Interview questions | Views: 4200 | Points: 40
Answer:

A property with abstract keyword is considered as abstract property. An abstract property cannot have any implementation in the class. In derived classes you must have to write their own implementation.

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;

abstract class Employee
{
private string fName = string.Empty;
private string lName = string.Empty;

public string FirstName
{
get
{
return fName;
}
set
{
fName = value;
}
}

public string LastName
{
get
{
return lName;
}
set
{
lName = value;
}
}
}

// FullName is abstract

public abstract string FullName
{
get;
}
}

class Company: Employee
{
// Overiding the FullName abstract property derived from employee class
public override string FullName
{
get
{
return "Mr. " + FirstName + " " + LastName;
}
}
}

class Main
{
public static void Main()
{
Company CompanyObj = new Company();
CompanyObj.FirstName = "Satish";
CompanyObj.LastName = "Reddy";
Console.WriteLine("Employee Full Name is : " + CompanyObj.FullName);
}
}


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response