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.
-5
votes
0answers
50 views
Virtual function in C++ [closed]
I have some doubts regarding inheritance in C++ language.
What is a virtual function?
Why does one need to define virtual functions in a class?
If I define/declare a virtual function in a base ...
2
votes
4answers
122 views
Initializing derived classes in the same way
I have a class Base that has several children, say A, B, C. For testing purposes I'd like to mock those derived classes by deriving from them. So MockA derives from A, MockB derives from B and so on.
...
1
vote
1answer
98 views
Class table inheritance… To 'type' or not to 'type'
I currently have a database that uses Class table inheritance model. Three different tables inherit from this table. The child tables have all a FK to the parent table and the fields are properly ...
24
votes
13answers
957 views
Multiple Interfaces in Java - Good or bad
I'm coming out of an interview just now and the interviewer asked me if a Java interface can "extend" more than one interfaces. I was thinking multiple inheritance is disallowed in java so got that ...
7
votes
1answer
156 views
Why is the use of constructors discouraged when creating prototypes?
Quick background: In JavaScript, the constructor function for each object type has a prototype property. The prototype refers to an object that each constructed object uses as the next step up in its ...
7
votes
6answers
658 views
Why are constructors not inherited?
I am confused as to what the problems could be if a constructor was inherited from a base class. Cpp Primer Plus says,
Constructors are different from other class methods in that they
create ...
1
vote
1answer
182 views
What principle of OOAD is this pattern breaking?
I'm trying to make a case for not putting the structure in the parent BaseModule class I've shown below. I'm more for a Strategy Pattern, and minimizing inheritance in favor of has-a relationships, ...
10
votes
3answers
882 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 ...
7
votes
2answers
256 views
How do I avoid writing lots of pass-through functions in a wrapper?
I have a class, which wraps another class of a common base type. Because the base type interface is quite large this involves writing a lot of pass-through functions. I am looking for a way to avoid ...
1
vote
0answers
81 views
Inheritance and constricted referencing
Let's say I have BaseA and BaseB classes. BaseB can have to several references of BaseA instances. Now, we have two other classes, DerivedA and DerivedB that respectively inherit from BaseA and BaseB.
...
15
votes
3answers
6k 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 ...
6
votes
4answers
526 views
Looking for a real-world example illustrating that composition can be superior to inheritance
I watched a bunch of lectures on Clojure and functional programming by Rich Hickey as well as some of the SICP lectures, and I am sold on many concepts of functional programming. I incorporated some ...
3
votes
2answers
193 views
Tricky compareTo, inheritance, easy to extend - Java
Let's say we have 4 classes A, B, C, D where: A is a superclass of both B and C and C is a superclass of D. I suppose, in a diagram, it should look like this:
A
/ \
B C
\
D
...
4
votes
5answers
507 views
How often is Inheritance used?
I admit that I am a junior developer, and so far I've only built simple web applications in ASP.NET MVC. But I've never had to use the inheritance aspect of Object Oriented Programming in my own ...
4
votes
2answers
265 views
Why Java does not support private/protected inheritance like C++? [closed]
While inheriting a class in C++, user can specify the access specifier like,
class Base
{
public int mem1;
protected in mem2;
};
class Derived1 : **private** Base
{
// mem1 will be ...
19
votes
7answers
685 views
Should I test inherited methods?
Suppose I have a class Manager derived from a base class Employee, and that Employee has a method getEmail() that is inherited by Manager. Should I test that the behaviour of a manager's getEmail() ...
25
votes
5answers
1k views
Many small classes vs. logical (but) intricate inheritance
I'm wondering what is better in terms of good OOP desing, clean code, flexibility and avoiding code smells in the future. Image situation, where you have a lot of very similar objects you need to ...
2
votes
2answers
104 views
Template method within one class without subclasses or inheritance
I have an algorithm with mostly invariant parts that needs to be reused within one class so as to stay DRY.
Code duplication with repeating method structure
public void save(String key, int value) ...
0
votes
1answer
266 views
Code reuse via inheritance [duplicate]
I have a set of classes that are all dealing with some related tasks. These tasks do have different inputs and outputs. This causes it to become impossible to have the tasks done via shared code ...
6
votes
6answers
2k 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:
...
5
votes
2answers
209 views
How does strengthening of pre conditions and weakening of post conditions violate Liskov Substitution principle?
I read that Liskov substitution principle is violated if :
Pre conditions are strengthened .
Post conditions are eased out.
But I don't get fully yet how these two points would violate Liskov ...
6
votes
1answer
226 views
Declaring functions as final… except when it is me who does the deriving
I have a class in which I want to disallow other programmers from overriding one of it's methods, since it requires special knowledge of the inner workings of the class. Since I personally know how ...
51
votes
14answers
10k 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 ...
1
vote
1answer
127 views
How to share common methods if objects have different roles?
If 2 classes have the following in common:
part of how their state is represented (both have a linear container)
multiple identical methods (identical code, not just signature)
But are not ...
32
votes
9answers
2k views
Code Smell: Inheritance Abuse
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 ...
3
votes
5answers
828 views
Why does Java allow to implement different interfaces, each containing a method with the same signature?
I recently found out that I can have two interfaces, one containing a method with the same signature as a method in the other interface. And I can have an interface or class that implements both of ...
2
votes
2answers
412 views
Alternative to “inheritance versus composition?” [duplicate]
Possible Duplicate:
Where does this concept of “favor composition over inheritance” come from?
I have colleagues at work who claim that "Inheritance is an anti-pattern" and want to use ...
1
vote
2answers
265 views
REST API wrapper - class design for 'lite' object responses
I am writing a class library to serve as a managed .NET wrapper over a REST API. I'm very new to OOP, and this task is an ideal opportunity for me to learn some OOP concepts in a real-life situation ...
4
votes
2answers
354 views
Naming methods that do the same thing but return different types
Let's assume that I'm extending a graphical file chooser class (JFileChooser).
This class has methods which display the file chooser dialog and return a status signature in the form of an int: ...
0
votes
2answers
83 views
How do you handle objects that need custom behavior, and need to exist as an entity in the database?
For a simple example, assume your application sends out notifications to users when various events happen. So in the database I might have the following tables:
TABLE Event
EventId ...
0
votes
0answers
55 views
Class hierarchy problem in this social network model
I'm trying to design a class system for a social network data model - basically a link/object system. Now I have roughly the following structure (simplified and only relevant methods shown)
class ...
7
votes
1answer
341 views
JavaScript objects and Crockford's The Good Parts
I've been thinking quite a bit about how to do OOP in JS, especially when it comes to encapsulation and inheritance, recently.
According to Crockford, classical is harmful because of new(), and both ...
9
votes
5answers
927 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 ...
2
votes
3answers
918 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 ...
2
votes
7answers
639 views
OO Software Architecture - base class that everything inherits from. Bad/good idea?
I am reviewing a proposed OO software architecture that looks like this:
Base
Foo
Something
Bar
SomethingElse
Where Base is a static class.
My immediate thought was that every object in any ...
4
votes
1answer
268 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
...
0
votes
5answers
709 views
Inheritance vs composition in this example
I'm wondering about the differences between inheritance and composition examined with concrete code relevant arguments.
In particular my example was
Inheritance:
class Do:
def do(self):
...
-3
votes
2answers
331 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
1k 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
239 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 ...
2
votes
7answers
573 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” ...
2
votes
1answer
332 views
Composition vs. Inheritance
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
931 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, ...
37
votes
11answers
4k 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 ...
1
vote
2answers
496 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
197 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
114 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 ...
0
votes
2answers
193 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 ...
7
votes
6answers
913 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 ...
3
votes
2answers
270 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 ...


