C#: Inherit and override property [duplicate] - c#

This question already has answers here:
C#: override a property of the parent class
(3 answers)
Closed 4 years ago.
I have a class Base which is like:-
class Base
{
public virtual string a=>SomeMethod(a);
}
I need to override the variable a in Base.
class Derived: Base
{
public string a ="Hello";
}
Is this correct?What is the correct way to inherit from Base and override the value of a?
Thanks!

Use the keyword 'override'. However, you cannot use it on fields. If you want to override the virtual property in your base class, create a corressponding property in your derived class.
class Derived: Base
{
public override string a => "Hello";
}

Related

What is the difference between Pure Abstract Class and Interface in C#? [duplicate]

This question already has answers here:
What's the difference between an abstract class and an interface? [duplicate]
(5 answers)
Closed 3 years ago.
public abstract class PureAbstract
{
public abstract bool GetData();
}
public class ChildClass : PureAbstract
{
public override bool GetData()
{
Console.WriteLine("Pure Abstract Class called");
Console.ReadKey();
return true;
}
}
public class DIClass
{
private PureAbstract pureAbstract;
public DIClass(PureAbstract abstractClass)
{
this.pureAbstract = abstractClass;
this.pureAbstract.GetData();
}
}
class Program
{
static void Main(string[] args)
{
ChildClass child = new ChildClass();
DIClass pureAbstract = new DIClass(child);
}
}
We all know that Interface allows us Multiple Inheritance in C#, but I want to know that if we ignore this reason and assume we always need single inheritance in our application then what is difference between Pure Abstract Class and Interface.
In short, there is no reason why you would want a pure abstract class. Don't ever use pure abstract classes, there is no point in using them. If you want to use a 'pure abstract class', go with interface so you can still use multiple interfaces.
An interface is like a contract. If a class implements an interface it has to implement all the services listed in the interface.
An abstract class is like a skeleton. It defines a certain way its extended classes will work while letting the abstract methods to be unique.

How to override the property [duplicate]

This question already has answers here:
Can I override a property in c#? How?
(5 answers)
Closed 6 years ago.
in base LocalStorage
public class BaseStorageRepository<T>
{
protected string OneKey = null;
protected string ListKey = null;
public async Task UpdateAllAsync(List<T> data)
{
await BlobCache.LocalMachine.InsertObject(ListKey, data);
}
}
in child
public class CompanyStorageRepository : BaseStorageRepository<Company>
{
protected new string OneKey = "Company";
protected new string ListKey = "CompaniesList";
}
When execution in
UpdateAllAsync
then OneKey == null;
But why ?
After all, I redefined the property in the derived class
After all, I redefined the property in the derived class
No, you hid the field (not property) in the derived class. Effectively the derived class now has two distinct OneKey fields. The base class code is still looking at the field from the base class, not the new one in the derived class.
Also note that fields can't be virtual or overridden. You could set the field value in the derived class's constructor, though:
public class CompanyStorageRepository : BaseStorageRepository<Company>
{
public CompanyStorageRepository()
{
OneKey = "Company";
ListKey = "CompaniesList";
}
}
A marginally better design would be to use properties instead of fields. With properties, you can control (and detect) when a value is changed, and you can change how the value is stored without breaking the classes semi-public signature.

What access modifier to use [duplicate]

This question already has answers here:
In C#, what is the difference between public, private, protected, and having no access modifier?
(19 answers)
Closed 6 years ago.
Suppose I have a base class:
public class A {
public float someValue;
<Access Modifier Here> float SomeValue {
get {
return someValue;
}
}
}
And I want to derive from it:
public class B : A {
public float SomeProperty {
get {
return SomeValue;
}
}
}
What access modifier would I use if I want to make the SomeValue property only available to the deriving class and not anywhere else?
for only derived classes.. use protected
Protected means that access is limited to the containing class or types derived from the containing class.

How to have a default method while still be able to override it? [duplicate]

This question already has answers here:
What is the difference between an abstract method and a virtual method?
(28 answers)
Closed 8 years ago.
This is a C# project.
I have several classes inherited from a base class. Most of the child classes have the same behavior while some of them behave differently.
I'm using the new keyword.
public class Parent
{
public DataTable FetchScore()
{
// blahblah
}
}
public class ChildNormal : Parent
{
}
public class ChildOdd : Parent
{
public new DataTable FetchScore()
{
DataTable dt = base.FetchScore();
// blahblah
}
}
But the build says using new is not a good practice.
I cannot use virtual and override either, because I want a default. I don't want to copy the same override implementation many times.
How do I do this in C#?
You need to declare the base virtual (which means it can be overridden if necessary) and override it where you need different behavior from the original. The non-overridden derived class will behave just like the base class.
public class Parent()
{
public virtual DataTable FetchScore()
{
// blahblah
}
}
public class ChildNormal() : Parent
{
}
public class ChildOdd() : Parent
{
public override DataTable FetchScore()
{
DataTable dt = base.FetchScore();
// blahblah
}
}

modifying access permissions from an abstract class in the derived class [duplicate]

This question already has answers here:
Adding a set accessor to a property in a class that derives from an abstract class with only a get accessor
(3 answers)
Closed 9 years ago.
So I have an abstract class called AbstactSearchWithTwoLevelCache that was provided to me. All of its abstract properties only have read access (with a get;). I am not permitted to add a set; to the those abstract properties. Is there a way to change this in the derived class, SearchWithTwoLevelCache? In other words, is there a way to set these properties in the derived class?
If you mark the property with new, you define a new property, like this:
abstract class BaseClass
{
public int Property
{
get { ... }
}
}
class NewClass : BaseClass
{
public new int Property
{
get { return base.Property; }
set { ... }
}
}
EDIT:
The above works if the property in the base class is not abstract. When it is abstract, this will not work since you need to implement it. One option you do have is to create a class in between, like this:
abstract class BaseClass
{
public abstract int Property { get; }
}
class Between : BaseClass
{
public override int Property
{
get { ... }
}
}
class NewClass : Between
{
public new int Property
{
get { return base.Property; }
set { ... }
}
}
This however in no way is an elegant solution. Then, the real answer becomes that you cannot really do this (at least not without the above work around).
I can't imagine a way you can override them directly. Indirectly when you override the abstract class properties, the Getters can retrieve from a private field, and then you can create new properties that have Setters that set those fields.
Kind of a rig I know. Maybe there is a more elegant way around this.

Categories

Resources