Inheritance is a way to reuse code of existing objects, or to establish a subtype from an existing object, or both, depending upon programming language support.

learn more… | top users | synonyms

-3
votes
2answers
779 views

Why don't inherited methods use child properties? (PHP)

I'm trying to get the code below to work in child classes. But it keeps failing because it is checking the basicDbClass rather than the child class. For those complaining and voting my question down ...
2
votes
5answers
4k views

How to create a common interface for classes with different subsets of members

Don't know how to put it, But I'll try to be as clear as possible I have a project in which I am creating lots of classes and those classes have some common properties and methods but those methods ...
3
votes
1answer
308 views

What is the best way to use inheritance with ORMs?

If I have a People table and those people can be at different addresses, and each address can have more than one person, thats a many-to-many relationship. So, using ORMs like Entity Framework and ...
3
votes
7answers
902 views

Inheritance using non-abstract base class

This post is based on the question http://stackoverflow.com/questions/49002/prefer-composition-over-inheritance/11758048#comment15634305_11758048. Some people said - check whether there is “is-a” ...
3
votes
1answer
511 views

Composition vs. Inheritance [duplicate]

Here's what is given: public interface Request {} // there are 20 subclasses of Request public class CreateUserRequest implements Request { @NotEmpty public String userName; } // request ...
7
votes
9answers
1k views

What OO Design to use ( is there a Design Pattern )?

I have two objects that represent a 'Bar/Club' ( a place where you drink/socialise). In one scenario I need the bar name, address, distance, slogon In another scenario I need the bar name, address, ...
41
votes
11answers
8k views

Why is it good to split a program into multiple classes?

I'm still a student in high school (entering 10th grade), and I have yet to take an actual computer course in school. Everything I've done so far is through books. Those books have taught me concepts ...
3
votes
3answers
3k views

Can I use a child class variables in its parent class?

I am programming in Objective C, iOS. I create a Class A which is a parent class, and Class B inherits to Class A. Now I have Class B variables which I also need to use in Class A. And also one ...
3
votes
2answers
834 views

Is it a good programming practice to have a class with several .h files?

I suppose the class have several different interfaces. Some it shows to some class, some it shows to other classes. Are there any good reason for that? One thing I can think of is with one .h per ...
3
votes
2answers
239 views

Does Parallel Inheritance Make for Good Code?

I'm writing a database interface in PHP and I have a base dbTables class, as well as a base dbTableFields class. dbTables has a function, getFields(), that instantiates dbTableFields objects, each of ...
1
vote
2answers
149 views

Extend the API or use the same name as a class in the API?

I have been running into this problem more and more: I am not happy with the current API, and end up making my own class that does what I wish the API did; however, I don't extend the 'super class' as ...
4
votes
1answer
472 views

Any valid reason to Nest Master Pages in ASP.Net rather than Inherit?

Currently in a debate at work and I cannot fathom why someone would intentionally avoid Inheritance with Master Pages. For reference here is the project setup: BaseProject ...
3
votes
2answers
741 views

Derived Classes and namespaces

I am deriving a class, for use in my application, from a class provided by another group. Should the derived class be in the namespace for my application or the namespace of the parent class? While ...
7
votes
6answers
2k views

Why is subclassing too much bad (and hence why should we use prototypes to do away with it)?

I was reading up on design patterns, and I read that the prototype design pattern does away with excessive subclassing. Why is subclassing bad? What advantage would using a prototype bring about over ...
2
votes
1answer
2k views

Rails - to use STI or not…that is the question

Six months ago, I asked a question about modeling data for my app, and received some advice pointing me towards STI (see Rails data model - best practices question for the details). I played around ...
8
votes
5answers
2k views

Interface and Inheritance: Best of both worlds?

I 'discovered' interfaces and I started to love them. The beauty of an interface is that it is a contract, and any object that fulfills that contract can be used wherever that interface is required. ...
26
votes
4answers
12k views

Why should I prefer composition over inheritance?

I always read that composition is to be preferred over inheritance. A blog post on unlike minds, for example, advocates using composition over inheritance, but I can't see how polymorphism is ...
5
votes
3answers
1k views

When calling a method should we use base.methodname and this.methodname?

In C#, with an inherited class set -- when calling a method should we use keywords 'base.methodname and this.methodname'... irrespective of whether it is a overridden method or not? The code is ...
2
votes
3answers
2k views

Are super methods in JavaScript limited to functional inheritance, as per Crockford's book?

In Douglas Crockford's "JavaScript: The Good Parts", he walks through three types of inheritance: classical, prototypal, and functional. In the part on functional inheritance he writes: "The ...
5
votes
2answers
458 views

Who decided on the terminology downcasting and upcasting?

As far as I know, the terminology comes from how the inheritance hierarchy is traditionally displayed, with the extending types at the bottom, and the parent types at the top. This is a bit ...
17
votes
3answers
13k views

Best design for Windows forms that will share common functionality

In the past, I have used inheritance to allow the extension of Windows forms in my application. If all of my forms would have common controls, artwork, and functionality, I would create a base form ...
13
votes
4answers
2k views

Inheritance vs mixins in dynamic languages?

When should you prefer inheritance patterns over mixins in dynamic languages? By mixins, I mean actual proper mixing in, as in inserting functions and data members into an object in runtime. When ...
0
votes
2answers
245 views

Inheritance versus Composition in a business application

I have a training management and tracking system, with a high level structure as follows: We have a Role1, e.g. Manager, Shift-boss, miner, etc. and a Candidate, training for that Role. The role has ...
1
vote
2answers
132 views

Does hierarchial inheritance belong to the past? [closed]

Recently it came to my attention that hierarchical inheritance may be a relic of thinking of classes as "structs with functions" rather than a product contract-driven mentality. Consider, as a simple ...
6
votes
5answers
442 views

Java without implementation inheritance

In a recent video on Java, Joshua Bloch states at 4 minutes 20 seconds into the video: And then there's inheritance, and that was a marketing necessity. You know, we can argue whether you really ...
24
votes
4answers
4k views

How prototypal inheritance is practically different from classical inheritance?

Inheritance, Polymorphism, and Encapsulation are the three most distinct, important features of OOP, and from them, inheritance has a high usage statistics these days. I'm learning JavaScript, and ...
24
votes
9answers
3k views

Are abstract classes / methods obsolete?

I used to create a lot of abstract classes / methods. Then I started using interfaces. Now I am not sure if interfaces aren't making abstract classes obsolete. You need a fully abstract class? ...
4
votes
6answers
237 views

Using all the parameters of a function

Code Complete (2nd ed.) says: "If you pass a parameter to a routine, use it. If you aren't using it, remove the parameters from the routine interface." However, in the past I sometime had to do ...
44
votes
17answers
4k views

Is OO-programming really as important as hiring companies place it?

I am just finishing my masters degree (in computing) and applying for jobs. I've noticed many companies specifically ask for an understanding of object orientation. Popular interview questions are ...
3
votes
1answer
325 views

Is it possible to refactor inheritance to composition when virtual methods are called inside the base class?

Let's say I have a class called Country and two subclasses called AI and Player. The Country class has a number of virtual methods to allow player-specific or AI-specific behavior. I want to make it ...
7
votes
6answers
3k views

When you use inheritance to reuse code, do you find it too tricky that it swallows the benifits of reuse?

I've been coding for about 8 years, however I still find inheritance is too flexible and sometimes it makes you totally confused with the code you have written. One simplest example would be: ...
6
votes
1answer
244 views

“Super” vs “protected method” for running common code

In many langauges, super() lets you call the parent method which you have overridden. I've been using super in my Javascript (with fake object oriented implementation) to run common code for a long ...
9
votes
5answers
1k views

How do we know to favour composition over generalisation is always the right choice?

Whether an object physically exists or not, we can choose to model it in different ways. We could arbitarily use generalisation or composition in many cases. However, the GoF principle of "favour ...
4
votes
9answers
752 views

Does single inheritance limit what we can do with generalisation?

As a rule of thumb, generalisation is used only in specific circumstances. For example, when we can say that X is literally a subclass of Y. So, we can happily say that a Horse is a subclass of ...
71
votes
14answers
18k views

Where does this concept of “favor composition over inheritance” come from?

In the last few months, the mantra "favor composition over inheritance" seems to have sprung up out of nowhere and become almost some sort of meme within the programming community. And every time I ...
2
votes
3answers
206 views

Name for the process of prototype evolution

I'm looking for a commonly used name for the process of developing a child object from a parent object, either through clone-and-modify techniques or through the prototypal inheritance mechanisms ...
12
votes
6answers
2k views

Real-world scenarios for protected methods

Today I noticed that I basically never use protected methods in C++ code, because I rarely feel the need to call non-public methods of a parent. I do use protected in Java in the template method ...
0
votes
2answers
67 views

Marking services for secure handling; Annotation or inheritance?

We have a lot of services, some that demand some security, some that don't. We want an easy way of telling, in code, if a service will be secure or not. What would be the better way: Annotation or ...
0
votes
5answers
1k views

Why does the use of interface-based programming appear to be limited to behaviour?

I have been doing a little thinking about inheritance vs. realization vs. composition. I am not about to post the whole detail here. So I was wondering, when we are not talking about creating ...
5
votes
9answers
471 views

Is there a programming language with not a tree but tags idea behind OOP?

I'm thinking about tree structures, and I feel that I don't like them. It's like when you have a shop, then you try to put all products to tree-like catalog, and then you need to place one product to ...
36
votes
9answers
4k views

Code Smell: Inheritance Abuse [duplicate]

It's been generally accepted in the OO community that one should "favor composition over inheritance". On the other hand, inheritance does provide both polymorphism and a straightforward, terse way of ...