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

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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.