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
743
votes
11 answers

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.__init__(self) I've seen…
user25785
  • 7,441
  • 3
  • 18
  • 5
608
votes
13 answers

How to determine an object's class?

If class B and class C extend class A and I have an object of type B or C, how can I determine of which type it is an instance?
carrier
  • 32,209
  • 23
  • 76
  • 99
563
votes
5 answers

Ruby: kind_of? vs. instance_of? vs. is_a?

What is the difference? When should I use which? Why are there so many of them?
Claudiu
  • 224,032
  • 165
  • 485
  • 680
538
votes
31 answers

How should I have explained the difference between an Interface and an Abstract class?

In one of my interviews, I have been asked to explain the difference between an Interface and an Abstract class. Here's my response: Methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can…
Thinker
  • 6,820
  • 9
  • 27
  • 54
528
votes
25 answers

When to use an interface instead of an abstract class and vice versa?

This may be a generic OOP question. I wanted to do a generic comparison between an interface and an abstract class on the basis of their usage. When would one want to use an interface and when would one want to use an abstract class?
Chirantan
  • 15,304
  • 8
  • 49
  • 75
506
votes
7 answers

How to invoke the super constructor in Python?

class A: def __init__(self): print("world") class B(A): def __init__(self): print("hello") B() # output: hello In all other languages I've worked with the super constructor is invoked implicitly. How does one invoke it in…
Mike
  • 58,961
  • 76
  • 175
  • 221
483
votes
36 answers

Use of .apply() with 'new' operator. Is this possible?

In JavaScript, I want to create an object instance (via the new operator), but pass an arbitrary number of arguments to the constructor. Is this possible? What I want to do is something like this (but the code below does not work): function…
Prem
  • 15,911
  • 11
  • 31
  • 35
454
votes
13 answers

Is it possible to make abstract classes in Python?

How can I make a class or method abstract in Python? I tried redefining __new__() like so: class F: def __new__(cls): raise Exception("Unable to create an instance of abstract class %s" %cls) But now, if I create a class G that inherits…
user1632861
421
votes
6 answers

Including another class in SCSS

I have this in my SCSS file: .class-a{ display: inline-block; //some other properties &:hover{ color: darken(#FFFFFF, 10%); } } .class-b{ //Inherite class-a here //some properties } In class-b, I would like to inherite all…
F21
  • 32,163
  • 26
  • 99
  • 170
404
votes
8 answers

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("title", getTitle())); } protected abstract…
deamon
  • 89,107
  • 111
  • 320
  • 448
349
votes
11 answers

Calling parent class __init__ with multiple inheritance, what's the right way?

Say I have a multiple inheritance scenario: class A(object): # code for A here class B(object): # code for B here class C(A, B): def __init__(self): # What's the right code to write here to ensure # A.__init__ and…
Adam Parkin
  • 17,891
  • 17
  • 66
  • 87
349
votes
14 answers

Extend data class in Kotlin

Data classes seem to be the replacement to the old-fashioned POJOs in Java. It is quite expectable that these classes would allow for inheritance, but I can see no convenient way to extend a data class. What I need is something like this: open data…
Dmitry
  • 4,232
  • 2
  • 15
  • 13
341
votes
7 answers

Explicitly calling a default method in Java

Java 8 introduces default methods to provide the ability to extend interfaces without the need to modify existing implementations. I wonder if it's possible to explicitly invoke the default implementation of a method when that method has been…
GOTO 0
  • 42,323
  • 22
  • 125
  • 158
320
votes
6 answers

JSP tricks to make templating easier?

At work I've been tasked with turning a bunch of HTML files into a simple JSP project. It's really all static, no serverside logic to program. I should mention I'm completely new to Java. JSP files seem to make it easy to work with common includes…
Scott
  • 4,070
  • 3
  • 21
  • 16
318
votes
7 answers

How can you represent inheritance in a database?

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 many others not common. For example, a commercial…