C
C#•10mo ago
Gipper

Best way to do this OOP-wise???

Ok, so this is not the actual program but for the sake of brevity I'll give you an equivalent one:
namespace namespace1
{
internal class Program
{

static void Main(string[] args)
{
BaseClass baseClass = new BaseClass();
DerivedClass1 = new DerivedClass1();//Here I initialize fields in BaseClass
DerivedClass2 = new DerivedClass2();//Here I want the data initialized in BaseClass on the previous line
}
}
}
namespace namespace1
{
internal class Program
{

static void Main(string[] args)
{
BaseClass baseClass = new BaseClass();
DerivedClass1 = new DerivedClass1();//Here I initialize fields in BaseClass
DerivedClass2 = new DerivedClass2();//Here I want the data initialized in BaseClass on the previous line
}
}
}
internal class BaseClass
{
private protected int someData;
private protected string someMoreDataStr;
}
internal class BaseClass
{
private protected int someData;
private protected string someMoreDataStr;
}
internal class DerivedClass1 : BaseClass
{
\\THIS CLASS INITIALIZES DATA TO THE FIELDS IN BASECLASS
public DerivedClass1()
{
someData=3;
someMoreDataStr = "foo";
}
}
internal class DerivedClass1 : BaseClass
{
\\THIS CLASS INITIALIZES DATA TO THE FIELDS IN BASECLASS
public DerivedClass1()
{
someData=3;
someMoreDataStr = "foo";
}
}
internal class DerivedClass2 : BaseClass
{
\\This Class will be instantiated after DerivedClass1 And I want to have the 3 And the "foo" In there
public DerivedClass2()
{
\\I want to have the same data initialized earlier here
}
}
internal class DerivedClass2 : BaseClass
{
\\This Class will be instantiated after DerivedClass1 And I want to have the 3 And the "foo" In there
public DerivedClass2()
{
\\I want to have the same data initialized earlier here
}
}
Right now what I'm doing is:
namespace namespace1
{
internal class Program
{

static void Main(string[] args)
{
BaseClass baseClass = new BaseClass();
DerivedClass1 = new DerivedClass1(baseClass);//Here I initialize fields in BaseClass
DerivedClass2 = new DerivedClass2(baseClass);//Here I want the data initialized in BaseClass on the previous line
}
}
}
namespace namespace1
{
internal class Program
{

static void Main(string[] args)
{
BaseClass baseClass = new BaseClass();
DerivedClass1 = new DerivedClass1(baseClass);//Here I initialize fields in BaseClass
DerivedClass2 = new DerivedClass2(baseClass);//Here I want the data initialized in BaseClass on the previous line
}
}
}
and just accessing the data through the argument passed in to the constructor and I'm only inheriting so I can access the private protected field in BaseClass. Is there a better way of doing this (i.e. two classes derived from the same base sharing the values of the vars in the baseObj)? Should I just make the field public and not inherit?
17 Replies
Jimmacle
Jimmacle•10mo ago
if you want to instantiate a new object by copying values from another object you'd do it the way you're doing now 2 classes derived from the same base can't share data because they're still completely different instances, inheritance doesn't affect that
Gipper
GipperOP•10mo ago
Then what is the point of breaking up code into different classes as opposed to having it all in the same class when you're just gonna have to end up passing data back and forth through instantiated objects? Doesn't that essentially land you back where you started (i.e. just having everything inside the same class)?
Jimmacle
Jimmacle•10mo ago
no? you use classes to encapsulate related sets of data and behavior if you need to pass data from one class to another then you pass it it might help if you describe the actual goal, i can't really understand what you're really trying to do from this example
Gipper
GipperOP•10mo ago
what is the point of encapsulating if you have to be constantly breaking the capsule?
Jimmacle
Jimmacle•10mo ago
if you are doing that then your class design is wrong
Gipper
GipperOP•10mo ago
You've already helped me on what to do, which is what I was already doing, I just don't understand some OOP concepts very deeply I guess hmm, I guess I just lack experience in larger codebases only my personal projects, then what is the difference between good class design as far as encapsulation is concerned and bad design?
Jimmacle
Jimmacle•10mo ago
classes are essentially for grouping related data and restricting how code outside that class is allowed to interact with it
Gipper
GipperOP•10mo ago
hmm, and to my specific example should I just make the fields I need to access public, or should I make them protected and inherit in the classes that should access them?
Jimmacle
Jimmacle•10mo ago
i can't answer that with this theoretical problem in general fields should never be public
Gipper
GipperOP•10mo ago
aah, so it depends on the specifics of each program is what you're saying?
Jimmacle
Jimmacle•10mo ago
yes, your class design depends entirely on the problem you're trying to solve the way you group/protect data depends on what the data actually is and how you need to control it
i like chatgpt
i like chatgpt•10mo ago
@Gipper Try deciphering https://github.com/suugbut/SuperTrashBin/blob/main/simple_oop_in_action.cs to understand the basic of OOP in less than 1 hour. 🙂
Gipper
GipperOP•10mo ago
is this a joke repo?
i like chatgpt
i like chatgpt•10mo ago
Yes. Only for conveniently sharing random stuffs.
Gipper
GipperOP•10mo ago
is the joke on oop?
i like chatgpt
i like chatgpt•10mo ago
No. It is a serious OOP in action. With this example, you learn encapsulation, inheritance and polymorphism in less than 1 hour instead of 1 semester. 🙂 I chose a simple scenario with very basic math topic that everyone should have learnt in the elementary school. - Shape is the base class from which other real shapes are derived. For simplicity, Shape has one common property Name and common functionality Area() because they are needed by any derived classes. - Circle is a derived class from Shape. - Triangle is also a derived class from Shape. - @ in @base is mandatory to "escape" base which is a reserved word in C#. - PrintArea() can also be made as a method in Shape but I deliberately extracted it from Shape and made it as an extension method only for learning purposes. Any other question?
Pobiega
Pobiega•10mo ago
Inheritance doesn't help with moving data from 1 object instance to another, it carries over structure and allows for polymorphism (using an instance of a derived class as if it was an instance of the base class (because it is))
Want results from more Discord servers?
Add your server