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
24
votes
3 answers

PowerMock: stub methods from parent class

I'm using PowerMock and I'd like to know how to keep all behavior of the child class, but stub super calls that may be overriden by the child. Say I have this class: public class A { public String someMethod() { return "I don't want to…
jchitel
  • 2,999
  • 4
  • 36
  • 49
24
votes
2 answers

Which of the 4 ways to call super() in Python 3 to use?

I wonder when to use what flavour of Python 3 super(). Help on class super in module builtins: class super(object) | super() -> same as super(__class__, ) | super(type) -> unbound super object | super(type, obj) -> bound…
deamon
  • 89,107
  • 111
  • 320
  • 448
24
votes
3 answers

Using Super in an Objective C Category?

I'd like to override a method in an Objective C class that I don't have the source to. I've looked into it, and it appears that Categories should allow me to do this, but I'd like to use the result of the old method in my new method, using super to…
Brad Parks
  • 66,836
  • 64
  • 257
  • 336
23
votes
3 answers

calling init for multiple parent classes with super?

Possible Duplicate: Can Super deal with multiple inheritance? Python inheritance? I have a class structure (below), and want the child class to call the __init__ of both parents. Is this possible to do in a 'super' way or is it just a terrible…
scruffyDog
  • 721
  • 3
  • 7
  • 17
22
votes
3 answers

Java: How to call super method from inner in-place class

I have base class Foo with method spam and class Bar which overrides spam. I need to call spam of base class in method of some callback object which is defined in-place: public class Foo { public void spam() { // ... } } public…
Vlad
  • 1,631
  • 1
  • 16
  • 21
22
votes
5 answers

What does super() method do?

What does the super method do? public DataFetch(Context context) { super(); this.ctx = context; } Does this constructor make the context of the newly created object the context of the super class? Not 100% sure how this works. So would the…
mergesort
  • 5,087
  • 13
  • 38
  • 63
22
votes
1 answer

What is the difference between super() with arguments and without arguments?

I encountered a code which uses the super() method in 2 different ways, and I can't understand what's the difference in the logic. I'm learning right now the pygame module and I got a task to create a class of a Ball which inherits from Sprite…
Jhon Margalit
  • 451
  • 4
  • 12
22
votes
2 answers

super keyword unexpected here

According to ES6 shorthand initialiser, following 2 methods are same: In ES5 var person = { name: "Person", greet: function() { return "Hello " + this.name; } }; In ES6 var person = { name: "Person", greet() { return "Hello " +…
Ratnesh Lal
  • 401
  • 4
  • 8
  • 18
22
votes
1 answer

Python3's super and comprehensions -> TypeError?

Using python3's super in a comprehension seems to always result in TypeError: super(type, obj): obj must be an instance or subtype of type (but using python 2's super does work as expected) class A(object): def __repr__(self): return…
NightShadeQueen
  • 3,284
  • 3
  • 24
  • 37
21
votes
2 answers

Modifying a namedtuple's constructor arguments via subclassing?

I want to create a namedtuple which represents the individual flags in a short bitfield. I'm trying to subclass it so that I can unpack the bitfield before the tuple is created. However, my current attempt isn't working: class…
Ben Blank
  • 54,908
  • 28
  • 127
  • 156
21
votes
2 answers

TypeError: Super does not take Key word arguments?

First, here is my code: class Enemy(): def __init__(self, name, hp, damage): self.name = name self.hp = hp self.damage = damage def is_alive(self): """Checks if alive""" return self.hp > 0 class…
said
  • 474
  • 4
  • 8
  • 18
21
votes
5 answers

super respondsToSelector: returns true but actually calling super (selector) gives "unrecognized selector sent to instance"

OK, I am a little confused. I have a subclass of UIScrollView, which is my attempt at a horizontally scrolling "table view" like UI element. UIScrollView itself sets up UIGestureRecognizers it uses internally, and it appears to set itself up as the…
19
votes
4 answers

Is 'T.super' a legal expression as per JLS?

Consider the following set of expressions: class T {{ /*1*/ Object o = T.super; // error: '.' expected /*2*/ o.toString(); }} An attempt to compile this will fail on line /*1*/ with the error: error: '.' expected o = T.super; …
charlie
  • 1,478
  • 10
  • 20
19
votes
2 answers

Why is super used so much in PySide/PyQt?

Short version (tl;dr) I am learning PySide, and most online tutorials use super to initialize UI elements. Is this important (i.e., more scalable), or is it a matter of taste? Clarification: as I make more clear in the detailed version, this is not…
eric
  • 7,142
  • 12
  • 72
  • 138
18
votes
2 answers

use of python super function in django model

Here's some code in a django tutorial that I'm going through. I've never come across the super function in python before and the way it's used here is different from the examples I've seen online. I.e., usually when you use super, don't you have…
user637965