-1

How can I use setters in the concrete class? I have two abstract classes and the bottom concrete class should be able to set all the private variables I have the abstract classes, how can I do that? I could just add getters and setters in my concrete class, but because I have 4 derived classes from my second abstract class, I don't want to have duplicate code and a long list of public properties, any way to resolve that?

I am working in C#

axis93
  • 17
  • 4
  • 1
    Can you share your code? – Pavel Anikhouski Nov 13 '19 at 15:02
  • 1
    Also, private members are private by definition. You cannot access them outside the declaring type. – Tanveer Badar Nov 13 '19 at 15:04
  • 1
    Please edit your question and add your code. Don't describe your classes; *show us*. –  Nov 13 '19 at 15:05
  • 3
    Children of abstract classes cannot access private members belonging to the parent. If you want this behaviour, make them `protected`. – Kyle Nov 13 '19 at 15:05
  • 1
    As everyone else said, you want protected, not private. This allows child classes to access the members and no other classes. – Nick Henry Nov 13 '19 at 15:07
  • If you don't want to duplicate code into Concrete class, do it in the Abstract class. – Che Nov 13 '19 at 15:14
  • 1
    A piece of code would be helpful to undertand what you want to do. why you need all properties from abstract class ? As the other people point you have to deine protected properties to be able set or get tem from derived class. But It doesn't sound efficent to me reach all properties and set them again. Are you sure you don't need virtual properties ? Instead of setting base class ovveride your virtual properties with new values. – nzrytmn Nov 13 '19 at 15:17

2 Answers2

2

Using the protected keyword in c# you can access the variables in parent objects Like this

public abstract class Parent {
    protected int integer {get;set;}
}
public class Child : Parent {

    public Child(int value) {
        integer = value;
    }
    public int getValue() {
        return integer;
    }
}

see : https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/protected

Joost K
  • 1,096
  • 8
  • 23
0

From Microsoft documentation :

private
The type or member can be accessed only by code in the same class or struct.

protected
The type or member can be accessed only by code in the same class, or in a class that is derived from that class. internal The type or member can be accessed by any code in the same assembly, but not from another assembly.

private protected
The type or member can be accessed only within its declaring assembly, by code in the same class or in a type that is derived from that class.

So doesn't matter if the class is abstract or not, private can only be accessed within the same class.

Fourat
  • 2,366
  • 4
  • 38
  • 53
  • You assume OP has 7.2 version, and that is a fairly new version. From your linked documentation: "The private protected access modifier is valid in C# version 7.2 and later" – Cleptus Nov 13 '19 at 16:21
  • The question concerns c# (see tags) not general OOP. I'm not assuming anything I'm just paying attention – Fourat Nov 13 '19 at 16:29