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
41
votes
2 answers

python subclass access to class variable of parent

I was surprised to to learn that a class variable of a subclass can't access a class variable of the parent without specifically indicating the class name of the parent: >>> class A(object): ... x = 0 ... >>> class B(A): ... y = x+1 ...…
new name
  • 15,861
  • 19
  • 68
  • 114
41
votes
3 answers

Subclassing NSObject in Swift - Best Practice with Initializers

Here is the layout of an example Class, can someone guide me on what's best practice when creating a subclass of NSObject? class MyClass: NSObject { var someProperty: NSString! = nil override init() { self.someProperty = "John" …
Woodstock
  • 22,184
  • 15
  • 80
  • 118
39
votes
10 answers

How to reduce code by using a superclass?

I would like to refactor some code that currently consists of a superclass and two subclasses. These are my classes: public class Animal { int a; int b; int c; } public class Dog extends Animal { int d; int e; } public class…
Jax Teller
  • 1,447
  • 2
  • 15
  • 24
39
votes
6 answers

Understanding upper and lower bounds on ? in Java Generics

I am really having a tough time understanding the wild card parameter. I have a few questions regarding that. ? as a type parameter can only be used in methods. eg: printAll(MyList) I cannot define classes with ? as type…
An SO User
  • 24,612
  • 35
  • 133
  • 221
38
votes
3 answers

iOS: UIView subclass init or initWithFrame:?

I made a subclass of UIView that has a fixed frame. So, can I just override init instead of initWithFrame:? E.g.: - (id)init { if ((self = [super initWithFrame:[[UIScreen mainScreen] bounds]])) { self.backgroundColor = [UIColor…
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
38
votes
5 answers

Override @property setter and infinite loop

There is Class A with: @interface ClassA : NSObject { } @property (nonatomic, assign) id prop1; @end @implementation @synthesize prop1; @end then I have subclass @interface ClassB : ClassA { } @end @implementation - (id)init { self = [super…
Marcin
  • 3,694
  • 5
  • 32
  • 52
38
votes
3 answers

Disallow subclasses from overriding Java method

Suppose I have a method in a Java class, and I don't want any subclass of that class to be able to override that method. Can I do that?
John
  • 4,596
  • 11
  • 37
  • 43
37
votes
3 answers

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 UIButton? And I know it's vague, but what are other…
SirRupertIII
  • 12,324
  • 20
  • 72
  • 121
35
votes
7 answers

Expose a private Objective-C method or property to subclasses

According to some official talk, a class in Objective-C should only expose public methods and properties in its header: @interface MyClass : NSObject @property (nonatomic, strong) MyPublicObject *publicObject; - (void)publicMethod; @end and…
hzxu
  • 5,753
  • 11
  • 60
  • 95
34
votes
5 answers

Subclassing dict: should dict.__init__() be called?

Here is a twofold question, with a theoretical part, and a practical one: When subclassing dict: class ImageDB(dict): def __init__(self, directory): dict.__init__(self) # Necessary?? ... should dict.__init__(self) be called,…
Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260
33
votes
4 answers

In C# 4.0, is it possible to derive a class from a generic type parameter?

I've been trying this, but I can't seem to figure this out. I want to do this... public abstract class SingletonType : TBaseClass where TSingleton : TBaseClass, new() where TBaseClass : class { static TSingleton…
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
32
votes
3 answers

Java: returning subclass in superclass method signature

I'm working on a problem where there are several implementations of Foo, accompanied by several FooBuilder's. While Foo's share several common variables that need to be set, they also have distinct variables that require their respective FooBuilder…
downer
  • 954
  • 2
  • 13
  • 24
31
votes
5 answers

Programmatically create UICollectionView with custom headers

I'm making an iOS app in swift, and I'm trying to make a collectionView programmatically. I want to use my own subclass of UICollectionReusableView as a header for the CollectionView, because I need some buttons and a stretchable image in the…
Wiingaard
  • 4,150
  • 4
  • 35
  • 67
30
votes
4 answers

When to use categories and when to use subclassing?

Can anybody tell me when to use categories and when to use subclassing in Objective-C? Also please tell me the advantages and disadvantages of them.
30
votes
4 answers

Swift - UIButton overriding setSelected

I'm making a UIButton subclass in Swift to perform custom drawing and animation on selection What would be the equivalent in Swift of overriding - (void)setSelected:(BOOL)selected in ObjC? I tried override var selected: Bool so I could implement…
LorTush
  • 719
  • 1
  • 8
  • 14