oop
how to achieve dynamic polymorphism without extending a class
Decorator design pattern that exploits encapsulation is what you’re looking for. Polymorphism through inheritance: class Cat { void meow() { // meow… } } class Lion extends Cat { } Polymorphism through encapsulation (Decorator pattern): interface Cat { void meow(); } class Lion implements Cat { private Cat cat; void meow() { this.cat.meow(); } } … Read more
Best method for storing this pointer for use in WndProc
This question has many duplicates and almost-duplicates on SO, yet almost none of the answers I’ve seen explore the pitfalls of their chosen solutions. There are several ways how to associate an arbitrary data pointer with a window, and there are 2 different situations to consider. Depending on the situation, the possibilities are different. Situation … Read more
Why are constructors not inherited in C#?
Sometimes, when subclassing, you want to restrict the conditions required to create an instance of the class. Let me give you an example. If classes did inherit their superclass constructors, all classes would have the parameterless constructor from Object. Obviously that’s not correct.
Why should I ever overload methods?
I think if you talk about the real benefits of function/method overloading, something without which you won’t get your way around, then as you’ve pointed out in your question, you won’t find any. But how is it helpful? Let’s consider this example. Let’s suppose that I’m making an application that finds a person by his … Read more
Object Oriented Best Practices – Inheritance v Composition v Interfaces [closed]
Mark, This is an interesting question. You will find as many opinions on this. I don’t believe there is a ‘right’ answer. This is a great example of where a rigid heirarchial object design can really cause problems after a system is built. For example, lets say you went with the “Customer” and “Staff” classes. … Read more
“AVOID using library private types in public APIs” – lint warning, even in in cookbook examples?
From the latest docs: Subclasses should override this method to return a newly created instance of their associated [State] subclass: @override State<MyWidget> createState() => _MyWidgetState(); So you should replace @override _AnimatedContainerAppState createState() => _AnimatedContainerAppState(); with @override State<AnimatedContainerApp> createState() => _AnimatedContainerAppState();
“Program to an interface”. What does it mean? [duplicate]
To put it simply, instead of writing your classes in a way that says I depend on this specific class to do my work you write it in a way that says I depend on any class that does this stuff to do my work. The first example represents a class that depends on a … Read more
Why are interfaces preferred to abstract classes?
That interview question reflects a certain belief of the person asking the question. I believe that the person is wrong, and therefore you can go one of two directions. Give them the answer they want. Respectfully disagree. The answer that they want, well, the other posters have highlighted those incredibly well. Multiple interface inheritance, the … Read more