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:
Right now what I'm doing is:
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
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
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)?
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
what is the point of encapsulating if you have to be constantly breaking the capsule?
if you are doing that then your class design is wrong
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?
classes are essentially for grouping related data and restricting how code outside that class is allowed to interact with it
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?
i can't answer that with this theoretical problem
in general fields should never be public
aah, so it depends on the specifics of each program is what you're saying?
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
@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. 🙂
is this a joke repo?
Yes. Only for conveniently sharing random stuffs.
is the joke on oop?
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?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))