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
81
votes
7 answers

How to check if a subclass is an instance of a class at runtime?

In an android app test suite I have a class like this where B is a view: public class A extends B { ... etc... } now I have a list of view objects which may contain A objects but in this case I only care if they're subclasses or "instances of" B.…
iamamused
  • 2,720
  • 3
  • 25
  • 17
78
votes
6 answers

Add rounded corners to all UIImageViews

I would like to add some rounded corners to all of the UIImageViews in my project. I have already got the code working, but am having to apply it to every image; should I subclass UIImageView to add this? If so, can someone give me some pointers as…
Jack
  • 3,878
  • 15
  • 42
  • 72
64
votes
2 answers

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. class TestClass(int): def __init__(self): …
me_and
  • 15,158
  • 7
  • 59
  • 96
61
votes
2 answers

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.py:4: DeprecationWarning: object.__init__() takes…
Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187
59
votes
8 answers

Reclassing an instance in Python

I have a class that is provided to me by an external library. I have created a subclass of this class. I also have an instance of the original class. I now want to turn this instance into an instance of my subclass without changing any properties…
balpha
  • 50,022
  • 18
  • 110
  • 131
55
votes
2 answers

Swift + CoreData: Cannot Automatically Set Optional Attribute On Generated NSManagedObject Subclass

I have a coredata entity named Record and has a property dateUpdated. I noticed that the generated NSManagedObject subclass has no optional mark (?) CoreData Editor: Generated Subclass: Expected: UPDATED: It's tedious in my part, because each…
Allan Macatingrao
  • 2,071
  • 1
  • 20
  • 28
54
votes
5 answers

Subclass dict: UserDict, dict or ABC?

What's the difference between UserDict, dict and ABC and which one is recommended? The docs seem to deprecate UserDict? Also it seems UserDict's update() would use my setitem method whereas dict doesn't? Which methods are really essential to…
Gere
  • 12,075
  • 18
  • 62
  • 94
53
votes
3 answers

Why doesn't a using-declaration work to solve the diamond problem?

Please consider the following code: struct A { void f() { } }; struct B1 : A { }; struct B2 : A { }; struct C : B1, B2 { void f() // works { B1::f(); } //using B1::f; // does not work //using B1::A::f; //…
gd1
  • 11,300
  • 7
  • 49
  • 88
53
votes
4 answers

Subclassing Python dictionary to override __setitem__

I am building a class which subclasses dict, and overrides __setitem__. I would like to be certain that my method will be called in all instances where dictionary items could possibly be set. I have discovered three situations where Python (in this…
Ian Clelland
  • 43,011
  • 8
  • 86
  • 87
52
votes
2 answers

What is the difference between parent and base in Perl 5?

There appears to be a new pragma named parent that does roughly the same thing as base. What does parent do that warrants a new (non-core) module? I am missing something?
Chas. Owens
  • 64,182
  • 22
  • 135
  • 226
52
votes
4 answers

C++ friend inheritance?

Does a subclass inherit, the main class' friend associations (both the main class' own and other classes friended with the main class)? Or to put it differently, how does inheritance apply to the friend keyword? To expand: And if not, is there any…
SE Does Not Like Dissent
  • 1,767
  • 3
  • 16
  • 36
46
votes
8 answers

What is a Subclass

What is a "subclass" in java? I know about classes and methods, but I do not know about subclasses.
Master C
  • 1,536
  • 4
  • 14
  • 19
46
votes
4 answers

Swift subclassing - how to override Init()

I have the following class, with an init method: class user { var name:String var address:String init(nm: String, ad: String) { name = nm address = ad } } I'm trying to subclass this class but I keep getting errors on the…
sirab333
  • 3,662
  • 8
  • 41
  • 54
43
votes
4 answers

How to subclass Python list without type problems?

I want to implement a custom list class in Python as a subclass of list. What is the minimal set of methods I need to override from the base list class in order to get full type compatibility for all list operations? This question suggest that at…
silvado
  • 17,202
  • 2
  • 30
  • 46
41
votes
1 answer

How do I override inherited methods when using JavaScript ES6/ES2015 subclassing

I have created a class that extends Array. I want to execute arbitrary code before calling the inherited push function. class newArray extends Array{ //execute any logic require before pushing value onto array this.push(value) …
Udesh
  • 1,919
  • 3
  • 20
  • 26