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
25
votes
1 answer

How can I change the default value of an inherited dependency property?

How can I change the default value for an inherited dependency property? In our case, we've created a subclass of Control which by default has its Focusable set to 'true'. We want our subclass to have the default of 'false'. What we've been doing…
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
24
votes
5 answers

Python: Can a subclass of float take extra arguments in its constructor?

In Python 3.4, I'd like to create a subclass of float -- something that can be used in math and boolean operations like a float, but has other custom functionality and can receive an argument at initialization that controls that functionality.…
Shimon Rura
  • 439
  • 4
  • 13
23
votes
6 answers

In what ways are subtypes different from subclasses in usage?

A subtype is established when a class is linked by means of extending or implementing. Subtypes are also used for generics. How can I differentiate subtyping from subclasses?
Oh Chin Boon
  • 23,028
  • 51
  • 143
  • 215
23
votes
6 answers

Why can't I subclass datetime.date?

Why doesn't the following work (Python 2.5.2)? >>> import datetime >>> class D(datetime.date): def __init__(self, year): datetime.date.__init__(self, year, 1, 1) >>> D(2008) Traceback (most recent call last): File "",…
Arkady
  • 14,305
  • 8
  • 42
  • 46
23
votes
4 answers

How to add constraints on inherited properties in a grails domain sub-class

Here's what I'd like to do: class A { String string static constraints = { string(maxSize:100) } } class B extends A { static constraints = { string(url:true) } } So class A should have some constraints and B should have the same…
22
votes
3 answers

Subclassing builtin types in Python 2 and Python 3

When subclassing builtin types, I noticed a rather important difference between Python 2 and Python 3 in the return type of the methods of the built-in types. The following code illustrates this for sets: class MySet(set): pass s1 = MySet([1,…
khinsen
  • 1,247
  • 1
  • 10
  • 18
22
votes
1 answer

What's the usage of a virtual subclass?

class AnimalMeta(type): def __instancecheck__(cls, instance): return cls.__subclasscheck__(type(instance)) def __subclasscheck__(cls, sub): return (hasattr(sub, 'eat') and callable(sub.eat) and hasattr(sub,…
weiwang
  • 393
  • 3
  • 10
22
votes
4 answers

Subclassing Numpy Array - Propagate Attributes

I would like to know how custom attributes of numpy arrays can be propagated, even when the array passes through functions like np.fromfunction. For example, my class ExampleTensor defines an attribute attr that is set to 1 on default. import numpy…
Overholt
  • 867
  • 1
  • 10
  • 23
22
votes
6 answers

What's the difference betwen a UserControl and a ContentControl?

According to all of the documentation, when you're creating a non-lookless control, you're supposed to subclass UserControl. However, UserControl is a simple subclass of ContentControl but it doesn't appear to add anything to it, interface-wise. …
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
22
votes
7 answers

How to create an immutable dictionary in python?

I want to subclass dict in python such that all the dictionaries of the sub-class are immutable. I don't understand how does __hash__ affects the immutability, since in my understanding it just signifies the equality or non-equality of objects ! So,…
Yugal Jindle
  • 44,057
  • 43
  • 129
  • 197
21
votes
1 answer

Understanding subclassing of JSONEncoder

I am trying to subclass json.JSONEncoder such that named tuples (defined using the new Python 3.6+ syntax, but it probably still applies to the output of collections.namedtuple) are serialised to JSON objects, where the tuple fields correspond to…
Xophmeister
  • 8,884
  • 4
  • 44
  • 87
21
votes
6 answers

Should I subclass the NSMutableArray class

I have an NSMutableArray object that I want to add custom methods to. I tried subclassing NSMutableArray but then I get an error saying "method only defined for abstract class" when trying to get the number of objects with the count method. Why is…
node ninja
  • 31,796
  • 59
  • 166
  • 254
21
votes
3 answers

Overriding a parent class's methods

Something that I see people doing all the time is: class Man(object): def say_hi(self): print('Hello, World.') class ExcitingMan(Man): def say_hi(self): print('Wow!') super(ExcitingMan, self).say_hi() # Calling the…
orokusaki
  • 55,146
  • 59
  • 179
  • 257
21
votes
3 answers

Override a "private" method in Python

Consider a class with a "private" method such as: class Foo(object): def __init__(self): self.__method() def __method(self): print('42') When I try to subclass Foo and override method __method, it can be seen that…
Ernest A
  • 7,526
  • 8
  • 34
  • 40
20
votes
1 answer

How to handle NestJS Dependency Injection when extending a class for a service?

I am trying to provide a different service based on a value from my ConfigService. The problem I am running into is that the mongoose model that gets injected does not return any values when executing query methods such as findOne() (result is…
cameronliam
  • 217
  • 1
  • 2
  • 9