Yes this may be violating OO principles. There is not enough of the design shown to say with certainty but what I see is a code smell.
A good class exposes functionality and hides state. When a property is exposed rather than a functional method using that state then take a good hard look. When exposing a naked, as it were, substantially complex data object then client manipulation code spreads like cancer among the using classes. It's becomes a real face palm situation with inheritance where the problem grows exponentially for want of encapsulation.
I'm not anti data transfer object (DTO) at all. However one should strive for design that conveys intent and also restricts usage by encapsulating DTOs in functional classes.
He will get an object of DataB instead of DataA. Does this violate any OO principles?
I get the impression that these days inheritance is somehow considered a violation of OO principles. The following is very OO:
public class B : A { }
A myThing = new B(dataThingy);
Now myThing
can behave as an A
or B
. This is a very good thing if it fits in a design that supports requirements.
Capital I interface
, composition, and inheritance should be thought of as co-equal siblings that taken together make for flexible, expressive, and OO compliant design.
IData', whether it's A or B depends on what you defined as
private DataB _data;` in this case, it will be DataB. Is not it?