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.

Inheritance is the main method by which object-oriented systems provide polymorphism.

Where a class Sub inherits from another class (or other object, as in Self or JavaScript) Base, Sub will share some or all of the operations (possibly including data access and storage) provided by Base. It is usually the case that Sub will receive at least the full public interface of Base in order to allow any object of type Sub to stand in place of an object of type Base in any code written to work on objects of type Base (refer to the Liskov Substitution Principle).

This facility is orthogonal to the type of type system used, the function binding regime (whether late or early), whether or not there is a privacy regime, or indeed the evaluation (whether lazy or weak).

Example of inheritance on a UML diagram:

enter image description here

See Inheritance on Wikipedia.

42250 questions
3182
votes
7 answers

Understanding Python super() with __init__() methods

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(Base): def __init__(self): Base.__init__(self) …
Mizipzor
  • 51,151
  • 22
  • 97
  • 138
1962
votes
35 answers

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
  • 343,444
  • 107
  • 203
  • 205
1829
votes
10 answers

Calling the base constructor in C#

If I inherit from a base class and want to pass something from the constructor of the inherited class to the constructor of the base class, how do I do that? For example, if I inherit from the Exception class I want to do something like this: class…
lomaxx
  • 113,627
  • 57
  • 144
  • 179
1694
votes
28 answers

Why not inherit from List?

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 List(); The ordering of this list…
Superbest
  • 25,318
  • 14
  • 62
  • 134
1656
votes
6 answers

Why do Python classes inherit object?

Why does the following class declaration inherit from object? class MyClass(object): ...
tjvr
  • 17,431
  • 6
  • 25
  • 26
1540
votes
8 answers

What are the differences between type() and isinstance()?

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 isinstance: if isinstance(a, dict): …
abbot
  • 27,408
  • 6
  • 54
  • 57
1234
votes
17 answers

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

What is the difference between public, private, and protected inheritance in C++? All of the questions I've found on SO deal with specific cases.
user106599
1054
votes
3 answers

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 child scope always prototypically inherit from its…
Mark Rajcok
  • 362,217
  • 114
  • 495
  • 492
905
votes
18 answers

What is object slicing?

In c++ what is object slicing and when does it occur?
Frankomania
  • 9,115
  • 3
  • 17
  • 3
896
votes
19 answers

Is List a subclass of List? 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 animals). By all the rules of inheritance and…
froadie
  • 79,995
  • 75
  • 166
  • 235
883
votes
17 answers

How do you declare an interface in C++?

How do I setup a class that represents an interface? Is this just an abstract base class?
Aaron Fischer
  • 20,853
  • 18
  • 75
  • 116
846
votes
10 answers

What are the rules for calling the base class constructor?

What are the C++ rules for calling the base class constructor from a derived class? For example, I know in Java, you must do it as the first line of the subclass constructor (and if you don't, an implicit call to a no-arg super constructor is…
levik
  • 114,835
  • 27
  • 73
  • 90
810
votes
19 answers

Implements vs extends: When to use? What's the difference?

Please explain in an easy to understand language or a link to some article.
Saad Masood
  • 11,036
  • 9
  • 32
  • 40
788
votes
16 answers

How do I call a parent class's method from a child class in Python?

When creating a simple object hierarchy in Python, I'd like to be able to invoke methods of the parent class from a derived class. In Perl and Java, there is a keyword for this (super). In Perl, I might do this: package Foo; sub frotz { …
jjohn
  • 9,708
  • 4
  • 20
  • 21
767
votes
7 answers

How to call a parent class function from derived class function?

How do I call the parent function from a derived class using C++? For example, I have a class called parent, and a class called child which is derived from parent. Within each class there is a print function. In the definition of the child's print…
IaCoder
  • 12,300
  • 11
  • 37
  • 45
1
2 3
99 100