Questions tagged [superclass]

A superclass is a parent or base class that is derived or inherited from by a child class (or subclass). Superclasses are used extensively in object-oriented programming (OOP).

SuperClass

A superclass is a class that has been extended by another class. It allows the extending class to inherit its state and behaviors.

Example

In this example, polygon is a superclass of triangle and rectangle:

enter image description here

Reference

1474 questions
1303
votes
13 answers

'Must Override a Superclass Method' Errors after importing a project into Eclipse

Anytime I have to re-import my projects into Eclipse (if I reinstalled Eclipse, or changed the location of the projects), almost all of my overridden methods are not formatted correctly, causing the error: The method must override a superclass…
Tim H
  • 13,513
  • 3
  • 17
  • 9
506
votes
7 answers

How to invoke the super constructor in Python?

class A: def __init__(self): print("world") class B(A): def __init__(self): print("hello") B() # output: hello In all other languages I've worked with the super constructor is invoked implicitly. How does one invoke it in…
Mike
  • 58,961
  • 76
  • 175
  • 221
423
votes
22 answers

Why is super.super.method(); not allowed in Java?

I read this question and thought that would easily be solved (not that it isn't solvable without) if one could write: @Override public String toString() { return super.super.toString(); } I'm not sure if it is useful in many cases, but I…
Tim Büthe
  • 62,884
  • 17
  • 92
  • 129
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
215
votes
2 answers

Test whether a Ruby class is a subclass of another class

I would like to test whether a class inherits from another class, but there doesn't seem to exist a method for that. class A end class B < A end B.is_a? A => false B.superclass == A => true (N.B. the is_a? test above does not make any sense, as…
Confusion
  • 16,256
  • 8
  • 46
  • 71
185
votes
11 answers

Why aren't superclass __init__ methods automatically invoked?

Why did the Python designers decide that subclasses' __init__() methods don't automatically call the __init__() methods of their superclasses, as in some other languages? Is the Pythonic and recommended idiom really like the following? class…
jrdioko
  • 32,230
  • 28
  • 81
  • 120
153
votes
7 answers

Should __init__() call the parent class's __init__()?

I'm used that in Objective-C I've got this construct: - (void)init { if (self = [super init]) { // init class } return self; } Should Python also call the parent class's implementation for __init__? class…
Georg Schölly
  • 124,188
  • 49
  • 220
  • 267
146
votes
5 answers

Inheritance and Overriding __init__ in python

I was reading 'Dive Into Python' and in the chapter on classes it gives this example: class FileInfo(UserDict): "store file metadata" def __init__(self, filename=None): UserDict.__init__(self) self["name"] = filename The…
liewl
  • 4,021
  • 13
  • 46
  • 65
142
votes
7 answers

How do I call a super constructor in Dart?

How do I call a super constructor in Dart? Is it possible to call named super constructors?
Eduardo Copat
  • 4,941
  • 5
  • 23
  • 40
97
votes
8 answers

Why call super() in a constructor?

I'm dealing with a class which extends JFrame. It's not my code and it makes a call to super before it begins constructing the GUI. I'm wondering why this is done since I've always just accessed the methods of the superclass without having to call…
mark
  • 2,841
  • 4
  • 25
  • 23
96
votes
3 answers

When do you need to explicitly call a superclass constructor?

So say I have a subclass that extends a superclass. In what scenarios do I need to explicitly type super() to get the superclass constructor to run? I'm looking at an example in a book about abstract classes and when they extend it with a…
jhlu87
  • 3,999
  • 8
  • 38
  • 48
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
77
votes
7 answers

Java Inheritance - calling superclass method

Lets suppose I have the following two classes public class alpha { public alpha(){ //some logic } public void alphaMethod1(){ //some logic } } public class beta extends alpha { public beta(){ //some…
roz
  • 791
  • 1
  • 5
  • 4
60
votes
5 answers

How to determine if an object is an instance of certain derived C++ class from a pointer to a base class in GDB?

I'm debugging a C++ program with GDB. I have a pointer to an object of certain class. The pointer is declared to be of some super class which is extended by several sub-classes. There is no fields in the object to specify the precise class type of…
user1101096
  • 699
  • 2
  • 6
  • 7
55
votes
8 answers

How to call a superclass method using Java reflection

I have two classes: public class A { public Object method() {...} } public class B extends A { @Override public Object method() {...} } I have an instance of B. How do I call A.method() from b? Basically, the same effect as calling…
Ted
  • 551
  • 1
  • 4
  • 3
1
2 3
98 99