Several derived abstract classes?

I need to create a course management system with course categories:

Cooking, sewing and writing courses 

cooking and writing each has 2 courses (Italian, seafood, creative writing and business writing). This creates a derivative abstract:

 abstract course -> abstract cooking -> concrete seafood 

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 create abstract methods of abstract classes, then Visual Studio says that they hide the abstract paragraph of the base class, and then the concrete methods of the class have errors saying that the base class must be abstract (it should not be registered). I was looking for an answer. I know that one single inheritance is used in C #, but inheritance carries the chain. What is the best answer?

Here is a snippet of code - 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 } } 
+7
source share
4 answers

if I make a derived abstract class Visual Studio methods they hide the abstract abstract class and then specific error class methods that indicate that the base class must be abstract (it should not be registered)

If you have a base class "A" that has the abstract method "b ()", you do not need to declare "b ()" again as abstract in B: A, so that C: B will override it, just don't use it. Even if you override the method "b ()" in class B, you can again override it in class "c ()" (and even use base. () To execute the implementation of B.

Some codes:

 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 d() method will be called, only if you see C as the specific class C this will work } 

Also clarify: the methods declared by the abstract should be redefined (both in the interface and only by the direct descendant of the class declaring the abstract method). Methods declared virtual may be overridden, but not required.

+7
source

I think it’s better to solve such problems using interfaces, rather than abstract classes: Example:

 // 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() } } 
+4
source
 public class StudentName { } public abstract class Course { public abstract void AddStudent(StudentName sn, int f); public abstract decimal CalculateIncome(); } public abstract class WritingCourse : Course { override public void AddStudent(StudentName sn, int f) { //Add student } override public abstract decimal CalculateIncome(); // can only be claculated in concrete } public class BusinessWritCourse : WritingCourse { override public void AddStudent(StudentName sn, int f) { base.AddStudent(sn, 0); } override public decimal CalculateIncome() { return (decimal)3.4;//do stuff } } public class SewingCourse : Course { public override void AddStudent(StudentName sn, int f) { //do stuff } public override decimal CalculateIncome() { return (decimal)10; //do stuff } } class Program { static void Main(string[] args) { } } 
+1
source

If I understood correctly, I think you want to use the keyword "virtual" for abstract methods that you want to override?

If you are talking about an error that says something like β€œsome method hides an inherited element, add a new keyword if you had to hide it”, then the virtual base method and overriding the inheritance method will do:

 public abstract class BaseClass { public virtual void SomeMethod() { } } public abstract class InheritingClass : BaseClass { public override void SomeMethod() { } } 
0
source

All Articles