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.

Filter by
Sorted by
Tagged with
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
1994 votes
36 answers
495k 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
1857 votes
10 answers
1.4m views

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 ...
lomaxx's user avatar
  • 115k
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
1693 votes
6 answers
449k 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
1561 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.7k
1262 votes
17 answers
783k 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
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
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
  • 82k
898 votes
17 answers
612k views

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's user avatar
868 votes
10 answers
1.1m views

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 ...
levik's user avatar
  • 117k
822 votes
19 answers
1.3m views

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's user avatar
  • 11.2k
817 votes
16 answers
934k views

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 ...
jjohn's user avatar
  • 10k
795 votes
9 answers
992k views

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 ...
IaCoder's user avatar
  • 12.6k
754 votes
11 answers
303k 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
616 votes
14 answers
853k views

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's user avatar
  • 32.7k
571 votes
5 answers
244k views

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's user avatar
  • 227k
546 votes
25 answers
432k views

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 ...
Chirantan's user avatar
  • 15.6k
542 votes
31 answers
480k views

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

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 ...
Thinker's user avatar
  • 6,872
519 votes
7 answers
408k views

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 ...
Mike's user avatar
  • 60.1k
483 votes
36 answers
85k views

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 (...
Prem's user avatar
  • 16.1k
460 votes
13 answers
301k views

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 %...
user avatar
436 votes
6 answers
402k views

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 ...
F21's user avatar
  • 33k
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
365 votes
15 answers
244k views

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 ...
Dmitry's user avatar
  • 4,392
364 votes
11 answers
199k views

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 ...
Adam Parkin's user avatar
  • 18.4k
356 votes
7 answers
110k views

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 ...
GOTO 0's user avatar
  • 46k
330 votes
8 answers
392k views

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 ...
Sydius's user avatar
  • 14k
330 votes
7 answers
162k 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
322 votes
6 answers
134k views

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 ...
Scott's user avatar
  • 4,100
317 votes
15 answers
63k views

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 ...
trinth's user avatar
  • 6,019
306 votes
6 answers
354k views

Abstract methods in Python [duplicate]

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 ...
user avatar
298 votes
5 answers
86k views

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....
Pierreten's user avatar
  • 10.1k
298 votes
3 answers
180k views

Chain-calling parent initialisers in python [duplicate]

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 ...
shylent's user avatar
  • 10.1k
290 votes
8 answers
412k views

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 { ...
yegor256's user avatar
  • 104k
288 votes
21 answers
277k views

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 ...
Stan Kurilin's user avatar
  • 15.7k
285 votes
8 answers
69k views

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's user avatar
284 votes
14 answers
128k views

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(); ...
Shiraz Bhaiji's user avatar
272 votes
14 answers
165k views

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 ...
Bluu's user avatar
  • 5,426
271 votes
6 answers
184k views

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 ...
Paul Biggar's user avatar
  • 28.4k
269 votes
15 answers
319k views

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 { ...
Paulo's user avatar
  • 4,293
268 votes
11 answers
298k views

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: <groupId>com.dummy.bla</groupId> <artifactId>parent</artifactId> <version>0.1-SNAPSHOT</version> ...
Shengjie's user avatar
  • 12.6k
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
258 votes
13 answers
28k views

Do I really have a car in my garage? [duplicate]

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 ...
T-Rex's user avatar
  • 1,550
251 votes
17 answers
264k views

Difference between Inheritance and Composition

Are Composition and Inheritance the same? If I want to implement the composition pattern, how can I do that in Java?
gmhk's user avatar
  • 15.8k
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
251 votes
6 answers
239k views

Struct inheritance in C++

Can a struct be inherited in C++?
user avatar
249 votes
8 answers
101k views

Why is it not possible to extend annotations in Java?

I don't understand why there is no inheritance in Java annotations, just as Java classes. I think it would be very useful. For example: I want to know if a given annotation is a validator. With ...
sinuhepop's user avatar
  • 20.2k
248 votes
13 answers
180k views

Why can't I inherit static classes?

I have several classes that do not really need any state. From the organizational point of view, I would like to put them into hierarchy. But it seems I can't declare inheritance for static classes. ...
User's user avatar
  • 30.8k

1
2 3 4 5
855