Questions tagged [subclass]

A subclass is a class that derives or inherits from a parent (or super) class. Subclassing is used extensively in object-oriented programming (OOP).

A subclass is any class that inherits from a parent (or super/base) class. Depending on the language, a subclass will have a subset of the attributes and methods of the (parent/base) class from which it inherits. As a result, it is important to always tag a question with the language being used when tagging it with this tag

4137 questions
406
votes
5 answers

How do I check if a type is a subtype OR the type of an object?

To check if a type is a subclass of another type in C#, it's easy: typeof (SubClass).IsSubclassOf(typeof (BaseClass)); // returns true However, this will fail: typeof (BaseClass).IsSubclassOf(typeof (BaseClass)); // returns false Is there any way…
Daniel T.
  • 37,212
  • 36
  • 139
  • 206
321
votes
11 answers

How to find all the subclasses of a class given its name?

I need a working approach of getting all classes that are inherited from a base class in Python.
Roman Prykhodchenko
  • 12,655
  • 8
  • 29
  • 33
260
votes
10 answers

How do I check (at runtime) if one class is a subclass of another?

Let's say that I have a class Suit and four subclasses of suit: Heart, Spade, Diamond, Club. class Suit: ... class Heart(Suit): ... class Spade(Suit): ... class Diamond(Suit): ... class Club(Suit): ... I have a method which receives…
snakile
  • 52,936
  • 62
  • 169
  • 241
237
votes
19 answers

How do you find all subclasses of a given class in Java?

How does one go about and try to find all subclasses of a given class (or all implementors of a given interface) in Java? As of now, I have a method to do this, but I find it quite inefficient (to say the least). The method is: Get a list of all…
Avrom
  • 4,967
  • 2
  • 28
  • 35
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
213
votes
3 answers

Subclass in type hinting

I want to allow type hinting using Python 3 to accept sub classes of a certain class. E.g.: class A: pass class B(A): pass class C(A): pass def process_any_subclass_type_of_A(cls: A): if cls == B: # do something elif…
user1211030
  • 2,680
  • 2
  • 19
  • 22
207
votes
4 answers

How do you use the ellipsis slicing syntax in Python?

This came up in Hidden features of Python, but I can't see good documentation or examples that explain how the feature works.
miracle2k
  • 29,597
  • 21
  • 65
  • 64
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
161
votes
11 answers

Abstract classes in Swift Language

Is there a way to create an abstract class in the Swift Language, or is this a limitation just like Objective-C? I'd like to create a abstract class comparable to what Java defines as an abstract class.
kev
  • 7,712
  • 11
  • 30
  • 41
160
votes
3 answers

How to test if one java class extends another at runtime?

How to I test if a is a subclass of b? Class a = A.class; Class b = B.class;
Armand
  • 23,463
  • 20
  • 90
  • 119
120
votes
5 answers

Understanding __init_subclass__

I finally upgraded my python version and I was discovering the new features added. Among other things, I was scratching my head around the new __init_subclass__ method. From the docs: This method is called whenever the containing class is…
EsotericVoid
  • 2,306
  • 2
  • 16
  • 25
109
votes
15 answers

How do I check if an object's type is a particular subclass in C++?

I was thinking along the lines of using typeid() but I don't know how to ask if that type is a subclass of another class (which, by the way, is abstract)
Chad
  • 2,335
  • 8
  • 29
  • 45
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
84
votes
5 answers

How does inheritance of __slots__ in subclasses actually work?

In the Python data model reference section on slots there is a list of notes on using __slots__. I am thoroughly confused by the 1st and 6th items, because they seem to be contradicting each other. First item: When inheriting from a class…
jathanism
  • 33,067
  • 9
  • 68
  • 86
1
2 3
99 100