Questions tagged [super]

super is a keyword or function used to access/invoke members and constructors of a superclass. Since different languages have such a feature, please use in combination with a language tag.

In Object Oriented programming, super is commonly used keyword to allow access to the members of the superclass from a derived class. It can also be used to select which superclass' constructor to execute when a subclass is created.

Although the concept is prevalent in many object oriented languages, every language has different usages for the keyword.

A few of the basic questions on super in different languages:

A superclass is also called base-class or parent-class and some languages use such keywords instead of super.

1567 questions
2768
votes
7 answers

Understanding Python super() with __init__() methods

I'm trying to understand the use of super(). From the looks of it, both child classes can be created, just fine. I'm curious to know about the actual difference between the following 2 child classes. class Base(object): def __init__(self): …
Mizipzor
  • 45,705
  • 20
  • 92
  • 136
798
votes
15 answers

What is PECS (Producer Extends Consumer Super)?

I came across PECS (short for Producer extends and Consumer super) while reading up on generics. Can someone explain to me how to use PECS to resolve confusion between extends and super?
peakit
  • 25,979
  • 25
  • 58
  • 77
626
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 super being used quite…
user25785
  • 6,263
  • 3
  • 16
  • 5
338
votes
7 answers

super() raises "TypeError: must be type, not classobj" for new-style class

The following use of super() raises a TypeError: why? >>> from HTMLParser import HTMLParser >>> class TextParser(HTMLParser): ... def __init__(self): ... super(TextParser, self).__init__() ... self.all_data = [] ... >>>…
Eric O Lebigot
  • 81,422
  • 40
  • 198
  • 249
230
votes
16 answers

super() in Java

Is super() used to call the parent constructor? Please explain super().
Mohan
  • 3,713
  • 9
  • 28
  • 40
225
votes
10 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
  • 14,883
  • 13
  • 57
  • 81
215
votes
4 answers

super() fails with error: TypeError "argument 1 must be type, not classobj" when parent does not inherit from object

I get some error that I can't figure out. Any clue what is wrong with my sample code? class B: def meth(self, arg): print arg class C(B): def meth(self, arg): super(C, self).meth(arg) print C().meth(1) I got the sample…
Ehsan Foroughi
  • 2,800
  • 2
  • 16
  • 19
166
votes
6 answers

Is it unnecessary to put super() in constructor?

Isn't this one automatically put by the compiler if I don't put it in a subclass's constructor? That means I don't even need to care about it? In some articles they put it out. And if I've got one constructor with arguments, will this be the…
ajsie
  • 70,516
  • 97
  • 259
  • 375
160
votes
1 answer

Why is Python 3.x's super() magic?

In Python 3.x, super() can be called without arguments: class A(object): def x(self): print("Hey now") class B(A): def x(self): super().x() >>> B().x() Hey now In order to make this work, some compile-time magic is…
Zero Piraeus
  • 47,176
  • 24
  • 135
  • 148
108
votes
4 answers

Python super() raises TypeError

In Python 2.5, the following code raises a TypeError: >>> class X: def a(self): print "a" >>> class Y(X): def a(self): super(Y,self).a() print "b" >>> c = Y() >>> c.a() Traceback (most recent call last): File…
Geo
  • 85,654
  • 109
  • 315
  • 497
100
votes
13 answers

Java: Calling a super method which calls an overridden method

public class SuperClass { public void method1() { System.out.println("superclass method1"); this.method2(); } public void method2() { System.out.println("superclass method2"); } } public class…
jsonfry
  • 1,947
  • 2
  • 15
  • 16
92
votes
4 answers

super.onCreate(savedInstanceState);

I have created an Android Application Project and in MainActivity.java > onCreate() it is calling super.onCreate(savedInstanceState). As a beginner, can anyone explain what is the purpose of the above line?
Pramod
  • 2,644
  • 6
  • 27
  • 39
88
votes
5 answers

Using super with a class method

I'm trying to learn the super() function in Python. I thought I had a grasp of it until I came over this example (2.6) and found myself…
dza
  • 1,360
  • 1
  • 12
  • 21
86
votes
8 answers

How to force derived class to call super method? (Like Android does)

I was wondering, when creating new Activity classes and then overriding the onCreate() method, in eclipse I always get auto added: super.onCreate(). How does this happen? Is there a java keyword in the abstract or parent class that forces this? I…
Peterdk
  • 14,529
  • 18
  • 97
  • 136
84
votes
11 answers

When do I use super()?

I'm currently learning about class inheritance in my Java course and I don't understand when to use the super() call? Edit: I found this example of code where super.variable is used: class A { int k = 10; } class Test extends A { public…
muttley91
  • 11,995
  • 27
  • 97
  • 144
1
2 3
99 100