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
32
votes
2 answers

Python super and setting parent class property

I'm having a really strange problem with Python super() and inheritance and properties. First, the code: #!/usr/bin/env python3 import pyglet import pygame class Sprite(pyglet.sprite.Sprite): def __init__(self, *args, **kwargs): …
darkfeline
  • 9,404
  • 5
  • 31
  • 32
30
votes
4 answers

super() in constructor

I'm reading through some code. In the constructor it has super() but the class implements interface which of course doesn't have a constructor. So which super() it is referring to? public class BoundingBox implements IBoundingVolume { public…
Nazerke
  • 2,098
  • 7
  • 37
  • 57
29
votes
3 answers

Python multi-inheritance, __init__

Regarding multiple parent inheritance, when I call the super.__init__, why doesn't parent2's __init__ function get called? Thanks. class parent(object): var1=1 var2=2 def __init__(self,x=1,y=2): self.var1=x …
Renl
  • 291
  • 1
  • 3
  • 4
29
votes
2 answers

How to use super() with one argument?

While reading the Python documentation on super(), I stumbled on the following statement: If the second argument is omitted, the super object returned is unbound. What does ‘unbound’ mean and how to use super() with one argument?
user3282758
  • 1,379
  • 11
  • 29
29
votes
4 answers

Python 2.x super __init__ inheritance doesn't work when parent doesn't inherit from object

I have the following Python 2.7 code: class Frame: def __init__(self, image): self.image = image class Eye(Frame): def __init__(self, image): super(Eye, self).__init__() self.some_other_defined_stuff() I'm trying to…
cjm2671
  • 18,348
  • 31
  • 102
  • 161
28
votes
1 answer

How to avoid infinite recursion with super()?

I have code like this: class A(object): def __init__(self): self.a = 1 class B(A): def __init__(self): self.b = 2 super(self.__class__, self).__init__() class C(B): def __init__(self): self.c = 3 …
david4dev
  • 4,854
  • 2
  • 28
  • 35
28
votes
3 answers

Java How to call method of grand parents?

Possible Duplicate: Why is super.super.method(); not allowed in Java? Let's assume I have 3 classes A, B and C, each one extending the previous one. How do I call the code in A.myMethod() from C.myMethod() if B also implements myMethod? class…
Arkaha
  • 1,582
  • 3
  • 17
  • 19
28
votes
2 answers

What is a basic example of single inheritance using the super() keyword in Python?

Let's say I have the following classes set up: class Foo: def __init__(self, frob, frotz): self.frobnicate = frob self.frotz = frotz class Bar: def __init__(self, frob, frizzle): self.frobnicate = frob …
lfaraone
  • 49,562
  • 17
  • 52
  • 70
27
votes
1 answer

Constructor overriding

I have a class: class One def initialize; end end I need to create a new class with my own constructor like this: class Two < One def initialize(some) puts some super end end Two.new("thing") but when I launch the code, I got an…
ceth
  • 44,198
  • 62
  • 180
  • 289
26
votes
11 answers

Emulate super in javascript

Basically is there a good elegant mechanism to emulate super with syntax that is as simple as one of the following this.$super.prop() this.$super.prop.apply(this, arguments); Criteria to uphold are : this.$super must be a reference to the…
Raynos
  • 166,823
  • 56
  • 351
  • 396
26
votes
2 answers

'self' used before super.init call

I'm new to swift and I don't understand how to initialize a class. Succeeded is initialized in the class definition as false if (succeeded && (time>1000)){ errormessage += ";connection slow" } Time is initialized as time =…
noɥʇʎԀʎzɐɹƆ
  • 9,967
  • 2
  • 50
  • 67
25
votes
3 answers

Is IntelliJ Python 3 inspection "Expected a dictionary, got a dict" a false positive for super with **kwargs?

I use Python 3 and want to wrap argparse.ArgumentParser with a custom class that sets formatter_class=argparse.RawDescriptionHelpFormatter by default. I can do this successfully, however IntelliJ IDEA 2017.1 with Python Plugin (PyCharm) gives a…
jan
  • 2,741
  • 4
  • 35
  • 56
25
votes
3 answers

Super keyword in Java, interesting behavior, please explain

Lets say that we have the following code: class A { public void doLogic() { System.out.println("doLogic from A"); } } class B extends A { @Override public void doLogic() { System.out.println("doLogic from B"); …
25
votes
3 answers

Using super() in nested classes

Imagine this: class A(object): class B(object): def __init__(self): super(B, self).__init__() This creates an error: NameError: global name B is not defined. I've tried A.B, but then it says that A is not…
Georg Schölly
  • 124,188
  • 49
  • 220
  • 267
24
votes
6 answers

calling a super method from a static method

Is it possible to call a super static method from child static method? I mean, in a generic way, so far now I have the following: public class BaseController extends Controller { static void init() { //init stuff } } public class…
opensas
  • 60,462
  • 79
  • 252
  • 386