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
317
votes
14 answers

Why is it necessary to set the prototype constructor?

In the section about inheritance in the MDN article Introduction to Object Oriented Javascript, I noticed they set the prototype.constructor: // correct the constructor pointer because it points to Person Student.prototype.constructor = Student; …
trinth
  • 5,919
  • 9
  • 40
  • 45
316
votes
8 answers

Inheriting constructors

Why does this code: class A { public: explicit A(int x) {} }; class B: public A { }; int main(void) { B *b = new B(5); delete b; } Result in these errors: main.cpp: In function ‘int main()’: main.cpp:13: error: no matching…
Sydius
  • 13,567
  • 17
  • 59
  • 76
297
votes
3 answers

Chain-calling parent initialisers in python

Consider this - a base class A, class B inheriting from A, class C inheriting from B. What is a generic way to call a parent class initialiser in an initialiser? If this still sounds too vague, here's some code. class A(object): def…
shylent
  • 10,076
  • 6
  • 38
  • 55
296
votes
5 answers

Benefits of prototypal inheritance over classical?

So I finally stopped dragging my feet all these years and decided to learn JavaScript “properly”. One of the most head-scratching elements of the languages design is its implementation of inheritance. Having experience in Ruby, I was really happy…
293
votes
6 answers

Abstract methods in Python

I am having trouble in using inheritance with Python. While the concept seems too easy for me in Java yet up till now I have been unable to understand in Python which is surprising to me at least. I have a prototype which follow: class Shape(): …
user506710
289
votes
8 answers

How to define custom exception class in Java, the easiest way?

I'm trying to define my own exception class the easiest way, and this is what I'm getting: public class MyException extends Exception {} public class Foo { public bar() throws MyException { throw new MyException("try again please"); …
yegor256
  • 102,010
  • 123
  • 446
  • 597
287
votes
21 answers

Do subclasses inherit private fields?

This is an interview question. Does subclasses inherit private fields? I answered "No", because we can't access them using the "normal OOP way". But the interviewer thinks that they are inherited, because we can access such fields indirectly or…
Stan Kurilin
  • 15,614
  • 21
  • 81
  • 132
282
votes
8 answers

What does it mean that Javascript is a prototype based language?

One of the major advantages with Javascript is said to be that it is a prototype based language. But what does it mean that Javascript is prototype based, and why is that an advantage?
Jonas Pegerfalk
  • 9,016
  • 9
  • 29
  • 29
273
votes
14 answers

Difference between new and override

Wondering what the difference is between the following: Case 1: Base Class public void DoIt(); Case 1: Inherited class public new void DoIt(); Case 2: Base Class public virtual void DoIt(); Case 2: Inherited class public override void…
Shiraz Bhaiji
  • 64,065
  • 34
  • 143
  • 252
268
votes
6 answers

How to "perfectly" override a dict?

How can I make as "perfect" a subclass of dict as possible? The end goal is to have a simple dict in which the keys are lowercase. It would seem that there should be some tiny set of primitives I can override to make this work, but according to all…
Paul Biggar
  • 27,579
  • 21
  • 99
  • 152
267
votes
15 answers

How do I get a PHP class constructor to call its parent's parent's constructor?

I need to have a class constructor in PHP call its parent's parent's (grandparent?) constructor without calling the parent constructor. // main class that everything inherits class Grandpa { public function __construct() { } } class…
Paulo
  • 4,275
  • 2
  • 20
  • 20
258
votes
13 answers

Do I really have a car in my garage?

I'm a newbie to Java programming, trying to get the hang of OOP. So I built this abstract class: public abstract class Vehicle{....} and 2 subclasses: public class Car extends Vehicle{....} public class Boat extends Vehicle{....} Car and Boat also…
T-Rex
  • 1,550
  • 2
  • 11
  • 17
258
votes
14 answers

In Python, how do I indicate I'm overriding a method?

In Java, for example, the @Override annotation not only provides compile-time checking of an override but makes for excellent self-documenting code. I'm just looking for documentation (although if it's an indicator to some checker like pylint,…
Bluu
  • 5,226
  • 4
  • 29
  • 34
257
votes
11 answers

Maven project version inheritance - do I have to specify the parent version?

I have two projects: Parent project: A, Sub project: B A/pom.xml: com.dummy.bla parent 0.1-SNAPSHOT pom And in B/pom.xml, I have:
Shengjie
  • 12,336
  • 29
  • 98
  • 139
254
votes
3 answers

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 class base { protected: int x; }; template class derived : public…
Ali
  • 56,466
  • 29
  • 168
  • 265