Reputation: 2059
I have the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
abstract class parent
{
public abstract void printFirstName();
protected virtual void printLastName()
{
Console.WriteLine("Watson");
}
protected void printMiddlename()
{
Console.WriteLine("Jane");
}
}
class child: parent
{
public override void printFirstName()
{
Console.WriteLine("Mary");
}
protected override void printLastName()
{
Console.WriteLine("Parker");
}
public void getMiddleName()
{
printMiddlename();
}
}
class Program: child
{
static void Main(string[] args)
{
child ch = new child();
ch.printFirstName();
ch.getMiddleName();
//ch.printLastName();
Console.Read();
}
}
}
This code runs properly and prints Mary Jane
However, when I uncomment ch.printLastName();
it showscompile error:
Why cant my Program Class call protected method of Child Class? especially when The child class has no problem calling the protected method (printMiddleName)
of Parent class?
Upvotes: 0
Views: 4811
Reputation: 43876
I guess you are confusing inheritance and access levels.
Your Program
class inherits the printFirstName
method from child
. So inside your Program
class you can access that method:
class Program : child
{
void Method() { this.printFirstName(); }
}
From outside a class you cannot access protected
methods. But from inside a class you can access the protected
methods of instance the same type:
class Program : child
{
void Method()
{
Program p1 = new Program();
p1.printFirstName(); // this works
child c1 = new child();
p1.printFirstName(); // this gives your compiler error
}
But you cannot access a protected
method of an instance of a different type, even if it is a type you derived from.
See C# Reference for more details.
Upvotes: 4
Reputation: 415
In layman's terms the method marked with protected is meaning that the class itself (child) can only access the method or other class inheriting from it.
Program calling the method off 'ch' can not access it, but an instance of Program (as its inheriting child) can call the method.
Upvotes: 0
Reputation: 4869
Your code doesn't match your question - you're asking why can't Program
class call protected method of it's parent (that is, child
class in your example) - but your code is showing an instance of child
class trying to publicly access a protected method - which fails, as intended.
This would work:
printLastName();
Or:
new Program().printLastName();
Upvotes: 0
Reputation: 158
Protected keyword means that only a type and types that derive from that type can access the member. so in this scenario you can't accesses Child.printLastName() from program Because it have two levels
Parent.printLastName() -> protected Child.printLastName() -> protected
How inheritance work when you call Child.printLastName() from program class it calls Parent.printLastName() -> Child.printLastName() But parent is not accessible that's the region it is showing compilation error.
Solution :-
You can make
Parent.printLastName() -> Internal access modifier
so Parent.printLastName() is accessible in this assembly .
namespace ConsoleApplication2 { abstract class parent { public abstract void printFirstName();
internal virtual void printLastName()
{
Console.WriteLine("Watson");
}
public void printMiddlename()
{
Console.WriteLine("Jane");
}
}
class child : parent
{
public override void printFirstName()
{
Console.WriteLine("Mary");
}
protected override void printLastName()
{
Console.WriteLine("Parker");
}
public void getMiddleName()
{
printMiddlename();
}
}
class Program : child
{
static void Main(string[] args)
{
child ch = new child();
ch.printFirstName();
ch.getMiddleName();
ch.printLastName();
Console.Read();
}
}
}
Upvotes: 1
Reputation: 401
printLastName is protected. Check the MSDN page about access modifiers:
protected
The type or member can be accessed only by code in the same class or struct, or in a class that is derived from that class.
Upvotes: 1
Reputation: 2604
C# specification Section 1.6.2 Accessibility
Each member of a class has an associated accessibility, which controls the regions of program text that are able to access the member
public - Access not limited
protected - Access limited to this class or classes derived from this class
Protected members are accessible only in current class (where it is defined) and classes derived from it.
In another word, you can access it only by this
.
Upvotes: 2