Skip to main content

Questions tagged [inheritance]

Inheritance is the system in object oriented programming that allows objects to support operations defined by anterior types without having to provide their own definition. It is the major vector for polymorphism in object-oriented programming.

919 votes
17 answers
270k views

What is object slicing?

In C++, what is object slicing and when does it occur?
Frankomania's user avatar
  • 9,247
910 votes
19 answers
141k views

Is List<Dog> a subclass of List<Animal>? Why are Java generics not implicitly polymorphic?

I'm a bit confused about how Java generics handle inheritance / polymorphism. Assume the following hierarchy - Animal (Parent) Dog - Cat (Children) So suppose I have a method doSomething(List<...
froadie's user avatar
  • 82.1k
140 votes
2 answers
18k views

Prototypical inheritance - writing up [duplicate]

So I have these 2 examples, from javascript.info: Example 1: var animal = { eat: function() { alert( "I'm full" ) this.full = true } } var rabbit = { jump: function() { /* something */...
frrlod's user avatar
  • 6,609
1995 votes
36 answers
496k views

Prefer composition over inheritance?

Why prefer composition instead of inheritance? What trade-offs are there for each approach? And the converse question: when should I choose inheritance instead of composition?
readonly's user avatar
  • 351k
1051 votes
3 answers
157k views

What are the nuances of scope prototypal / prototypical inheritance in AngularJS?

The API Reference Scope page says: A scope can inherit from a parent scope. The Developer Guide Scope page says: A scope (prototypically) inherits properties from its parent scope. So, does a ...
Mark Rajcok's user avatar
330 votes
7 answers
163k views

How can you represent inheritance in a database? [closed]

I'm thinking about how to represent a complex structure in a SQL Server database. Consider an application that needs to store details of a family of objects, which share some attributes, but have ...
Steve Jones's user avatar
  • 3,505
266 votes
3 answers
33k views

Why do I have to access template base class members through the this pointer?

If the classes below were not templates I could simply have x in the derived class. However, with the code below, I have to use this->x. Why? template <typename T> class base { protected: ...
Ali's user avatar
  • 57.8k
3234 votes
7 answers
2.8m views

Understanding Python super() with __init__() methods [duplicate]

Why is super() used? Is there a difference between using Base.__init__ and super().__init__? class Base(object): def __init__(self): print "Base created" class ChildA(...
Mizipzor's user avatar
  • 51.9k
405 votes
8 answers
149k 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() { add(new Label("...
deamon's user avatar
  • 91.3k
1727 votes
29 answers
253k views

Why not inherit from List<T>?

When planning out my programs, I often start with a chain of thought like so: A football team is just a list of football players. Therefore, I should represent it with: var football_team = new ...
Superbest's user avatar
  • 26.2k
1562 votes
8 answers
894k views

What are the differences between type() and isinstance()? [duplicate]

What are the differences between these two code snippets? Using type: import types if type(a) is types.DictType: do_something() if type(b) in types.StringTypes: do_something_else() Using ...
abbot's user avatar
  • 27.8k
1263 votes
17 answers
784k views

What is the difference between public, private, and protected inheritance?

What is the difference between public, private, and protected inheritance in C++?
user avatar
754 votes
11 answers
304k views

What does 'super' do in Python? - difference between super().__init__() and explicit superclass __init__()

What's the difference between: class Child(SomeBaseClass): def __init__(self): super(Child, self).__init__() and: class Child(SomeBaseClass): def __init__(self): SomeBaseClass....
user25785's user avatar
  • 7,551
251 votes
15 answers
150k views

Convert List<DerivedClass> to List<BaseClass>

While we can inherit from base class/interface, why can't we declare a List<> using same class/interface? interface A { } class B : A { } class C : B { } class Test { static void Main(...
Asad's user avatar
  • 21.8k
1693 votes
6 answers
450k views

Why do Python classes inherit object?

Why does the following class declaration inherit from object? class MyClass(object): ...
tjvr's user avatar
  • 17.8k

15 30 50 per page
1
2 3 4 5
306