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

Initialize an object with a superclass' instance

Let's say a have a Superclass and an instance of this class superclassObject. I create a derived ClassA. How can I instantiate (initialize) an object classAObject of the derived class in a such way, that all the inherited fields are equal to ones of…
adubr
  • 779
  • 7
  • 21
17
votes
5 answers

Constructor overloading - best practice in Java

Constructors can be overloaded as any other method as well and I am aware of that fact. Due to a task I decided to use an abstract superclass with multiple constructors: Abstract Superclass: protected ListSortierer() { this( null, null…
L.Spillner
  • 1,772
  • 10
  • 19
17
votes
1 answer

Delegating constructors: an initializer for a delegating constructor must appear alone

I have a pair of constructors that work just fine in C++03 style. One of the constructors calls a superclass (or base class) constructor ... class Window : public Rectangle { public: Window() : win(new RawWindow(*this)) { refresh();…
learnvst
  • 15,455
  • 16
  • 74
  • 121
16
votes
4 answers

How to call both super(...) and this(...) in case of overloaded constructors?

I've never needed to do this before but since both have to be the 'first' line in the constructor how should one tackle it? What's the best refactoring for a situation like this? Here's a sample: public class Agreement extends Postable { public…
PhD
  • 11,202
  • 14
  • 64
  • 112
16
votes
4 answers

How do I access the super-super class, in Java? [Mini-example inside]

In the example below, how can I access, from C, the method method() of the class A? class A { public void method() { } } class B extends A{ public void method() { } } class C extends B{ public void method() { } void test() { …
John Assymptoth
  • 8,227
  • 12
  • 49
  • 68
16
votes
2 answers

Python 3: super() raises TypeError unexpectedly

Coming from Java, I'm struggling a bit getting down inheritance, abstract classes, static methods and similar concepts of OO programming in Python. I have an implementation of an expression tree class, given (simplified) by # Generic node…
Mate de Vita
  • 1,102
  • 12
  • 32
16
votes
2 answers

How do you call a method for an Objective-C object's superclass from elsewhere?

If you're implementing a subclass, you can, within your implementation, explicitly call the superclass's method, even if you've overridden that method, i.e.: [self overriddenMethod]; //calls the subclass's method [super overriddenMethod]; //calls…
executor21
  • 4,532
  • 6
  • 28
  • 38
16
votes
1 answer

How to call a parent class's @classmethod from an overridden @classmethod in Python?

Let's say I have a class class SimpleGenerator(object): @classmethod def get_description(cls): return cls.name class AdvancedGenerator(SimpleGenerator): @classmethod def get_description(cls): desc =…
Krystian Cybulski
  • 10,789
  • 12
  • 67
  • 98
15
votes
4 answers

Uninstantiable superclass

So, I'm writing a module for connecting to external account providers (Twitter, Facebook etc) and I have a superclass that is useless on its own, but contains generic methods that need to be invoked by the subclasses for persisting auth tokens,…
tiny_mouse
  • 479
  • 2
  • 9
15
votes
2 answers

How do I find the nearest common superclass of two non-interface classes

To find the nearest common superclass, given two non-interface classes a and b, I do the following: static Class findClosestCommonSuper(final Class a, final Class b) { Iterator> pathA = pathFromObject(a).iterator(); …
Alex Salauyou
  • 14,185
  • 5
  • 45
  • 67
15
votes
2 answers

How do I concatenate many objects into one object using inheritance in python? (during runtime)

I have the following classes: class hello(object): def __init__(self): pass class bye(object): def __init__(self): pass l = [hello, bye] If I do the following I get an error: >>> class bigclass(*l): File "", line…
Har
  • 3,727
  • 10
  • 41
  • 75
15
votes
1 answer

Python: RuntimeError: super-class __init__() of %S was never called

I tried to do some operation (setParent) on an object in Python (an instance of a class which inherits from a different class - to be specific, QtGui.QLabel), but during runtime the above error was raised. The object itself has had some fields with…
Tomer
  • 189
  • 1
  • 3
  • 11
14
votes
3 answers

Why protected superclass member cannot be accessed in a subclass function when passed as an argument?

I get a compile error, which I'm slightly confused about. This is on VS2003. error C2248: 'A::y' : cannot access protected member declared in class 'A' class A { public: A() : x(0), y(0) {} protected: int x; int y; }; class B : public…
Chris Bednarski
  • 3,364
  • 25
  • 33
14
votes
3 answers

java, initialize SubClass from SuperClass

public class Base { //long list of attributes // no Constructor using fields // no init methode // i cannot change this class } now i extended the Base Class like: public class subClass extends Base{ private boolean selected; ... …
Rami.Q
  • 2,486
  • 2
  • 19
  • 30
13
votes
3 answers

Do I need to call [super init] or [super initWithCoder], etc for NSObject

Typically when I subclass from a UI class I will call the superclass initializer of interest. However, I'm not sure of the implementation details of NSObject, and it seems like there's not much going on in terms of member vars, so I wonder: do I…
tacos_tacos_tacos
  • 10,277
  • 11
  • 73
  • 126