Resolved inheritance method question

John007

New member
Joined
Jan 19, 2021
Messages
2
Programming Experience
5-10
Hi

I have two classes ClassA and ClassB. ClassB is inherited from ClassA. I want the Sub1 in ClassA to handle some situations but for others I want to leave it for Sub2 in ClassB to handle. I am not sure how to set this up. My skeleton code is below but its obviously not correct. Any help would be appreciated.

Thanks

Regards

C#:
    public class ClassA
    {
        public string xyz = "abc";
        void Sub0()
        {
            Sub1(xyz);
        }

        public void Sub1(string xyz)
        {
            if (true)
            {
                //Handle it here if condition is true else leave it for Sub2 in ClassB to handle
            }
            else
            {
                //Let it handled in Sub2 in ClassB
                Sub2(xyz);
            }
        }

        public abstract void Sub2(string xyz);
        // I am getting this on above line; 'ClassA.Sub2(string)' is abstract but it is contained in non-abstract type 'ClassA'
        // How can I fix it? I don't want to make ClassA abstract
    }

    class ClassB : ClassA
    {
        public override void Sub2(string xyz)
        {
            // Handle it here
        }
    }
 
Last edited:
Solution
So, do you want to instantiate the base class directly or not? If so, do this:
C#:
public class BaseClass
{
    public void Method1()
    {
        if (someBooleanExpression)
        {
            // ...
        }
        else
        {
            Method2();
        }
    }

    public virtual void Method2()
    {
        throw new InvalidOperationException("This method should be invoked on derived classes only.");
    }
}

public class DerivedClass : BaseClass
{
    public override void Method2()
    {
        // ...
    }
}
If not, do this:
C#:
public abstract class BaseClass
{
    public void Method1()
    {
        if (someBooleanExpression)
        {
            // ...
        }
        else
        {
            Method2()...
ClassB is inherited from ClassA.
No it's not. That terminology is wrong. ClassB inherits ClassA and ClassB is derived from ClassA. A member declared in ClassA and not overridden in ClassB is inherited from ClassA.
 
An abstract method is one that you declare but don't implement. Because an abstract member is not implemented, you cannot create an instance of that class directly. For that reason, the class must be declared abstract as well. If you want to be able to instantiate the base class directly then it cannot be declared abstract which means that the method cannot be declared abstract either. In that case, you'd have to declare it virtual and provide some sort of default implementation. If that method is not supposed to be called on the base class then that implementation should throw an InvalidOperationException.
 
So, do you want to instantiate the base class directly or not? If so, do this:
C#:
public class BaseClass
{
    public void Method1()
    {
        if (someBooleanExpression)
        {
            // ...
        }
        else
        {
            Method2();
        }
    }

    public virtual void Method2()
    {
        throw new InvalidOperationException("This method should be invoked on derived classes only.");
    }
}

public class DerivedClass : BaseClass
{
    public override void Method2()
    {
        // ...
    }
}
If not, do this:
C#:
public abstract class BaseClass
{
    public void Method1()
    {
        if (someBooleanExpression)
        {
            // ...
        }
        else
        {
            Method2();
        }
    }

    public abstract void Method2();
}

public class DerivedClass : BaseClass
{
    public override void Method2()
    {
        // ...
    }
}
 
Solution
Back
Top Bottom