An abstract method overrides an abstract method - c#

public abstract class A
{
public abstract void Process();
}
public abstract class B : A
{
public abstract override void Process();
}
public class C : B
{
public override void Process()
{
Console.WriteLine("abc");
}
}
This code throws an Compilation Error: 'B' does not implement inherited abstract member 'A.Process()'.
Is there any way to do this?

Just leave out the method completely in class B. B inherits it anyway from A, and since B itself is abstract, you do not explicitly need to implement it again.

Works for me in VS2008; no errors, no warnings. BUT, there's no reason to have the 'override' in B. This code is equivalent:
public abstract class A
{
public abstract void Process();
}
public abstract class B : A
{
}
public class C : B
{
public override void Process()
{
Console.WriteLine("abc");
}
}

The place where I've seen this sometimes is overriding a non-abstract virtual method with an abstract method. For example:
public abstract class SomeBaseType
{
/* Override the ToString method inherited from Object with an abstract
* method. Non-abstract types derived from SomeBaseType will have to provide
* their own implementation of ToString() to override Object.ToString().
*/
public abstract override string ToString();
}
public class SomeType : SomeBaseType
{
public override string ToString()
{
return "This type *must* implement an override of ToString()";
}
}

Alon -
This makes no sense. First of all, this does actually compile fine. Secondly, the abstract method you declared in A is inherited (still abstract) into B. Therefore, you have no need to declare Process() in class B.
--
Mark

Related

Why abstract class cannot have Sealed method [duplicate]

This question already has an answer here:
Can we declare sealed method in a class
(1 answer)
Closed 4 years ago.
Code Snippet 1 (Compilation Error) - A.M2() cannot be sealed because it is not an override
abstract class A
{
public abstract void M1();
public sealed void M2()
{
// Do Something
}
}
Code Snippet 2 (Works Fine)
abstract class A
{
public abstract void M1();
public virtual void M2()
{
}
}
class B : A
{
public sealed override void M1()
{
}
public sealed override void M2()
{
}
}
Question - If I am providing the implementation of a method in the Abstract class itself, why would C# not allow me to mark it Sealed, why would it want me to override in the sub class, there after mark it as sealed. I cannot understand this discrepancy
Sealed keyword can only be put on functions that are overridable.
That function you specified, is not declared as a virtual function, and hence is not overridable. Also it does not make any sense for a function to be declared "virtual" and "sealed" as sealed cancels out being "virtual"
Sealed only can be used hand in hand with the "override" keyword, and stops other classes from overriding the functions themselves.
It has nothing to do with an abstract class. You cannot make a method as sealed in any class until it is an override method in derived class.
If you had intentions for restricting it from override in derived class then you better use private access modifier.
And the reason why you could use sealed in derived class; I've an example of it below
You have three classes A,B,C where B overrides A and C derives from B -> B:A, C:B
abstract class A
{
public abstract void MyMethod();
}
class B : A
{
public sealed override void MyMethod()
{
}
}
class C : B
{
public override void MyMethod()
{
}
}
In above example we could override method of A in B class because it is not sealed. But if you override method of B in class C then it is not allowed due to sealed keyword.
It will restrict further overrides from class B. Thats where we can use sealed

Override method from instance of the class

I have a merly simple question, but seems cant find an answer for it, I want to know if its possible to override a method from a instance class structore would look like this:
public class A : baseA
{
public virtual void methodA()
{
}
}
public class B : baseB
{
public void method B()
{
var ClassA = new A();
}
/* Now Is there some sort of overide like */
public override methodA()
{
//Do stuff
}
}
And those classes do not inherit from each other, to make it more difficult.
Now if this sort of construction is possible in c#?
No. You cannot override a class's behavior if you don't inherit from it.
The override modifier is required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event.
Class B must inherit from class A in order to do so.
public class A
{
public virtual void methodA()
{
}
}
public class B : A
{
public void methodB()
{
var ClassA = new A();
}
public override void methodA()
{
//Do stuff
}
}
Check MSDN for more details:
An override method provides a new implementation of a member that is inherited from a base class. The method that is overridden by an override declaration is known as the overridden base method. The overridden base method must have the same signature as the override method

Set generic type property in derived classes

I have a generic type G<T> where T : A, where A is an abstract class. In each class B derived from A I want to have a field of type G<B>, without writing repetetive code, however I'm not sure if it's even possible. One way to do this would be
abstract class A
{
protected object g;
protected abstract void SetG();
public A()
{
SetG();
}
}
class B : A
{
protected override void SetG()
{
this.g = new G<B>();
}
public B() : base() {}
}
But this would mean a lot of repetetive code in every derived class. Is there a better way to do this?
You could add an extra abstract class in between:
public abstract class A<T> : A where T : A
{
protected override void SetG()
{
this.g = new G<T>();
}
}
...then, update your B declaration to:
public class B : A<B>
{
public B() : base() { }
}
I believe that what you are trying to do is a Covariant Conversion. See this MSDN article on using delegates and see if that works for you. Look in the section "Using Delegates with Covariant Type Parameters".
In your A:, create a delegate:
Func<G<A>> GetG;
Then, in your derived classes, set this func pointer to a function of type
Func<G<B>>
Bingo!

Preventing the virtual and override in inheritance chain

Please note: This is a conceptual question and not related to production specific code.
Suppose we have Class A with virtual method GetBonus(int value)
Next, we derive a class from this called Class B. In this Class B we override the method GetBonus.
Next, we derive a class from Class B called Class C.
Now class C can also override the GetBonus method of class A.
Question:
Whether Class C overrides the method definition of Class A or of the Class B?
In Class C, How can the overriding of the method of Class A be prevented ?
In Class C, How can the overriding of the method of Class B be prevented ?
I know that there is a SEALED keyword for sealing the virtual overridden methods. But above questions will help me clear my doubts.
Questions 2 and 3 boil down to the same thing basically, and sealed is indeed the answer here.
Perhaps you asked it a bit vague, but you can only prevent overriding of virtual methods in derived classes. Not in the derived class itself. In the end, for both questions 2 and 3, you have only one option:
class A
{
public virtual void GetBonus(int value) { }
}
class B : A
{
public sealed override void GetBonus(int value) { } // We seal this method
}
class C : B
{
public override void GetBonus(int value) // This line is invalid
// because it cannot override the sealed member from class B.
{ }
}
This will prevent method GetBonus from getting overridden in derived classes.
This sample also answers question 1. It gives a compilation error because class C's override of GetBonus is attempting to override the version provided by class B and not the one provided by A. This is true because overriding the one from A would obviously work as it isn't sealed.
according to https://msdn.microsoft.com/en-us/library/ms173149%28v=vs.110%29.aspx (take a look at picture)
when you override a virtual method and then derived that class in another class you inherit overriden implementation
class A
{
public virtual void GetBonus(int value)
{
//if you define this method as seald no one can override this
}
}
class B:A
{
public override void GetBonus(int value)
{
}
}
class C:B
{
public override void GetBonus(int value)
{
//here we override implementation of class B
}
}
}

Type-Cast member of derived classes

I have a class M and many derived classes A:M, B:M, C:M
The derived classes A,B,C are obviously different, else they were not been declared.
M implements a member that needs to know the type of the calling class.
So, today I'm using an abstract member in M and individually override in each dervied class.
In M:
public abstract Do() {};
In A:
public override void Do()
{
DoMore<A>();
}
In B:
public override void Do()
{
DoMore<B>();
}
In C:
public override void Do()
{
DoMore<C>();
}
Is there a way to implement Do() in M just once for all derived classes ? Something like:
In M:
public Do<T>() {
DoMore<T>();
}
This does not work because DoMore() is casting on the derived class.
If you make the base class generic, you can make it "aware" of its derived type:
public class M<T>
{
public Do<T>()
{
DoMore<T>();
}
}
public class A : M<A> {...}
public class B : M<B> {...}
public class C : M<C> {...}
It will be possible in case you create the
DoMore method in the base class and make it abstract and override
in the child class so that appropriate method is called
but more or less it will be the
same as overriding Do method which you have just move to parent class and made it non abstract.

Categories

Resources