There is a lot to understand about OO Inheritance. Last time we just scratched the surface. Today we’ll stick with inheritance and take a closer look at the protected access modifier. This access modifier is all about inheritance. protected is not needed for anything else.
Unlike constructs marked with the public access modifier, callers cannot see protected constants, methods, constructors, properties & member variables. Anything marked as protected is only accessible within the class itself and child classes.
public class Dog { protected virtual string BarkingSound => "Woof! Woof!"; public virtual void Bark() { Console.WriteLine(BarkingSound); } }
The new BarkingSound property on Dog is virtual so we can override it in subclasses like Chihuahua:
public class Chihuahua : Dog { protected override string BarkingSound => "Yap! Yap!"; public override void Bark() { Console.WriteLine(BarkingSound); } }
The output on the console for
new Dog().Bark(); new Chihuahua().Bark();
remains unchanged:
“Woof! Woof!”
“Yap! Yap!”
Property BarkingSound is accessible to Dog and Chihuahua but not outside of this inheritance relationship.
Notice how my IDE (Visual Studio) doesn’t show BarkingSound as an option when I am trying to use the dot operator (.) on an instance of Dog:
The protected access modifier is a great way to share construction, data, and behaviour only between classes in an inheritance relationship.
Which constructs can we declare as protected? Anything that lives at the class level:
Constants
Class members
Constructors
Properties
Methods
We’ll dig into some of these more as we get deeper into the workings of inheritance.
I’ve kept the best for last. Sharing data and behaviour between classes in an inheritance relationship via the protected access modifier often allows us to radically simplify child and parent classes.
Let’s return to our Dog/Chihuahua example. Notice how the Bark() method looks identical between the Dog base class and the Chihuahua child class. The only thing that varies is the BarkingSound property.
Knowing that child classes inherit public methods from their parents, we now remove method Bark() from Chihuahua. At the same time, Dog.Bark() no longer needs to be overridable – let’s delete the virtual keyword:
public class Dog { protected virtual string BarkingSound => "Woof! Woof!"; public void Bark() { Console.WriteLine(BarkingSound); } } public class Chihuahua : Dog { protected override string BarkingSound => "Yap! Yap!"; }
Now, when we call Bark() on an instance of Dog and Chihuahua respectively, we still get
“Woof! Woof!”
“Yap! Yap!”
How can this be?
Whether we have an instance of Chihuahua or Dog, we call Dog.Bark() all the time. However, Dog.Bark() will switch in the BarkingSound of the given instance, either Dog or Chihuahua. Pretty cool. With this latest version of the code, we’ve managed to minimise duplication – Dog only has two lines of code and Chihuahua only one!
Use the protected access modifier when you want to share data and behaviour exclusively between classes in an inheritance hierarchy.
The protected Access Modifier
/by Olaf ThielkeThe
protected
Access ModifierPhoto by Shripal Daphtary on Unsplash
There is a lot to understand about OO Inheritance. Last time we just scratched the surface. Today we’ll stick with inheritance and take a closer look at the
protected
access modifier. This access modifier is all about inheritance.protected
is not needed for anything else.Unlike constructs marked with the
public
access modifier, callers cannot seeprotected
constants, methods, constructors, properties & member variables. Anything marked asprotected
is only accessible within the class itself and child classes.An example is in order. Let’s go back to our previous
Dog
/Chihuahua
inheritance example and extract the barking sound string into a protected read-only property:The new BarkingSound property on Dog is virtual so we can override it in subclasses like Chihuahua:
The output on the console for
remains unchanged:
“Woof! Woof!”
“Yap! Yap!”
Property
BarkingSound
is accessible toDog
andChihuahua
but not outside of this inheritance relationship.Notice how my IDE (Visual Studio) doesn’t show
BarkingSound
as an option when I am trying to use the dot operator (.
) on an instance ofDog
:The protected access modifier is a great way to share construction, data, and behaviour only between classes in an inheritance relationship.
Which constructs can we declare as
protected
? Anything that lives at the class level:We’ll dig into some of these more as we get deeper into the workings of inheritance.
I’ve kept the best for last. Sharing data and behaviour between classes in an inheritance relationship via the
protected
access modifier often allows us to radically simplify child and parent classes.Let’s return to our
Dog
/Chihuahua
example. Notice how theBark()
method looks identical between theDog
base class and theChihuahua
child class. The only thing that varies is theBarkingSound
property.Knowing that child classes inherit
public
methods from their parents, we now remove methodBark()
fromChihuahua
. At the same time,Dog.Bark()
no longer needs to be overridable – let’s delete thevirtual
keyword:Now, when we call
Bark()
on an instance ofDog
andChihuahua
respectively, we still get“Woof! Woof!”
“Yap! Yap!”
How can this be?
Whether we have an instance of
Chihuahua
orDog
, we callDog.Bark()
all the time. However,Dog.Bark()
will switch in theBarkingSound
of the given instance, eitherDog
orChihuahua
. Pretty cool. With this latest version of the code, we’ve managed to minimise duplication –Dog
only has two lines of code andChihuahua
only one!Use the
protected
access modifier when you want to share data and behaviour exclusively between classes in an inheritance hierarchy.