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.

1757 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
895
votes
16 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
  • 28,597
  • 27
  • 63
  • 80
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
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
343
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
  • 91,433
  • 48
  • 218
  • 260
238
votes
18 answers

super() in Java

Is super() used to call the parent constructor? Please explain super().
Mohan
  • 3,893
  • 9
  • 33
  • 42
234
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
  • 3,010
  • 2
  • 18
  • 20
186
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
  • 77,632
  • 106
  • 276
  • 381
162
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
  • 56,143
  • 27
  • 150
  • 160
112
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
  • 2,067
  • 2
  • 15
  • 16
109
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
  • 93,257
  • 117
  • 344
  • 520
101
votes
4 answers

python multiple inheritance passing arguments to constructors using super

Consider the following snippet of python code class A(object): def __init__(self, a): self.a = a class B(A): def __init__(self, a, b): super(B, self).__init__(a) self.b = b class C(A): def __init__(self, a, c): …
Lin
  • 1,547
  • 2
  • 12
  • 26
101
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,478
  • 2
  • 13
  • 24
98
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,828
  • 6
  • 31
  • 40
95
votes
12 answers

Getting the name of a sub-class from within a super-class

Let's say I have a base class named Entity. In that class, I have a static method to retrieve the class name: class Entity { public static String getClass() { return Entity.class.getClass(); } } Now I have another class extend…
Matt Huggins
  • 81,398
  • 36
  • 149
  • 218
1
2 3
99 100