Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OO Question

Status
Not open for further replies.

Smeat

Programmer
Mar 27, 2004
193
GB
Hi All

I have a protected class in which I want to declare a method as must override. I thought I could simply declare that method as abstract such as
Code:
protected abstract Table GetMainContent();
but that gives the following compiler error:

Code:
MasterTemplate.GetMainContent() is abstract but it is contained in nonabstract class 'BaseClasses.MasterTemplate'

Can anyone tell me how I can declare my method as one that must be overridden in the inheriting class?

TIA

Smeat
 
You hava to declare the class as abstract, like this
Code:
public abstract SomeClass {
...
}
 
whoops...
I meant
Code:
public abstract class SomeClass {
...
}
Sorry.
 
Hi KLP

Thanks for rsponding.

Unfortunately, your suggestion won't work in this instance as my class needs to be scoped as protected. The reason for this is that most of the functionallity is provided by my base class.

What I need to do is ensure that any class that derives from my base class implements the GetMainContent() method.

It seems that you have to scope the whole class as abstract but I find this hard to believe as what I need to do must be fairly common practice.

Any comments appreciated.

Smeat
 
There is no reason why abstract class couldn't be protected...
protected is an access modifier - a protected field (or class) can be accessed only by containing class or its descendants.
abstract means that class cannot have any instances (because it constains abstract methods - methods which have no implementation). Descendants of an abstract class must override all abstract methods (or be declared as abstract).
You can declare class/field/method as protected abstract
 
A member function without an implementation must be abstract and a class containing an abstract member function should be abstract as well. So you have to look around!

public class ClassParent
{
public virtual Table GetMainContent()
{
throw new CustomerException("Must override this method or get a constant exception");

}
}

public class ClassChild : ClassParent
{
public override Table GetMainContent()
{
System.Console.WriteLine("Blackmail implementation");
}

}

I haven't tried it out but it should work. I would suggest that you stick to declaring your base class as abstract... this way all child classes will have to implement the method. You might have come accross the access modifier "MustOverride" in VB.Net. I'm not sure if this keywork is available in C# .Net.
 
Thanks guys

Declaring the class as abstract gave the required result.

Ta

Smeat
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top