This question already has answers here:
C# virtual keyword
(9 answers)
C# - Keyword usage virtual+override vs. new
(11 answers)
Can you write virtual functions / methods in Java?
(6 answers)
Closed 4 years ago.
In an interview it was asked why do we need to override method of base class.
I tried to answer like when we want to have different implementation in derived class.
But then he said, "Why don't we just create a new method with different name and different implementation instead of overriding base class method?", anyway we are not reusing base class method as implementation will be different then just create a new method instead of overriding.
I got confused what to answer. Could somebody explain.
public class BaseClass
{
virtual void Foo(){}
}
public class DerivedClass: BaseClass
{
override void Foo(){}
}
Generally we implement overriding like above.
What he said is like why do we need concept of overriding we can do like below
public class BaseClass
{
void Foo(){}
}
public class DerivedClass: BaseClass
{
void Foo1(){}
}
His question was looking weird I tried to explain but like its a method of base class we are just redefining it in derived class. In this way our code will be clean as well. But looks like he was not satisfied.
I would check this answer:
Why does this polymorphic C# code print what it does?
then try to grasp the concept of methods in an object basically having pointers to code. When you override implementation in an subclass then that becomes the new code pointed to, whether it's used or cast as a superclass or not.
So the main purpose of overriding is to create classes that inherit from one class but each have their own implementation and then be able to treat or operate on them equally the same as the original superclass. This is the essence of the Liskov Principle or the 'L' in SOLID. What is an example of the Liskov Substitution Principle?
Related
This question already has answers here:
Default Interface Methods. What is deep meaningful difference now, between abstract class and interface?
(6 answers)
Closed 3 years ago.
C# 8.0 has introduced a new language feature – default implementations of interface members.
public interface IRobot
{
void Talk(string message)
{
Debug.WriteLine(message);
}
}
The new default interface implementations provides the elements of the traits language. However is is also blurring the line between what is an abstract class and what is an interface.
What is now the benefit of using an abstract class instead of an interface with default implemenation?
Funny enough, but the CLR implements default interfaces as abstract classes. CLR supports multiple inheritance. Here is a good article about this.
In C#, you need default interfaces when you need to implement (in this case actually inherit from) multiple interfaces, because the language doesn't let you inherit from multiple classes.
An abstract class also allows you to have state. State, i.e. fields, are not allowed in interfaces.
This question already has answers here:
Multiple Inheritance in C#
(13 answers)
Closed 4 years ago.
All similar questions I've looked though here, mentions multiple interface inheritance. However, I am not sure how MII could be a workaround to the problem.
Lets say, I have two library class (My_Class_1 and My_Class_2) of different methods. Then I want to create a new class, that can use both of these classes' methods natively, like:
public class My_Application : My_Class_1, My_Class_2 {
public My_Application(){
method_from_Class1();
smth_property_declared_in_My_Class_2 = "hello";
}
}
However, that is not possible with C#. What are flexible workarounds, to extend/enrich class with other classes? In PHP, that is unbelievably simple, just in the top of the class we can:
use example_trait_1;
use example_trait_2;
I would rather not use interfaces; in my view, they have no relation to solving this problem.
p.s. I don't want to create initialize objects for those classes. I want them to be native part of the application class.
As others mentioned, it isn't possible (because that's not what inheritance is), but it seems one workaround would be to have a public property of type Class1, inside a class that inherits Class2.
That also gets around the problem of "which class am I looking at now?" since you'd need to explicitly mention the property when you want something of type Class1.
And if you need to modify Class1 first, then you just create a separate class that inherits Class1 first, then have the public property be of that new type.
This question already has answers here:
Why do members of a static class need to be declared as static? Why isn't it just implicit?
(8 answers)
Closed 7 years ago.
My question is why members are not by default static in static class.
As we see, interface members are by default public and abstract.
Thanks,
Anil
We should ask c# language designer.
But I understand the ratio behind: it forces the programmer to say "this function is static", even if it would be implied by the fact the the class is static.
Maybe It's a matter of readability: when you read a method without body (and no abstract keyword), you know that this method can only be part of an interface.
When you read a method without "static" modifier, you would need to read also class declaration to understand that is part of a static class and therefore static itself
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Non Public Members for C# Interfaces
Suppose I have
internal interface IInterface
{
int MyProperty { get; set; }
}
public class MyClass : IInterface
{
internal int MyProperty
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
}
I get this:
does not implement interface member; cannot implement an interface
member because it is not public;
I know what's the fix, but I am wondering why doesn't C# let interface members to be private.
Many times over I wanted my classes to follow a pattern, but needn't expose the members to public, say when I am writing a library. And best of all, the Interface itself is not public :X
Note: I am not asking how to implement private interface members, but I am knowing the design logic that went behind this decision. I couldn't find a suitable duplicate.
Update: More than the original thread, this code sample from the answer in another thread explains it better than most description answers. Or even better, from #JonSkeet this
An interface is used to define a contract, by making the fields/methods private there is really no point in using an interface then. How does the client know how to use the contract?
Unless you really need an abstract class.
The point of interfaces is that they provide a contract that other objects can use to communicate with your object. There would be not point of making the members private because it would not be useful anymore
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why can’t I create an abstract constructor on an abstract C# class?
Can abstract class's Constructor be marked as 'abstract'?
No. C# does not support this in any version. Note that constructors are not inherited in derived classes, although they can be "chained". This is probably what you want to be doing.
If you want to indicate that the derived class should be doing some sort of initialisation, you could create an abstract Initialise method or such which the constructor of the base class (and indirectly of the sub-class) calls on creation.
As a side point, I'm not sure whether the CLR (or associated CIL language) actually supports it - I would suspect it may, though there is little use for it from within C# for the reason just mentioned.
No, a constructor cannot be marked as abstract. In abstract classes constructors are usually marked as protected though, at least that's what I would recommend you doing.
Basically no.
If its abstract, you have to override it in a concrete child class, and you can't override a constructor, only overload it.