353
votes
27answers
52k views

Prefer composition over inheritance? [closed]

Why prefer composition over inheritance? What trade-offs are there for each approach? When should you choose inheritance over composition?
87
votes
4answers
23k views

What's wrong with overridable method calls in constructors?

I have a Wicket page class that sets the page title depending on the result of an abstract method. public abstract class BasicPage extends WebPage { public BasicPage() { ...
33
votes
12answers
6k views

Thou shalt not inherit from std::vector

Ok, this is really difficult to confess, but I do have a strong temptation at the moment to inherit from std::vector. I need about 10 customized algorithms for vector and I want them to be directly ...
66
votes
12answers
20k views

Inheritance vs. Aggregation [closed]

There are two schools of thought on how to best extend, enhance, and reuse code in an object-oriented system: Inheritance: extend the functionality of a class by creating a subclass. Override ...
10
votes
5answers
4k views

Why use Interfaces, Multiple Inheritance vs Interfaces, Benefits of Interfaces?

I still have some confusion about this thing. What I have found till now is (Similar questions have already been asked here but I was having some other points.) Interface is collection of ...
39
votes
7answers
7k views

Why there is no multiple inheritance in Java, but implementing multiple interfaces is allowed

Java doesn't allow multiple inheritance but it allows implementing multiple interfaces. Why?
86
votes
16answers
8k views

Use of .apply() with 'new' operator. Is this possible?

In JavaScript, I want to create an object instance (via the new operator), but pass an arbitrary number of arguments to the constructor. Is this possible? What I want to do is something like this ...
44
votes
11answers
39k views

Multiple Inheritance in PHP

I'm looking for a good, clean way to go around the fact that PHP5 still doesn't support multiple inheritance. Here's the class hierarchy: Message -- TextMessage -------- InvitationTextMessage ...
16
votes
7answers
13k views

Difference between Inheritance and Composition

Are Composition and Inheritance the same? If I want to implement the composition pattern, how can I do that in Java?
3
votes
1answer
281 views

Objects don't inherit prototyped functions

I have one constructor function, which acts as a superclass: Bla = function(a){this.a = a;} I prototype it to include a simple method: Bla.prototype.f = function(){console.log("f"); And now new ...
15
votes
5answers
3k views

Why are constructors not inherited?

I'm guessing there's something really basic about C# inheritance that I don't understand. Would someone please enlighten me?
38
votes
8answers
33k views

Why can't I create an abstract constructor on an abstract C# class?

I am creating an abstract class. I want each of my derived classes to be forced to implement a specific signature of constructor. As such, I did what I would have done has I wanted to force them to ...
20
votes
9answers
7k views

Inheritance or composition: Rely on “is-a” and “has-a”?

When I design classes and have to choose between inheritance and composition, I usually use the rule of thumb: if the relationship is "is-a" - use inheritance, and if the relationship is "has-a" - use ...
33
votes
5answers
21k views

PHP: get classname from static call in extended class

Here is the situation. I have two classes: Action, and MyAction, last one is declared as: class MyAction extends Action {/* some methods here */} All i need is method in Action class(only in it, ...
32
votes
11answers
12k views

Object Oriented Best Practices - Inheritance v Composition v Interfaces

I want to ask a question about how you would approach a simple object-oriented design problem. I have a few ideas of my own about what the best way of tackling this scenario, but I would be interested ...

1 2 3 4 5 11
15 30 50 per page