478 questions
243
votes
18
answers
182k
views
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 ...
341
votes
13
answers
206k
views
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.
5
votes
3
answers
6k
views
How to create a subclass in python that is inherited from turtle Module
So, i'm trying to learn python and every time i post a question here it feels like giving in...
I'm trying to make my own class of turtle.Turtle.
import turtle
class TurtleGTX(turtle.Turtle):
...
109
votes
6
answers
136k
views
How to properly subclass dict and override __getitem__ & __setitem__
I am debugging some code and I want to find out when a particular dictionary is accessed. Well, it's actually a class that subclasses dict and implements a couple extra features. Anyway, what I would ...
423
votes
5
answers
272k
views
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 ...
64
votes
2
answers
31k
views
Subclassing tuple with multiple __init__ arguments
The following code works:
class Foo(tuple):
def __init__(self, b):
super(Foo, self).__init__(tuple(b))
if __name__ == '__main__':
print Foo([3, 4])
$ python play.py
Result:
play....
215
votes
5
answers
72k
views
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.
68
votes
2
answers
34k
views
Subclassing int in Python
I'm interested in subclassing the built-in int type in Python (I'm using v. 2.5), but having some trouble getting the initialization working.
Here's some example code, which should be fairly obvious.
...
5
votes
3
answers
3k
views
why java polymorphism not work in my example
I have these 4 java clases:
1
public class Rect {
double width;
double height;
String color;
public Rect( ) {
width=0;
height=0;
color="transparent";
...
258
votes
5
answers
168k
views
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):
...
37
votes
3
answers
28k
views
Why shouldn't I subclass a UIButton?
I've asked a few questions on stack overflow about subclassing a UIButton, and a couple of people have informed me that I shouldn't subclass a UIButton.
What are the negatives of subclassing a ...
18
votes
9
answers
52k
views
Is it possible to call subclasses' methods on a superclass object?
Animal is a superclass of Dog
and Dog has a method called bark
public void bark()
{
System.out.println("woof");
}
Consider the following:
Animal a = new Dog();
if (a instanceof Dog){
a.bark(...
26
votes
15
answers
40k
views
Inheritance in Java - creating an object of the subclass invokes also the constructor of the superclass. Why exactly?
I have a question about inheritance in Java.
I have two classes A and B , and class B, inherits from A:
public class A {
public A() {
System.out.println("Hi!");
}
}
public class B ...
195
votes
11
answers
101k
views
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 ...
17
votes
8
answers
10k
views
Why do I get "non-static variable this cannot be referenced from a static context"?
I have a very simple class which I want to use as a subclass of another one. But when I put its code in the parent's class I get :
non-static variable this cannot be referenced from a static ...