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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Abstract methods in abstract classes

Status
Not open for further replies.

0ddball

Technical User
Nov 6, 2003
208
0
0
GB
Please consider the following:

Code:
public abstract class BaseClass
{
	public abstract void SomeMethod();
}

public abstract class MidlevelClass : BaseClass
{
	// Wants me to implement SomeMethod();
	// I don't WANT to implement some method!
}

public abstract class ToplevelClass : MidlevelClass
{
	public override void SomeMethod()
	{
		// Better!
	}
}

Am I going to have a problem? Can I skip an inheretance generation when both the base and the derived types are abstract, passing responsibility for implementing the base classes functionality further up the inheretance ladder?

Am I making sense?


Yet another unchecked rambling brought to you by:
Oddball
 
Ok - I've messed up here... let me add some more code and I'll explain my difficulty:

Code:
public abstract class BfiDataEntity<T>
	where T : class, new()
{
	public abstract void SomeMethod();
}

public abstract class Contact : BeIntegrated.Model.Data.BfiDataEntity<Contact>
{
	// My problem occurs here!

	public abstract override void SomeMethod();
}

public class Person : Contact
{
	public override void SomeMethod()
	{
	}
}

Everything is fine with the code - the real problem is with the generics. I've got a very strange set-up going here that is working just fine... well, it was. It's a complicated story to do with caching and replication. You don't want to know.

I can't decalare the parameterless constructor required by the new() constraint of the BfiDataEntity class because Contact is an abstract so doesn't have constructors.

If I make Contact a normal class, I can't decare abstract members and force child types to implement the relevent methods.

This is my problem in a nutshell. I feel I am rapidly loosing the shell and just becoming a nut.


Yet another unchecked rambling brought to you by:
Oddball
 
public abstract override void SomeMethod();

You can not use the modifier override in an abstract method declaration.

Marty
 
From the MS website:

Code:
// compile with: /target:library
public class D
{
    public virtual void DoWork(int i)
    {
        // Original implementation.
    }
}

public abstract class E : D
{
    public abstract override void DoWork(int i);
}

public class F : E
{
    public override void DoWork(int i)
    {
        // New implementation.
    }
}


Yet another unchecked rambling brought to you by:
Oddball
 
Sorry I was so brief Marty, I was rusing to get away from work.

Do you have any ideas how to get around my problem?

That page doesn't really help me with my generics problem.


Yet another unchecked rambling brought to you by:
Oddball
 
You should have a factory that creates an instance of your classes depending on which type you want returned.

Your factory should be well aware of the class type and set the properties specific to that class before returing it back the the caller.

So in this case, you can have a constructor with parameters and just pass them in from the factory.
 
Why not override the method as and when required by definintion but simply throw a NotSupported Exception in the method call of classes where it's, well, not supported?

Rhys
The use of COBOL cripples the mind; its teaching should, therefore, be regarded as a criminal offense Edsgar Dijkstra

Church of the Flying Spaghetti Monster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top