Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple derived abstract classes?

I have to create a Course Management System with course categories:

Cooking, sewing and writing courses

cooking and writing each have 2 courses (Italian, seafood, creative write and business write). This creates derived abstract:

abstract course -> abstract cooking -> concrete seafood

The abstract cooking and writing have common fields and some common methods, however they also have abstract methods that are abstract in the base class.

Can this be done in C#? If I make the derived abstract class methods abstract Visual Studio says they hide the base class abstract and then the concrete class methods have errors saying the base class must be abstract (it is but must not register). I have looked for an answer. I know single inheritance is used in C# but inheritance carries down the chain. What is the best answer?

Here is a code snippet - I hope it clarifies the problem:

public abstract class Course
{
    public abstract void AddStudent(StudentName sn, int f);
    public abstract decimal CalculateIncome();
}

public abstract class WritingCourse : Course
{
    public void AddStudent(StudentName sn, int f)
    {
     //Add student
    }
    public abstract decimal CalculateIncome(); // can only be claculated in concrete
}

public class BusinessWritCourse : WritingCourse
{
    public void AddStudent(StudentName sn, int f):
    base(sn, false){}

    public decimal CalculateIncome()
    {
       return //do stuff
    }
}

public class SewingCourse : Course
{
    public override void AddStudent(StudentName sn, int f)
    {
        //do stuff
    }

    public override decimal CalculateIncome()
    {
       return //do stuff
    }
}
like image 706
Julesyw Avatar asked Jun 30 '11 06:06

Julesyw


People also ask

Can we have multiple abstract class?

A class can inherit from multiple abstract classes.

Can abstract class have multiple abstract methods?

An abstract class in Java is one that is declared with the abstract keyword. It may have both abstract and non-abstract methods(methods with bodies).

How many abstract classes can be used in multilevel?

How many abstract classes can be used in multilevel inheritance? Explanation: At least one class must implement all the undefined functions. Hence there must be at least one class which is not abstract. That is at least one less than number of levels.

Can we extend multiple abstract classes?

Another key difference is that classes can implement more than one interface, but they can extend only one abstract class. This is a design decision based on the fact that multiple inheritance (extending more than one class) can cause code deadlocks.


2 Answers

if I make the derived abstract class methods abstract Visual Studio says they hide the base class abstract and then the concrete class methods have errors saying the base class must be abstract (it is but must not register)

If you have base class 'A' which has an abstract method 'b()' then you don't have to declare 'b()' again as abstract in B : A, to have C : B override it, just don't use it. Even if you override the 'b()' method in class B you can again override it in class 'c()' (and even use base.(); to execute B's implementation.

Some code:

public abstract class A{
    public abstract void b();
}

public class B : A{
    public override void b() { //do stuff }; //overrides b from a
    public virtual void c() { //do stuff }; //creates an implemented method c in B that can be overriden by childs.
    public void d() { //do stuff};
}

public class C : B{
    public override void b() { //do stuff}; //overrides b from A, works!
    public override void c() {//do stuff}; //overrides c from B, works!
    public override void d() {//do stuff}; //doesn't work since d from B isn't abstract or virtual (hides it)
    public new void d() {//do stuff}; //works, hides d, but when you use inheritance this method will not be called, instead B's d() method will be called, only if you see C as  the specific class C this will work
}

Also to clarify: methods declared abstract must be overriden (just like in an interface, and only by the direct child of the class declaring the abstract method). Methods declared virtual can be overriden, but don't have to be.

like image 63
Roy T. Avatar answered Sep 20 '22 12:09

Roy T.


I think this kind of problems is better to resolve using interfaces and not abstract classes: Ex:

//
interface IInterface1
{
    void SameMethod();
}

interface IInterface2
{
    void SameMethod();
}


class TestClass : IInterface1, IInterface2
{
    void IInterface1.SameMethod()
    {
        // do one thing for Interface 1
    }

    void IInterface2.SameMethod()
    {
        // do something elsefor Interface 2
    }
}

class Program
{
    static void Main(string[] args)
    {
        TestClass test = new TestClass();

        IInterface1 i1 = test;
        i1.SameMethod(); // will call IInterface1.SameMethod()

        IInterface2 i2 = test;
        i2.SameMethod(); // will call IInterface2.SameMethod()

    }
}
like image 27
Sasha Avatar answered Sep 19 '22 12:09

Sasha