Questions tagged [subclassing]

A subclass, "derived class", heir class, or child class is a modular, derivative class that inherits one or more language entities from one or more other classes.

A subclass, "derived class", heir class, or child class is a modular, derivative class that inherits one or more language entities from one or more other classes (called superclasses, base classes, or parent classes). The semantics of class inheritance vary from language to language, but commonly the subclass automatically inherits the instance variables and member functions of its superclasses. Some languages support the inheritance of other construct as well. For example, in Eiffel, contracts which define the specification of a class are also inherited by heirs. The superclass establishes a common interface and foundational functionality, which specialized subclasses can inherit, modify, and supplement. The software inherited by a subclass is considered reused in the subclass. A reference to an instance of a class may actually be referring one of its subclasses. The actual class of the object being referenced is impossible to predict at compile-time. A uniform interface is used to invoke the member functions of objects of a number of different classes. Subclass may replace superclass functions with entirely new functions that must share the same method signature.

833 questions
90
votes
9 answers

Protocol func returning Self

I have a protocol P that returns a copy of the object: protocol P { func copy() -> Self } and a class C that implements P: class C : P { func copy() -> Self { return C() } } However, whether I put the return value as Self I get…
aeubanks
  • 1,261
  • 1
  • 12
  • 12
75
votes
4 answers

If you overwrite a field in a subclass of a class, the subclass has two fields with the same name(and different type)?

I have 3 classes: public class Alpha { public Number number; } public class Beta extends Alpha { public String number; } public class Gama extends Beta { public int number; } Why does the following code compile? And, why does the test…
Kiril Kirilov
  • 11,167
  • 5
  • 49
  • 74
67
votes
3 answers

Extending Generic Classes

public class MyGeneric {} public class Extend1 extends MyGeneric {} public class Extend2 extends MyGeneric {} As far as I am aware, both of the subclasses in the above example are valid. I was wondering how Java…
James
  • 2,483
  • 2
  • 24
  • 31
51
votes
5 answers

How to subclass str in Python

I am trying to subclass str object, and add couple of methods to it. My main purpose is to learn how to do it. Where I am stuck is, am I supposed to subclass string in a metaclass, and create my class with that meta, or subclass str directly? And…
yasar
  • 13,158
  • 28
  • 95
  • 160
45
votes
2 answers

How can I subclass a Pandas DataFrame?

Subclassing Pandas classes seems a common need, but I could not find references on the subject. (It seems that Pandas developers are still working on it: Easier subclassing #60.) There are some SO questions on the subject, but I am hoping that…
Lei
  • 733
  • 1
  • 5
  • 13
43
votes
3 answers

Why can't you reduce the visibility of a method in a Java subclass?

Why does the compiler give an error message when you reduce the visibility of a method while overriding it in the subclass?
ria
  • 7,904
  • 11
  • 39
  • 46
42
votes
6 answers

How do I include subclasses in Swagger API documentation/ OpenAPI specification using Swashbuckle?

I have an Asp.Net web API 5.2 project in c# and generating documentation with Swashbuckle. I have model that contain inheritance something like having an Animal property from an Animal abstract class and Dog and Cat classes that derive from…
Paolo Vigori
  • 1,612
  • 1
  • 18
  • 32
41
votes
8 answers

Why do all backgrounds disappear on UITableViewCell select?

My current project's UITableViewCell behavior is baffling me. I have a fairly straightforward subclass of UITableViewCell. It adds a few extra elements to the base view (via [self.contentView addSubview:...] and sets background colors on the…
epologee
  • 11,229
  • 11
  • 68
  • 104
31
votes
5 answers

What's the closest thing in C++ to retroactively defining a superclass of a defined class?

Suppose I have the class class A { protected: int x,y; double z,w; public: void foo(); void bar(); void baz(); }; defined and used in my code and the code of others. Now, I want to write some library which could very well…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
31
votes
6 answers

UIPopoverPresentationController should have a non-nil sourceView or barButtonItem set before the presentation occurs on iOS 9

I'm trying to show a popup using a custom UIPopoverPresentationController class. But it crashes with the error() should have a non-nil sourceView or barButtonItem set before the presentation occurs. Below…
Francis F
  • 3,157
  • 3
  • 41
  • 79
28
votes
5 answers

Best practice, overriding __construct() versus providing init() method

When you are subclassing objects and want to extend the initialization code, there are two approaches. Overriding __construct(), and implementing an initialization method that your superclass constructor calls. Method 1: class foo { public…
GordonM
  • 31,179
  • 15
  • 87
  • 129
27
votes
2 answers

Non-testable base class extending PHPUnit_Framework_TestCase

Summary How can I create a base class that extends PHPUnit_Framework_TestCase and use that for subclassing actual test cases, without having the base class itself tested by PHPUnit? Further explanation I have a series of related test cases for which…
jgivoni
  • 1,605
  • 1
  • 15
  • 24
26
votes
2 answers

How to override a superclass' property with more specific types?

The Scenario I have a situation where a base class called AbstractRequest has a delegate property of type id declared in the header file: @property (nonatomic, assign) id delegate; The abstract…
epologee
  • 11,229
  • 11
  • 68
  • 104
26
votes
5 answers

How to cast 'Class A' to its subclass 'Class B' - Objective-C

I'm using a framework which defines and uses 'ClassA', a subclass of NSObject. I would like to add some variables and functionality so naturally I created 'ClassB', a subclass of 'ClassA' Now my problem is this. Many of the methods within this…
nrj
  • 1,701
  • 2
  • 22
  • 37
25
votes
4 answers

How to create a subclass in C#?

How do I create a subclass in C# for ASP.NET using Visual Studio 2010?
Neel Desai
  • 449
  • 4
  • 15
  • 26
1
2 3
55 56