private and protected methods - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

private and protected methods

oludannyoludanny Posts: 1Member
Hello,


I am using Visual Studio 2010 Pro. Below is a simple inheritance code. I tried using private and protected keywords for my methods but I found out that I can't access them.

class Employee
{
private string initTitle;
private string initfName;
private string initlName;
private string initoName;
private string initSuffix;

public Employee()
{
initTitle = "";
initfName = "";
initlName = "";
initoName = "";
initSuffix = "";
}

public Employee(string title, string fName, string lName, string oName, string suffix)
{
initTitle = title;
initfName = fName;
initlName = lName;
initoName = oName;
initSuffix = suffix;
}

protected virtual void printName()
{
Console.WriteLine("Title: {0}", initTitle);
Console.WriteLine("First Name: {0}", initfName);
Console.WriteLine("Last Name: {0}", initlName);
Console.WriteLine("Other Name(s): {0}", initoName);
Console.WriteLine("Suffix: {0}", initSuffix);
}
}



class FullTimeEmployee : Employee
{

public FullTimeEmployee()
{
}

public FullTimeEmployee(string title, string fName, string lName, string oName, string suffix)
: base(title, fName, lName, oName, suffix)
{
}

private void printEmpType()
{
Console.WriteLine("Full-Time Employee");
}
}


class CasualEmployee : Employee
{

public CasualEmployee()
{
}

public CasualEmployee(string title, string fName, string lName, string oName, string suffix)
: base(title, fName, lName, oName, suffix)
{
}

private void printEmpType()
{
Console.WriteLine("Casual Employee");
}
}


class Program
{
static void Run()
{
FullTimeEmployee fTEmp = new FullTimeEmployee("Mr.", "Ade", "Osun", "Kunle", "O");
fTEmp.printName();
fTEmp.printEmpType();
Console.WriteLine();
CasualEmployee cEmp = new CasualEmployee("Mr.", "Jamie", "Lee", "Sung", "K");
cEmp.printName();
cEmp.printEmpType();
Employee emp = new Employee("Mr.", "David", "Banner", "Bush", "K");
emp.printEmpType();
}
static void Main(string[] args)
{
Run();
}
}

I read that base class and derived classes can accesses protected fields and methods but mine is an exception. Even the base class that owns it could not access its own method.

I also made some methods private and the instance of its class can't access it! Please help out.
Sign In or Register to comment.