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
29
votes
6 answers

Java - is there a "subclassof" like instanceof?

Im overriding an equals() method and I need to know if the object is an instance of a Event's subclass (Event is the superclass). I want something like "obj subclassof Event". How can this be made? Thanks in advance!
rasgo
  • 1,381
  • 4
  • 15
  • 28
29
votes
2 answers

How to subclass an OrderedDict?

Subclassing a Python dict works as expected: >>> class DictSub(dict): ... def __init__(self): ... self[1] = 10 ... >>> DictSub() {1: 10} However, doing the same thing with a collections.OrderedDict does not work: >>> import…
Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260
29
votes
10 answers

How to "clone" an object into a subclass object?

I have a class A and a class B that inherits class A and extends it with some more fields. Having an object a of type A, how can I create an object b of type B that contains all data that object a contained? I have tried a.MemberwiseClone() but that…
Vizu
  • 1,871
  • 1
  • 15
  • 21
28
votes
2 answers

How can I implement single table inheritance using Laravel's Eloquent?

Currently I have a model class named Post. class Post extends Eloquent { protected $table = 'posts'; protected $fillable = array('user_id', 'title', 'description', 'views'); /* * Relationships */ public function user() …
JasonK
  • 5,214
  • 9
  • 33
  • 61
28
votes
1 answer

Make SKSpriteNode subclass using Swift

I'm trying to create class which is a subclass of SKSpriteNode and I want to add other properties and functions to it. But in the first step I faced an error. Here's my code: import SpriteKit class Ball: SKSpriteNode { init() { …
Potter
  • 855
  • 2
  • 9
  • 9
28
votes
6 answers

Enforcing Class Variables in a Subclass

I'm working on extending the Python webapp2 web framework for App Engine to bring in some missing features (in order to make creating apps a little quicker and easier). One of the requirements here is that each subclass needs to have some specific…
A_Porcupine
  • 1,008
  • 2
  • 13
  • 23
28
votes
8 answers

how to set UIButton type in UIButton Subclass

I am subclassing the UIButton, what i want is to set the button type to Round Rect. Button.h @interface Button : UIButton {} - (void)initialize; @end Button.m @implementation Button - (id)initWithFrame:(CGRect)frame { self = [super…
Haris Hussain
  • 2,531
  • 3
  • 25
  • 38
27
votes
4 answers

Make Background of UIView a Gradient Without Sub Classing

Is there a way to make the background of a UIView a gradient without subclassing it? I'd rather not use an image file to accomplish this either. It just seems obtuse to have to subclass UIView just to draw a gradient for the background.
joshwbrick
  • 5,882
  • 9
  • 48
  • 72
27
votes
5 answers

Is there an Objective-C equivalent to Swift's fatalError?

I want to discard superclass's default init method.I can achieve this easily with fatalError in Swift: class subClass:NSObject{ private var k:String! override init(){ fatalError("init() has not been implemented") } init(kk:String){ …
wj2061
  • 6,778
  • 3
  • 36
  • 62
26
votes
5 answers

How to subclass UIScrollView and make the delegate property private

Here is what I want to achieve: I want to subclass an UIScrollView to have additional functionality. This subclass should be able to react on scrolling, so i have to set the delegate property to self to receive events like: - (void)…
Mexyn
  • 375
  • 1
  • 4
  • 9
26
votes
15 answers

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 extends A { public B() { …
user42155
  • 48,965
  • 27
  • 59
  • 60
26
votes
1 answer

ES6 class super() with variadic arguments

In ES6, is there a way to call a parent constructor passing through variadic arguments, a la foo.apply(this, arguments)? I've looked for an answer, and the only instances I see are either calling super() (no arguments) or calling super(x, y) (with…
Turner Hayes
  • 1,844
  • 4
  • 24
  • 38
26
votes
4 answers

best way to implement custom pretty-printers

Customizing pprint.PrettyPrinter The documentation for the pprint module mentions that the method PrettyPrinter.format is intended to make it possible to customize formatting. I gather that it's possible to override this method in a subclass, but…
intuited
  • 23,174
  • 7
  • 66
  • 88
26
votes
3 answers

Subclass of class with synthesized readonly property cannot access instance variable in Objective-C

In the superclass MyClass: @interface MyClass : NSObject @property (nonatomic, strong, readonly) NSString *pString; @end @implementation MyClass @synthesize pString = _pString; @end In the subclass MySubclass @interface MySubclass :…
tacos_tacos_tacos
  • 10,277
  • 11
  • 73
  • 126
25
votes
5 answers

In .NET, can you use reflection to get all non-inherited methods of a class?

Because of this issue here, I'm trying to write a custom JsonConverter that handles cases where you subclass a list or a collection, then add extra properties to it. As such, one approach would be to ignore all base-class properties and only…
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286