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

1382 questions
1278
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,263
  • 3
  • 15
  • 9
437
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
  • 54,052
  • 71
  • 166
  • 213
385
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
  • 58,799
  • 16
  • 82
  • 126
338
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
  • 81,422
  • 40
  • 198
  • 249
197
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 A trivial implementation of what I want would…
Confusion
  • 14,331
  • 7
  • 43
  • 71
159
votes
9 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
  • 28,317
  • 26
  • 75
  • 116
135
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
  • 116,521
  • 48
  • 204
  • 258
134
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
  • 3,849
  • 12
  • 41
  • 60
103
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
  • 3,991
  • 5
  • 21
  • 39
97
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,849
  • 7
  • 31
  • 46
80
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
  • 73,807
  • 32
  • 140
  • 214
66
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
  • 681
  • 1
  • 5
  • 4
65
votes
6 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,311
  • 3
  • 22
  • 22
51
votes
4 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
  • 599
  • 2
  • 5
  • 7
49
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
  • 491
  • 1
  • 4
  • 3
1
2 3
92 93