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
91
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
  • 15,625
  • 20
  • 101
  • 140
88
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
  • 12,278
  • 33
  • 106
  • 160
84
votes
4 answers

super() and @staticmethod interaction

Is super() not meant to be used with staticmethods? When I try something like class First(object): @staticmethod def getlist(): return ['first'] class Second(First): @staticmethod def getlist(): l = super(Second).getlist() …
Ben J
  • 2,252
  • 4
  • 23
  • 30
77
votes
9 answers

What is a real life example of generic ?

I understand that represents any super class of T (parent class of T of any level). But I really struggle to imagine any real life example for this generic bound wildcard. I understand what means and I have seen this…
BanzaiTokyo
  • 1,376
  • 4
  • 17
  • 33
70
votes
5 answers

How to call super method from grandchild class?

I am working with some code that has 3 levels of class inheritance. From the lowest level derived class, what is the syntax for calling a method 2 levels up the hierarchy, e.g. a super.super call? The "middle" class does not implement the method I…
SeanLabs
  • 1,739
  • 4
  • 18
  • 22
70
votes
6 answers

Bounding generics with 'super' keyword

Why can I use super only with wildcards and not with type parameters? For example, in the Collection interface, why is the toArray method not written like this interface Collection{ S[] toArray(S[] a); }
mohsenof
  • 711
  • 1
  • 6
  • 5
64
votes
3 answers

Python's Multiple Inheritance: Picking which super() to call

In Python, how do I pick which Parent's method to call? Say I want to call the parent ASDF2's __init__ method. Seems like I have to specify ASDF1 in the super()..? And if I want to call ASDF3's __init__, then I must specify ASDF2?! >>> class…
Name McChange
  • 2,750
  • 5
  • 27
  • 46
59
votes
2 answers

Python super method and calling alternatives

I see everywhere examples that super-class methods should be called by: super(SuperClass, instance).method(args) Is there any disadvantage to doing: SuperClass.method(instance, args)
IanSR
  • 1,415
  • 3
  • 16
  • 18
58
votes
5 answers

How does Python's "super" do the right thing?

I'm running Python 2.5, so this question may not apply to Python 3. When you make a diamond class hierarchy using multiple inheritance and create an object of the derived-most class, Python does the Right Thing (TM). It calls the constructor for the…
Joseph Garvin
  • 20,727
  • 18
  • 94
  • 165
57
votes
5 answers

Python, Overriding an inherited class method

I have two classes, Field and Background. They look a little bit like this: class Field( object ): def __init__( self, a, b ): self.a = a self.b = b self.field = self.buildField() def buildField( self ): …
d00nut
  • 673
  • 1
  • 5
  • 6
54
votes
1 answer

Equivalent of Super Keyword in C#

What is the equivalent c# keyword of super keyword (java). My java code : public class PrintImageLocations extends PDFStreamEngine { public PrintImageLocations() throws IOException { super( ResourceLoader.loadProperties(…
Ganeshja
  • 2,675
  • 12
  • 36
  • 57
52
votes
2 answers

When calling super() in a derived class, can I pass in self.__class__?

I've recently discovered (via StackOverflow) that to call a method in a base class I should call: super([[derived class]], self).[[base class method]]() That's fine, it works. However, I find myself often copying and pasting between classes when I…
Simon Elms
  • 17,832
  • 21
  • 87
  • 103
51
votes
4 answers

What exactly is super in Objective-C?

As far as I know, it's a pointer to the superclass. It's hard-wired with the superclass, and not dynamically figured out at runtime. Would like to know it more in detail... Anyone?
dontWatchMyProfile
  • 45,440
  • 50
  • 177
  • 260
48
votes
5 answers

Is super() broken in Python-2.x?

It's often stated that super should be avoided in Python 2. I've found in my use of super in Python 2 that it never acts the way I expect unless I provide all arguments such as the example: super(ThisClass, self).some_func(*args, **kwargs) It seems…
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
47
votes
2 answers

Python super() arguments: why not super(obj)?

I am trying to understand when and how to use super() in Python correctly (either 2.7.x or 3.x) on >>> help(super) the interpreter tells me how to call it: class super(object) | super(type) -> unbound super object | super(type, obj) -> bound…
Gabriel
  • 1,078
  • 1
  • 8
  • 15