Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello I am not completely new to c#, but I still have some problems with overriding methods and properties. Well I would like to know how you override them correctly. Let's look at this example:

C#
public abstract class BaseClass {
   private string description;
   public virtual string Description { get.... }
   .......
   .......
}

public class SubClass : BaseClass {
   .......
   .......
   public override string Description {....}
   .......
}

public class SecondSubClass : SubClass {
   .......
   .......
   ???????
}


My question is now: I know that you need to override all members of an abstract class and if you miss one then you need to declare that class as abstract as well, but what about the SecondCubClass? Is the rule to include all members of the abstract class only valid for the direct subclass of that abstract class? I thought maybe I don't need to include the property in the SecondSubClass because I already implemented it in the SubClass and therefore can use it in that class because it is a subclass of the subclass. But I would be thankful for a clear answer because I did not find any clear statement on this online.

Thank you!
Posted
Updated 26-Jul-15 7:53am
v2
Comments
Richard MacCutchan 26-Jul-15 12:52pm    
The second class inherits all the public and protected members of SubClass. So it only needs to override the ones that it wants to modify.

1 solution

First of all, the rule to have all abstract members overridden any any non-abstract class is not just about direct derived class, this is about all the derived classes. But you are right: once a member is implemented and made non-abstract, you can inherit it in next level of hierarchy instead of overriding. It depends on your purpose.

You will easily understand this rule if you understand its purpose. Imagine you managed to create a non-abstract class with at least one of the virtual members not overridden (not overridden anywhere in the part of hierarchy up from your class in question), you will be able to instantiate the class. And you would end up with the instance of the class having access to a virtual abstract member, and you would be able to call this member… with fatal results.

But your problem is different: in your code sample, you don't have any abstract members. All of the about would be valid only if you marked Desription as abstract. Without it, you are allowed to use BaseClass.Description without overriding it. Your code sample is just inadequate to your question. Also, your code sample does not show how description is initialized and possibly modified; this is not a mistake, because it could be under "…", but it's an important detail.

—SA
 
Share this answer
 
Comments
Bollinez 2-Aug-15 16:01pm    
Thank you! Your solution helped me a lot!
Sergey Alexandrovich Kryukov 2-Aug-15 16:24pm    
You are very welcome.
Good luck, call again.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900