Calling abstract class protected methods and properties

I have a base class with various methods and computed properties:

BaseClass
Method1
Method2
ComputedProperty1
ComputedProperty2

And subclasses that define some of the methods that need to change for the subclass

SubClass
Method1 - get the BassClass result and do SubClass-specific stuff with it

But other BaseClass methods, Method2 as an example, are not specific to any subclass. Assuming I want to make all parts of BaseClass protected, is my only resort to call all the BaseClass methods and properties in each SubClass:

SubClass
Method1 - get the BassClass result and do SubClass-specific stuff with it
Method2 - just return the BassClass result
ComputedProperty1 - just return the BaseClass result
ComputedProperty2 - just return the BaseClass result

Or is there some other nifty way to access protected methods and properties in a subclass without having to define each of them in each subclass?

Yes. If you want them to be protected in the superclass, then they’d have to be overridden with public methods in each subclass.

Note that properties behave differently from methods in that they cannot be overridden by a subclass at all.

Well while grumbling to myself about all the typing I’d be doing, I thought do I even need to protect all the base methods and properties? If I just protect the constructors, there wouldn’t be instances of the base out there to run the methods on. Or at least that’s what I’m telling myself right now.

This is the way I do it. If they can’t construct it, they can’t access the methods in the first place.

2 Likes