2
votes
3answers
156 views

OOD: Java inheritance and access to child methods via casting

I have several classes Parent and Child1 ... Child9 implemented in Java. Parent is an abstract class, containing all the common variables of the child classes (a lot, which is the main reason why I ...
1
vote
1answer
134 views

Should I declare the most specific type as return type of method?

Sorry for my English In Android I can put only ArrayList of Parcelable objects in Intent: Intent putParcelableArrayListExtra(String name, ArrayList<? extends Parcelable> value); I can't put ...
1
vote
2answers
120 views

Deriving from a class only to provide a special constructor

In our codebase, I encountered the following construct: There is a base class B, and then there is a class D which derives from B, but does not override or add any members. In fact, D is completely ...
3
votes
2answers
372 views

Are there any OO languages without inheritance?

During a code review today, a colleague of mine said something interesting: prototype is only useful when you need inheritance - and when's inheritance ever a good idea? I thought about this and ...
2
votes
2answers
587 views

One boilerplate class or many similar classes?

Lets say I'm trying to model a variety of objects that are virtually identical, the only difference being their class variables. Am I better off creating one boilerplate class and just calling the ...
4
votes
2answers
867 views

How to replace inheritance with composition in this case?

I've recently read several articles about the advantages of the composition over inheritance. Their authors said that you can always replace inheritance with composition (to be precise, they say they ...
21
votes
1answer
467 views

How are mixins or traits better than plain multiple inheritance?

C++ has plain multiple inheritance, many language designs forbid it as dangerous. But some languages like Ruby and PHP use strange syntax to do the same thing and call it mixins or traits. I heard ...
2
votes
4answers
154 views

Subclass only have methods without variables

Is it ok to have subclass which does not declare its own variables (instance variables) and inherits from super class or base class? It has methods and there are some common methods which it overides ...
2
votes
5answers
327 views

Classic inheritance problem?

I keep seeing the following pattern when people learn about OOP: Problem: How do I put objects of different but related types into a container? Solution: Inherit from a common base class. New ...
1
vote
3answers
215 views

What alternative is better to diagram this scenario?

I was creating and discussing a class diagram with a partner of mine. To simplify things, I've modify the real domain we're working on and made up the following diagram: Basically, a company works ...
0
votes
2answers
285 views

Can an object call a method on it's immediate descendant? [closed]

I have an inheritance hierarchy three deep : an abstract base class, from which I derive another abstract class, call it the intermediate class, from which will derive possibly a large variety of ...
3
votes
4answers
258 views

Why is there no 'subclasses-only' access modifier in Java?

In Java, there are four available access modifiers for methods: public - any class can use this method. protected - classes in the same package and subclasses in any package can use this method. ...
2
votes
3answers
299 views

Inheritance: Is code from superclass virtually *copied* to subclass, or is it *referred-to by subclass*?

Class Sub is a subclass of class Sup. What does that mean practically? Or in other words, what is the practical meaning of "inheritance"? Option 1: The code from Sup is virtually copied to Sub. (as ...
4
votes
2answers
732 views

What is the process of determining which method in a class hierarchy should execute known as?

I thought I understood inheritance and polymorphism, but I was given this question, and I can't, for the life of me, figure out what the proper answer is or what they're trying to get at: The ...
1
vote
3answers
263 views

Why are inheritance and interfaces restricted to instance members?

Disclaimer: I think the rules are almost the same in most OO languages, but since I'm most familiar with C# I'll be relating to this specific language. I think that the use of attributes and ...
1
vote
5answers
173 views

Clarification about Polymorphism / Inheritance

I am trying to better my understanding of polymorphism. Say I have a base class called baseClass with one method called foo() and I have three derived classes called derived1 , derived2 and derived3 ...
-1
votes
1answer
62 views

Concerning JavaScript prototype and the initial constructor function

I am going through a tutorial on OOP in JavaScript (javascriptissexy.com using OOP in JS: What you Need to know). The instructor is explaining the Constructor/Prototype pattern of object creation. I ...
2
votes
1answer
151 views

Using partially implemented interfaces

Consider the following hierarchy in C++ notation: class A { public: virtual void doStuff()=0; virtual void doOtherStuff()=0; }; class B:public A { public: ...
3
votes
2answers
287 views

Base class should have no knowledge of its subtypes?

What's the OO principle that states (in sum): A base object should have no knowledge of its subtypes. I thought it was Liskov Substitution but after reading that wikipedia article I don't ...
33
votes
3answers
1k views

Does Java development typically involve more subclassing than C#/.NET?

I've recently started looking at Android development. This has brought me back into the world of Java software development. The last time I worked with Java, I'll admit, I didn't understand OOP nearly ...
10
votes
12answers
2k views

Is “If a method is re-used without changes, put the method in a base class, else create an interface” a good rule-of-thumb?

A colleague of mine came up with a rule-of-thumb for choosing between creating a base class or an interface. He says: Imagine every new method that you are about to implement. For each of them, ...
2
votes
4answers
499 views

Confusion about inheritance

I know I might get downvoted for this, but I'm really curious. I was taught that inheritance is a very powerful polymorphism tool, but I can't seem to use it well in real cases. So far, I can only ...
0
votes
2answers
436 views

Object inheritance and method parameters/return types - Please check my logic

I'm preparing for a test and doing practice questions, this one in particular I am unsure I did correctly: We are given a very simple UML diagram to demonstrate inheritance: I hope this is clear, it ...
0
votes
2answers
166 views

Python — when should a class have-a rather than be-a?

This is related to "Extends is evil" vs. OCP? but separate because the idea of "implement the interface" doesn't exist in Python. I'm writing a class to pull some data off a webpage. It's ...
3
votes
3answers
173 views

Which one subsumes the other: class-based object-orientation or prototypal inheritance?

In this talk: http://www.youtube.com/watch?v=hQVTIJBZook, Douglas Crockford claims that class-based object-orientation can be represented in terms of prototypal inheritance. The construction he gives ...
1
vote
4answers
564 views

Explanation of the definition of interface inheritance as described in GoF book

I am reading the first chapter of the Gof book. Section 1.6 discusses about class vs interface inheritance: Class versus Interface Inheritance It's important to understand the difference between ...
1
vote
3answers
337 views

Does non-virtual-by-default lead us to composition-over-inheritance?

There are some design guidelines about testable code in “The Art of Unit Testing”. The first one is “Make methods virtual by default”. I’m curious to know your idea about non-virtual-by-default ...
2
votes
5answers
509 views

OOP concept: is it possible to update the class of an instantiated object?

I am trying to write a simple program that should allow a user to save and display sets of heterogeneous, but somehow related data. For clarity sake, I will use a representative example of vehicles. ...
13
votes
3answers
1k views

Is there a specific name for the “Square inherits from Rectangle” paradox?

A certain failure of OOP is shown with a class Square inheriting from Rectangle, where logically Square is a specialization of Rectangle and should therefore inherit from it, but everything falls ...
6
votes
2answers
1k 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 ...
4
votes
5answers
2k 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 ...
0
votes
1answer
1k 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 ...
5
votes
2answers
874 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 ...
24
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
7answers
913 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 ...
-3
votes
2answers
945 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
5k 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
7answers
973 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
565 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 ...
8
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, ...
43
votes
11answers
9k views

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

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 ...
0
votes
2answers
250 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 ...
28
votes
9answers
4k 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? ...
45
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 ...
6
votes
1answer
257 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
781 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 ...
75
votes
14answers
19k 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 ...
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 ...