Questions tagged [superclass]

A superclass is a parent or base class that is derived or inherited from by a child class (or subclass). Superclasses are used extensively in object-oriented programming (OOP).

SuperClass

A superclass is a class that has been extended by another class. It allows the extending class to inherit its state and behaviors.

Example

In this example, polygon is a superclass of triangle and rectangle:

enter image description here

Reference

1474 questions
50
votes
4 answers

Preventing a class from direct instantiation in Python

I have a super class with a method that calls other methods that are only defined in its sub classes. That's why, when I create an instance of my super class and call its method, it cannot find the method and raises an error. Here is an…
mohi666
  • 6,842
  • 9
  • 45
  • 51
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
45
votes
5 answers

implicit super constructor Person() is undefined. Must explicitly invoke another constructor?

I am working on a project and i am getting the error "implicit super constructor Person() is undefined. Must explicitly invoke another constructor" and i don't quite understand it. Here is my person class: public class Person { public…
sirnomnomz
  • 623
  • 1
  • 7
  • 20
44
votes
7 answers

C# : how do you obtain a class' base class?

In C#, how does one obtain a reference to the base class of a given class? For example, suppose you have a certain class, MyClass, and you want to obtain a reference to MyClass' superclass. I have in mind something like this: Type superClass =…
JaysonFix
  • 2,515
  • 9
  • 28
  • 28
42
votes
11 answers

When to implement an interface and when to extend a superclass?

I've been reading a lot about interfaces and class inheritance in Java, and I know how to do both and I think I have a good feel for both. But it seems that nobody ever really compares the two side by side and explains when and why you would want to…
Agrajag9
  • 726
  • 3
  • 7
  • 10
42
votes
6 answers

how to inherit Constructor from super class to sub class

How to inherit the constructor from a super class to a sub class?
Anil
  • 441
  • 1
  • 4
  • 3
41
votes
5 answers

Python super() behavior not dependable

For some reason, the super() method is not always behaving as expected, opting to return: TypeError('super(type, obj): obj must be an instance or subtype of type)' I understand what the error means. I do not understand why it is coming up as an…
Thomas Thorogood
  • 2,150
  • 3
  • 24
  • 30
41
votes
5 answers

Jackson serialization: how to ignore superclass properties

I want to serialize a POJO class which is not under my control, but want to avoid serializing any of the properties which are coming from the superclass, and not from the final class. Example: public class MyGeneratedRecord extends…
Ferenc
  • 779
  • 1
  • 6
  • 14
40
votes
18 answers

Why is constructor of super class invoked when we declare the object of sub class?

Consider this code: class Test { Test() { System.out.println("In constructor of Superclass"); } int adds(int n1, int n2) { return(n1+n2); } void print(int sum) { System.out.println("the sums are " +…
aman_novice
  • 1,027
  • 3
  • 13
  • 31
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
3 answers

Why is super class constructor always called

I have the following 2 classes public class classA { classA() { System.out.println("A"); } } class classB extends classA { classB() { System.out.println("B"); } } and then running 1 classA c = new…
rgarci0959
  • 539
  • 1
  • 4
  • 8
39
votes
1 answer

Difference between super() and calling superclass directly

In Python 2.7 and 3, I use the following method to call a super-class's function: class C(B): def __init__(self): B.__init__(self) I see it's also possible to replace B.__init__(self) with super(B, self).__init__() and in python3…
johannestaas
  • 1,175
  • 1
  • 9
  • 15
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
33
votes
3 answers

scope of private constructor in Nested Class

This is more of a puzzle than question. I have the following code: public class PrivateBaseConstructor { public static class BaseClass { private BaseClass() { } } public static class DerivedClass extends BaseClass { …
Anshu
  • 7,783
  • 5
  • 31
  • 41
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
1
2
3
98 99