Questions tagged [inheritance]

One of the four pillars of object-oriented programming (OOP), inheritance establishes a "is-a" relationship between objects. For example, a "Cat" object can be treated in code as any other "Animal" object because a "Cat" is-a "Animal".

Filter by
Sorted by
Tagged with
33
votes
5answers
143k views

Implement a Shape abstract class

To learn more about OOP, @nhgrif challenged me to implement a Shape abstract class (more details in code remarks below. Here is how I did it. Any and all advice appreciated! Shape.java ...
22
votes
5answers
6k views

Modelling a Call Center

This is the requirement I have (from the book: Cracking the Coding Interview) Imagine you have a call center with three levels of employees: fresher, technical lead (TL), and product manager (PM). ...
19
votes
2answers
3k views

A Python wrap-around list

I want to gain experience creating data structures that look and feel like Python builtin types. As a first exercise, I've written a WraparoundList class meant to ...
15
votes
2answers
2k views

Encapsulated text-based RPG using a randomized combat system

For my rags-to-riches submission, I've decided to improve this code: Text-based RPG game using classes However, I've decided to start off with something different. The original code is more ...
14
votes
11answers
1k views

Design dilemma: extensibility vs simplicity

Here is my problem abstracted to Bird classes. I know that number of Birds will increase on the future and new behaviors might ...
14
votes
5answers
3k views

Mini RPG equipment code

what do you think about this code? It's upgraded version of: Classes representing items in an RPG game ...
14
votes
4answers
2k views

Reusability vs simplicity in a small game with a set of interfaces

I have a small game I'm working on with a set of interfaces: IHavePosition: ...
13
votes
2answers
834 views

Dynamic memory management for a class hierarchy of geometric shapes

The task was to write function that compare areas of two random generated geometric shapes (circle, square, rectangle) using base class with virtual function. Am I doing it right in terms of memory ...
11
votes
5answers
2k views

A simple program with trains and inheritance

Here it is my program. I'm especially worried about the main because I know that one must be careful while instancing vectors of "inherited" objects. The code I have works fine (as far as I can see) ...
11
votes
2answers
4k views

Decomposing the animal kingdom

I have implemented a Mammal class hierarchy in Java. Is it an intelligent approach, with respect to decomposition, locality and procedural abstraction? ...
11
votes
1answer
185 views

DataLayer Interfaces Genericized

I have some concerns on my refactoring for the data layer in my current Project. Just for a small info, I am currently building a CRM as "training JEE application". But enough of talk, let's talk code!...
10
votes
6answers
3k views

Fluent interface and polymorphism for building a scene with shapes

I would like to improve the interfaces of some polymorphic classes going from positional to named parameters and I came up with the fluent interface. The following is the most clean, compact and ...
10
votes
3answers
641 views

General purpose replacement for enum with FlagsAttribute

Enums with the FlagsAttribute have the disadvantage that you need to be careful when assigning their values. They are also inconvenient when you would like to allow ...
10
votes
1answer
221 views

Implementing a Maybe for multidimensional data

The scenario is we're working with a REST endpoint that gives us nice JSON objects. We're using requests, and everything works wonderfully. But one day you notice that data you though was always being ...
10
votes
1answer
2k views

Static Multilevel Inheritance with CRTP (Fast Intrusive Pointers)

I wanted to have a way to be able to static_cast to the proper derived type pointer of a base class to simulate a virtual call. Here the virtual call is for a ...
10
votes
1answer
666 views

Subclassing pathlib.Path

This arises from the SO Question subclassing pathlib.Path. The pathlib sources show that Path, when invoked, selects one of ...
9
votes
3answers
1k views

Why would I want to always have to explicitly call a “base” method if I just want to use base functionality?

I recently worked with an architect who structured his base class like this: ...
9
votes
1answer
2k views

OOP modeling of a boat rental system

I have some question about this exercise, did I model the problem correctly (the code works)?. If I did it correctly, Is there anything that can improve the code?. For example, how could I avoid the ...
8
votes
4answers
6k views

Basic Postfix Calculator in Java

I recently posted some sample code that I was providing students with and got great feedback so figured I post another one of the examples I will be providing students with. (See Simple Example of an ...
8
votes
1answer
2k views

Creating instances of all subclasses in Python

I have a superclass with several subclasses. Each subclass overrides the "solve" method with some a different algorithm for estimating the solution to a particular problem. I'd like to efficiently ...
8
votes
1answer
15k views

Calculate area and perimeter for shapes: polygon, circle, rectangle and triangle

This is my project on inheritance and polymorphism. I have the main file from my teacher and built the project base on it. My base class is Shape. ...
8
votes
2answers
5k views

Class for printing class hierarchy as text

I'm coding a little class hierarchy printing tool for easy show hierarchies of java classes. This is my current code: ...
8
votes
2answers
2k views

Video Store Rental Application

I'm in the middle of developing a video store rental application for a school project but am starting to question whether I'm going about things in the correct manner. Although the application is ...
8
votes
1answer
507 views

Creating Inheritance hierarchy using function constructor

Problem statement You need to create the Animal base class having four fields: a. name b. ...
8
votes
1answer
535 views

Multiple inheritance with JavaScript with support of calling of super() methods

I have just finished working on a Node.JS module that provides "multiple inheritance that works". I had some really specific requirements: Works in safe mode Auto-calling of parent constructors Makes ...
8
votes
1answer
7k views

Solar System model: Moving Space Balls

I have a model of the solar system that I'm working on at the moment. Now is the time to start cleaning my code up a little. Here's what I have so far: A main 'driver' method: ...
8
votes
1answer
822 views

Extending the functionality of lxml.etree

I wrote a class to slightly customize the behavior of lxml.etree.ElementTree and I use it quite extensively. It works great, but there are a few methods that I'm ...
8
votes
1answer
215 views

Polymorphic animals speak their name and make a noise

I'm new to GNU Smalltalk. I'd like to port a script I've written in Ruby, Scala, CoffeeScript, and several others. It's the one I use to try to learn the classic OOP concepts of abstract classes, ...
8
votes
2answers
142 views

Inheriting methods of an immutable type

In my project, I have a type BalanceByBucket that is an immutable type and has a bunch of methods. I have another class called a ...
8
votes
2answers
2k views

Recorder for keyboard and mouse events

I'm building an event recorder. So far the code looks clean, but I feel like I'm missing something that can make this code even better. Is composition a better solution? If yes, how can I do it ...
7
votes
4answers
2k views

Armor and Weapon classes for a game

I have a 2 classes Armor and Weapon in my game, that inherit from the abstract class Gear: <...
7
votes
2answers
5k views

Dispose pattern - DisposableObject

I am trying to make an universal implementation of IDisposable (as a base class): ...
7
votes
1answer
6k views

Case class design in Scala

In the Java world while writing the data access layer (for CRUD) and the model layer, I have done something like this: ...
7
votes
2answers
571 views

Java - Something similar to Abstract Factory?

I have many repositories stored in a map. User chooses some of them and then a downloading begins. Repository may be one of 4 types (see code below). ...
7
votes
3answers
2k views

Create dictionary with default immutable keys

I've created a dictionary subclass with a set of default immutable keys. ...
7
votes
2answers
553 views

Layer Stack class to practice std::shared_ptr

The following three source files is to define and test a class StackLayer. While it was written in a need for scalable layer-based architecture design, it was also ...
7
votes
3answers
10k views

Raising error if method not overridden by sub-class

Background I have a base (only 2 classes inherit from it, and only from it) abstract (meaning I don't want it to be used directly) class that implements some common functionality. Some of it depends ...
7
votes
2answers
242 views

Using a CRTP approach for loading OpenGL programs

I wanted to try using CRTP (also new for me) to try making the loading of shared resources implicit. (Specifically I'm using it for loading OpenGl shader programs) This will also help separate some ...
7
votes
1answer
4k views

Optional base class template to get conditional data members

In generic code, I sometimes want to conditionally add a data member to a class template. Writing separate template specializations scales as 2^N for ...
7
votes
1answer
366 views

Moving method from derived class to base

I've some classes (Derived1, Derived2, etc.) derived from class Base. All of derived classes ...
7
votes
1answer
175 views

Multiple inheritance pattern for vehicle information

I wanted an easy way to augment objects, by adding functionality from any other object(s). More importantly, I needed a way to augment the object from multiple sources in a clean one-line solution. ...
7
votes
1answer
175 views

Out of order class resequencer

I got bored recently and wrote a tool to re-order out of order classes in Python files. This can occur with some auto-generation code tools for databases and the like; these files can sometimes be ...
6
votes
5answers
753 views

Initializing JTree

I have a class called Piece, and many many subclasses of Piece. I want to add an instance of every single subclass of ...
6
votes
2answers
3k views

Simple PHP inheritance using an abstract class

I have created simple class inheritance of an abstract class Pet which is basically the skeleton of all other children classes. ...
6
votes
4answers
749 views

Better way to add fields to superclass in Java

I'm making my first steps in OOP and I'm currently doing some exercises. I've done a simple program with class structure like this (I'll provide only essential parts): Super class: ...
6
votes
1answer
584 views

Creature generator

Originally posted this here, and it was helpfully suggested to post in this forum. In the past couple of years I've returned part time to programming after a 15 year gap. I was C/UNIX. So, I've ...
6
votes
3answers
191 views

Object inheritance

I have written some example code to test object inheritance, but I'm not sure if it's really the best way for an object to inherit another's functions (like Java's ...
6
votes
2answers
122 views

“Simple” generic inheritance

I'm sure that my intentions are highly erroneous, and I really just want to see what you guys would suggest I do instead. I have a class I made for my game which helps store various values regarding ...
6
votes
1answer
262 views

Code Golf challenge that plays Mafia

I am working on writing a Code Golf King of the Hill challenge, the details of which can be read here. You can read the full code here. Briefly, the game proceeds in cycles of night then day. There ...
6
votes
2answers
526 views

System for registering people

For a small administrative program, I have to be able to register people and their data. But, files are sometimes created through a phone call "on the fly" and then later certain data is added to the ...