Reputation: 31
I picked this code form MSDN resources in C# polymorphism.
public class A
{
public virtual void DoWork() { }
}
public class B : A
{
public override void DoWork() { }
}
public class C : B
{
public override void DoWork()
{
// Call DoWork on B to get B's behavior:
base.DoWork();
// DoWork behavior specific to C goes here:
// ...
}
}
It says class C is overriding the DoWork() of B, but the DoWork() method of class B is not made virtual. Since C inherits all methods and data of B, does it also inherit the virtual methods of A (which are available in B, as A is the base class for B)?
What if B does not provide an implementation of DoWork() then would C be accessing the copy of A's virtual method directly to override it?
Also, when C inherits from B does it get a separate copy of A's members or the copy of those of B. I guess for separate copy of A's members one has to inherit A separately, like
public class C : A, B
Correct me if wrong.
EDIT :
public class A
{
public virtual void DoWork()
{
Console.WriteLine("In Class A");
}
}
public class B : A
{
public void DoWork() { Console.WriteLine("In Class B"); }
}
public class C : B
{
public void DoWork()
{
Console.WriteLine("In Class C");
base.DoWork();
}
}
When I run this on VS C#, virtual keyword is not reqd. in class B's DoWork(), and override keyword in class B's and C's DoWork(), as it generates only a tip-off warning. So does it mean just specifying which base class is being derived in the class name definition as in public class C : B is enough to qualify he methods o be virtual in nature?
Also, since C# doesnot provide with the option of multiple inheritance of classes, is there a way to directly use A's implementation of DoWork() from C's class without explicitly creating an object of A and then accessing DoWork() using it?
Upvotes: 0
Views: 150
Reputation: 1503799
It says class C is overriding the DoWork() of B, but the DoWork() method of class B is not made virtual.
When you override a virtual method, it "stays" virtual unless you explicitly seal it with sealed
. (You can only specify the sealed
modifier for a method if it's also an override method.)
What if B does not provide an implementation of DoWork() then would C be accessing the copy of A's virtual method directly to override it?
Yes. Or rather, it would invoke A's implementation at execution time. It's not really a "copy".
Also, when C inherits from B does it get a separate copy of A's members
It's not really clear what you mean here. What sort of "copy" are you talking about, and which members? If you mean which fields are in each object, then it's just the union of the fields declared in all the classes up the inheritance chain.
EDIT: In your edited code, you're not overriding anything. You're declaring new methods, which aren't called polymorphically. So if you write:
A a = new C();
a.DoWork();
that will just print "In Class A" - whereas if B
and C
overrode the virtual method, it would print "In Class C" and then "In Class B".
Also, since C# doesnot provide with the option of multiple inheritance of classes, is there a way to directly use A's implementation of DoWork() from C's class without explicitly creating an object of A and then accessing DoWork() using it?
Assuming this is in the case where B
overrides DoWork
, no. That would break encapsulation - it could break the invariants of B
, for example.
Upvotes: 6
Reputation: 3379
C
is inheriting from B
, this means it can override all virtual methods in B
. Override
means method is still virtual.
If B
would not give any implementation for DoWork
, then base.DoWork
in C
's implementation would call the implementation in A
class - as normally virtual behaves. You should not assume that your base call will be call to exactly B
method and no other - it's implementation detail that could be changed in future. Important thing is it will call nearet parent implementation of DoWork
.
And finally - when inheriting B, you also get all the properties, methods and everything inherited from A
. In C#
there is nothing like multiple inheritance (meaning C : A, B
where C
, A
and B
are classes will not work). You cannot get separate copy or anything of A
with inheritance - you just get what's in B
(which of course includes things that B
has inherited from A
and so on).
Upvotes: 1