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.

Superclass

A superclass is a parent or base class that is derived or inherited from by a child class (or subclass)



Can not lower

Example

"Still you cannot inherited from a as superclass cannot have a lower visibility than subclassing"

from question  

Public class extends internal class

"Superclass cannot handle subclassing exceptions;subclassing can replace wrap superclass methods not the other way round"

from question  

Superclass handling subclass exceptions

"One thing that i could do is just catch and log an error only when the superclass cannot be investigated;so the subclassing would be outputted correctly"

from question  

OrmLiteConfigUtil config file generation problems

Object specific smaller

Example

"With explicit superclass calling your subclassing can accept more or fewer arguments than the subclassing didn t superclass and can decide the subclassing didn t what to pass when calling its superclass"

from question  

Understanding calling parent __init__ in subclass

"A subclassing is more flexible and is treated as an entire object which responds to all superclass methods plus it s own"

from question  

Categories on NSObject -- keeping it safe

"The general problem is that the subclassing is more specific than the superclass"

from question  

Use extended class in place of base class

"If we are creating an object of a subclassing and invoking the method of superclass and if subclassing extends more than one class which superclass method should be called"

from question  

Why is Multiple Inheritance not allowed in Java or C#?

"Casting an object from a subclassing to a superclass doesn t require an explicit cast;casting an object from a superclass to a subclassing requires an explicit cast"

from question  

Java inheritance and casting

"For my understanding the superclass is always smaller less complex then the subclassing"

from question  

Why is it possible to define an variable with type of a superclass, but assign it an object of a subclass?

"However they are not the same because the subclassing has more specific functions and data members that accomplish a more specific task that the superclass"

from question  

Inheritance concept java..help

"2 since superclass is smaller than subclassing one should use memory object carefully"

from question  

Can't downcast because class is not polymorphic?

"If new member fields are declared in the subclassing then yes a subclassing presumably uses more memory since it has all the fields declared in the superclass plus all the fields declared in the subclassing"

from question  

Will extend create an object of its super class.?

"Superclass defines more general features of the objects of its subclassing"

from question  

Java Inheritance: Distinguishing between the IS-A and the HAS-A relationships

"These relations naturally arise when you impose restrictions on what you can handle--then if a subclassing means that the method can handle less acts as a superclass of since can handle everything that the subclassing can handle and more"

from question  

Where's the contravariance?

"Imagine your superclass has an object member but in your subclassing this is now more defined to be an integer"

from question  

Is there a way to override class variables in Java?

"I have an arraylist executed like this im trying to pass in a subclassing object with more parameters than the superclass"

from question  

Arraylist with 2 constructors from subclasses

"In the given question the superclass is the one that has a more general object as the parameter while the subclassing is more specific"

from question  

Why can't Java interface be implemented by less specific function

"In my question the subclassing is less specific than the superclass meaning the subclassing is always able to handle whatever the superclass requires"

from question  

Why can't Java interface be implemented by less specific function

Value method specific

Example

"The other way round returning a subclassing that is a more specific class does not break the contract because a subclassing that is a more specific class has at least the same functionality as the superclass"

from question  

Why we can't override a method and define it to return a superclass of the original method?

"Add a protected getter method in the superclass to return the balance value such as this;this method will be visible to subclassing but not visible externally"

from question  

Java - calling a method in a subclass

"If the subclassing is more specific then the subclassing might fill in all by 2 of the arguments to the subclassing superclass __init__ method"

from question  

Validating Python Arguments in Subclasses

"I suspect that you have some oop background so i won t go too much into subclassing other than to say a subclassing is very often a specialized or more specific version of the superclass;in other words every subclassing is a kind of its superclass but every superclass is not necessarily a type of subclassing"

from question  

Protocols and accessing another class's methods

"I m looking for a clean way to express that a subclassing is returning more specific types from methods on the superclass in typescript"

from question  

* (no title is found for this review)

"You are passing the value into the super constructor but the field value in the superclass is not a class variable it s a local variable;the field value in the subclassing is global but not getting set by the super constructor due to this"

from question  

Reflection set field value in constructor

"Same variable declared in subclassing does not override the value in superclass;to reflect the value associated with superclass you need to pass it to the constructor and set the superclass variable using the super keyword"

from question  

Why is upcasting a Class not changing overridden methods?

"Shouldn t a subclassing interfaced be able to take in more than the superclass interfacec and use the same method"

from question  

Implement interface by taking superclass of parameter

"However if the subclassing returns a narrower subtype of the superclass method return this is called a covariant return type and is allowed in java since jdk 1.5"

from question  

Different return types of abstract method in java without casting

"When you call a method using the super keyword you re specifying that you want to use your superclass s implementation of that method;if your subclassing doesn t override a method defined in super then performing the selector on self will trigger a miss on your class s method lookup table and find the method definition on your superclass"

from question  

Get super class instance in Objective C

"Casting it to the superclass does not invoke the superclass method;the subclassing s method is called automatically"

from question  

Objective C - Overriding a base class method - How to call subclass's method?

"The subclassing is a form of the superclass the superclass is not a form of the sub class;that is because the superclass does not have that method"

from question  

Inheritance can't call child class method

"The subclassing is then free to change the value of the properties or even provide different properties accessor and code in the superclass that uses the properties will just work;in general the superclass shouldn t have to know anything about its subclassing"

from question  

Access subclass property from super class

Constructor default constructors

Example

"In other words if a subclassing is more accessible than its superclass then the access modifier of the superclass loses effect"

from question  

Swift:Class cannot be declared public because its Super class is internal

"So i inspected the compiled firebasemessagingservice which was the subclassing of com.google.firebase.iid.zzb and had zzae intent method as private but its subclassing firebasemessagingservice had protected access i know that subclassing cannot have weaker reference than its superclass so i guess there is something wrong with firebasemessagingservice library"

from question  

FirebaseMessagingService cannot override zzae(Intent) in zzb. Weaker reference error

"Since the constructor is a method that is invoked on the construction of the object in the memory heap then once you create a subclassing that inherited from a superclass the constructor of the superclass is not invoked by default;for instance if you have a class vehicle and a subclassing car assume the vehicle constructor is as follows"

from question  

What is constructor in java, if it is not a member of class?

"The constructor in the subclassing doesn t have to match the one in superclass but it should call it using super ... subclassing should provide a constructor that calls super .. like this or even this superclass has more than one explicit constructor subclassing are forced to provide at least one constructor that calls any of the superclass s constructors subclassing could be any of the following or or or any combination of those constructors"

from question  

Java: Inherit all superclass's constructors by default

"If the superclass has explicit constructors all of which take arguments then the subclassing needs an explicit constructor as well since without such a constructor there would be no way to know what arguments the superclass constructor should be called with;if the default superclass constructor accessed through super is available then the subclassing does not need to have an explicit constructor"

from question  

Does a subclass NEED to have a constructor?

"A subclassing does not have to have any constructor with the same number of parameters in the constructor as the superclass but it does have to call some of its superclass constructors from its own constructor;if the superclass has a no-arg constructor it is called by default if an explicit call to a superclass constructor is omitted or if the subclassing has no explicit constructor at all as is your case but since your superclass does not have a no-arg constructor compilation fails"

from question  

Constructor cannot be applied to given types?

"If the superclass does not contain any default constructor the compiler will provide its own default constructor;no its not necessary to have a argument constructor in superclass if subclassing contains argument constructor"

from question  

Java programming constructor understanding

"If the superclass does not have a no-argument constructor you will get a compile-time error;super t in the subclassing is explicitly invoking a no-args constructor in its superclass class b"

from question  

Why does the subclass have to invoke the no args constructor in the super class?

"A subclassing needs a constructor if the superclass does not have a default constructor or has one that is not accessible to the subclassing;if the subclassing has no constructor at all the compiler will automatically create a public constructor that simply calls through to the default constructor of the superclass"

from question  

Does a subclass NEED to have a constructor?

"No because subclassing are not forced to have the same constructors as their superclass they can have more or fewer constructors;superclass has no explicit constructors subclassing are not forced to have an explicit constructor"

from question  

Java: Inherit all superclass's constructors by default

"In your example above the superclass constructor calls an overridden method test;this works but is potentially dangerous since the subclassing constructor has not been called and your subclassing will not have been fully initialised"

from question  

Overriding Constructors

"The subclassing overridden method cannot have weaker access than superclass method"

from question  

Method overiding and inheritance

Bigger second statement

Example

"In this case superclass is bigger than subclassing that s why the second statement is correct"

from question  

Java extending classes

"You can t cast a superclass in a subclassing because a subclassing may have a bigger interface than the superclass means a subclassing can have functions like getunixtimestamp that the superclass doesn t have"

from question  

Java typecasting and inheritance

Public methods instance

Example

"Maybe the superclass did not even have a constructor;however the subclassing constructors have still called superclass constructors in those case"

from question  

Can't understand Java inheritance

"My superclass does not handle any jaxb annotations it doesn t have to and i would like my subclassing not to include superclass properties while marshalling;adding the xmlaccesortype on superclass cannot be the solution as i have no way to modify the superclass"

from question  

How do I prevent JAXB from binding superclass methods of the @XmlRootElement when marshalling?

"But an instance of the superclass is not necessarily an instance of the subclassing;thus the superclass methods are always available in both but the subclassing methods are only available in the subclassing"

from question  

Java get String from subclass

"You cannot have a subclassing with less methods than a superclass"

from question  

Remove/Deprecate a method in subclass

"When is done in the subclassing the subclassing makes the superclass variable hidden so that the superclass variable is no longer available"

from question  

Why does super not refer to the variable in super class?

"Only public methods that are defined in superclass are accessible;methods defined in subclassing are not"

from question  

Whats the difference between subClass sc = new subClass() and superClass sc = new subClass?

"Think of it this way an instance of a subclassing is also an instance of the superclass;but an instance of the superclass is not necessarily an instance of the subclassing"

from question  

Java get String from subclass

"Then in your superclass s static usesubclass method you can iterate through that list of subclassing instances find the particular one you care about maybe specified by some argument and then do something with it;in general you cannot do that from a superclass and shouldn t"

from question  

Can I know the extending class from the super class in Java?

"This breaks the inherited and the subclassing is no longer an instance of the superclass"

from question  

JavaScript inheritance, superclass identifier in subclass is not defined, even though it is

"There is a third scenario in which a method of the subclassing has the same signature as a method of the superclass but the method of the superclass is not accessible to the subclassing it s either private or package-private and the superclass is in a different package;in this case the method of the subclassing is not overriding the method of the superclass"

from question  

In Java, is there any no-man's-land between overloaded and overridden methods?

"If a method in a superclass refers to a particular field static or otherwise of that class only that class s declaration of the field will be in scope at that point;any fields static or otherwise of subclassing will not be in scope"

from question  

In Java, if a child class shadows a static parent variable with an instance child variable, which variable will inherited methods use?

"And correct way to call the superclass method is no need car.super;since the private instance variable is inherited in the subclassing but not accessible in subclassing you can use the reflection to access it"

from question  

Using a method of parent class with variable of child class without overriding

"If the subclassing doesn t contain all the methods of the superclass they shouldn t be related in such a way;defining b as a subclassing of a is akin to saying b will provide all the functionality that a does plus whatever else you decide to define"

from question  

C#:inheritance for a child class that does have a property from parent

"However i only pass subclassing of errormessagepojosuperclass as arguments the class errormessagepojobundle has more methods than its superclass"

from question  

Pass an instance of the subclass to a method and use it later within that method

"By making the derived method private you d be changing the contract made by the superclass preventing any further subclassing from accessing the originally public method;a function passed a parameter typed to be of the superclass would not know in advance whether it s allowed to call the method or not"

from question  

Inheritance-Why can't I declare a private method in a subclass with same method signature in super?

"You have two private_method methods one in the superclass that is private and an overriden one in the subclassing that is public;your code works because the self.private_method in the superclass isn t calling its own private private_method but the one that is public in the subclassing"

from question  

Recursive call with private method in Ruby

"A subclassing is an extension of the superclass and can access any public protected package members and methods in the superclass;the superclass cannot access members methods of the subclassing unless you cast this to the subclassing"

from question  

Why subclass must call superclass constructor before the subclass can initialise its own variable

Optional placeholders argument

Example

"If you subclassing has less arguments than a superclass and you could make them optional in the superclass just add placeholders in the subclassing"

from question  

Silence "Declaration ... should be compatible" warnings in PHP 7

"Subclassing must allow more than the superclass not less and going from an argument being optional to not optional is allowing less"

from question  

How to not violate the Liskov substitution principle when methods have optional parameters?

Generalized inheritor becomes

Example

"Beyond that the rules and guidelines for abstract-class interfaces and general superclass are the same;any class interface which is being subclassing will not be resolved through magento factory methods"

from question  

Using Object interfaces in Magento modules

"Our superclass is a generalized class but your subclassing will be a specialized inheritor of your superclass;your subclassing becomes more specialized and less generalized as you move down the inherited hierarchy"

from question  

Generalization and Specialization - What is the differences

Deinitializers deinitializer end

Example

"Superclass deinitializers are always;called even if a subclassing does not provide its own deinitializer"

from question  

Understand deinitialization and inheritance in swift language

"Superclass deinitializers are inherited by their subclassing and the superclass deinitializer is called automatically at the end of a subclassing deinitializer implementation;superclass deinitializers are always called even if a subclassing does not provide its own deinitializer"

from question  

Is it possible to override deinit in Swift?

Values reasonable

Example

"So what this says is that provided your subclassing doesn t have any more values than the superclass used to determine ordering implementing is reasonable"

from question  

What should compareTo() return if argument is different implementation of the same interface?

"So what this says is that provided your subclassing doesn t have any more values than the superclass used to determine ordering implementing is reasonable"

from question  

Is there ever a reason to implement java.util.Comparable for a type other than itself?

Others

Example

This way the superclass doesn t have to know what the subclassing need;each subclassing defines its own behavior

from question  

Check if Calling Object is Instance of Child Class

Constricting the access modifier of a superclass method is an invalid override because it s breaking the superclass contract and invalidates the substitution principle a subclassing object is-a superclass object as well;if this was allowed the above client code would break because your subclassing doesn t have that method public

from question  

Why can't we assign weaker privilege in subclass

Hence your subclassing is accepting less classes than the superclass contract promises

from question  

Generic Confusion on Inheritence -- Java

Here having a superclass makes more sense or at least having a realnumber subclassing of number

from question  

Abstract vs Regular Super Class

In enhanced er modelling subclassing inheriting from more than 1 superclass is called multiple inherited

from question  

Multiple Inheritance ER Model

Watch out you can t query a superclass if you have more than 1 subclassing then something like in jql

from question  

How map superclass in JDO with similar behaviour to @MappedSuperclass in JPA

Take note of this there is no way to subclassing more than one superclass at a time

from question  

How to extend Fragment and Activity

The superclass has a more stringent constraint on a property content blank false than the subclassing content nullable true and i am using tableperhierarchy false

from question  

Issue with inheritance and constraints in Grails/GORM

Even in your original post the subclassing is actually more restrictive than the superclass so doing something like

from question  

Override Setter in Subclass

For example you want to use abstract-class in such a case that each of the subclassing is a more specific type of its abstract superclass fruit apple orange banana grape strawberries etc. and you want to use non-abstract superclass in a relationship such as dad and son

from question  

What is a good design pattern for using an abstract superclass?

As other answers describe the superclass cannot know the type of the subclassing unless it s passed in runtime;if the issue you re trying to avoid is to have to create the map-constructor in each subclassing i see only two solutions each with its flaws

from question  

Java static factory in super class

This is not a python convention it is what oo best practice suggests you should do given the language you happen to be using leaves this decision up to you the superclass doesn t know about the subclassing but the subclassing is expected to know about the semantics of the superclass it inherited from;ultimately it is up to the subclassing to maintain a consistent behavior of a true sub-typing which unfortunately the python language does little to help the programmer with

from question  

Python convention: should I normally call the super class' __init__?

Yes but if you think of your diagram as a topographic map the subclassing have higher altitudes than the superclass

from question  

Why are Super-class and Sub-class reversed?

Which means that another function that can take more arguments counts as a subclassing since it can process anything that the superclass can process and if the superclass produces a smaller set of results that s okay since the superclass will also obey the superclass construct that way

from question  

Why making a difference between methods and functions in Scala?

It s because superclass is a lower bound annotation meaning classes that are super to vector;if vectorb is a subclassing to vector then vectorb is a subclassing to vector won t work

from question  

Trouble Using Vector's addAll( ) function when the Vector is in a containing class

Most likely this class doesn ll still be tightly linked with implementations generally a bad thing or if not bad this class doesn s not especially good but i think a structure like this is better than a superclass figuring out what all of a superclass subclassing are

from question  

Getting the name of a sub-class from within a super-class

Subclassing must be aware of superclass es implementation details;creating the superclass is much harder when you have to think about how it can be extended

from question  

Prefer composition over inheritance?

Inherited refers to how members of the superclass are accessible through the subclassing;the subclassing will not actually get copies of its own

from question  

Inheritance of non access modifiers

But a subclassing can be a delegate of its superclass if the superclass does not implement certain methods and you can enforce that the delegate implements these methods if the superclass specifies the protocol required methods and the subclassing adopts it;in obj-c it is not possible to force subclassing to overwrite methods of its superclass

from question  

Objective-C How to force subclass to implement methods?

Superclass receive this message before their subclassing;the superclass implementation may be called multiple times if subclassing do not implement initialize the runtime will call the inherited implementation or if subclassing explicitly call super initialize

from question  

Why is the +initialize method of Core Data managed objects being called twice?

As described in the above diagram we have here a superclass called animal which has three subclassing mammal bird and fish some animals share common behavior a cat and a dove can both walk but the cat cannot fly. these kinds of behavior are orthogonal to this classification so we cannot implement these behavior in the superclasses. if a class could have more than one superclass it would be easy we could create three other classes walker swimmer flyer

from question  

"with" keyword in Flutter

In this case your superclass is not meant to throw an exception;this is a case where your subclassing is thus throwing an exception which is not expected by the overlying software architecture

from question  

Java : how to handle the design when template methods throw exception when overrided method not throw

The fact that the subclassing is more selective only actually writes about what it writes is new functionality especially in light of the fact that the superclass promises nothing

from question  

Why declare an instance as a supertype but instantiate it as a subtype, plus Liskov Substitution Principle

If at some point you need to talk to a subclassing in a way that the superclass would not understand or cause an error needing to use instanceof then what you need is a different class not a subclassing;try to find ways to use inherited and polymorphism instead say for example in your base class you can have a method called performmainjob and using polymorphism you would define that to be a call to a method solveproblem for an engineer subclassing and a call to a method verifybooks for an accountant subclassing

from question  

Class design without using instanceof

Say all instance variables are 32-bit ints for simplicity if the superclass has 3 and the subclassing adds 2 more then each instance of the subclassing will allocate 5 x 4 20 bytes -- 8 for the subclassing own instance variables plus 12 for the instance variables of the superclass

from question  

Do subclasses allocate memory and methods of their ancestors?

So the superclass defines the flow from an abstract viewpoint and the subclassing implement them appropriately;in your case the simplest options are to either just leave performtransform empty in the superclass or implement it as an empty method in the subclassing that doesn t require it when you mix this approach with a short comment you get a more maintainable system imo

from question  

OOP design issue: Polymorphism

Subclassing do not inherited their superclass initializers by default;however superclass initializers are automatically inherited if

from question  

Swift do subclasses inherit initializers?

I get no error when i run the following output in init and without init first read this automatic initializer inherited as mentioned above subclassing do not inherited their superclass initializers by default;however superclass initializers are automatically inherited if certain conditions are met

from question  

Missing argument for parameter 'from' in call when creating instance of Codable class

More mathematically you can think of a class a set of all objects with some similar characteristic and then a subclassing of pets surrounds its superclass and has even more characteristics

from question  

Subclass instance variables changing superclass instance variables in Ruby

This is because when a third class imports the subclassing s header file when a third class imports the subclassing s header file needs the declaration for the superclass prototype and you don t want to have to import all the superclass prototypes header files

from question  

What's the difference between @class Foo and import "Foo.h"?

If superclass is not serializable then all values of the instance variables inherited from superclass will be initialized by calling constructor of non-serializable superclass during deserialization process;in case superclass is not serializable than to serialize the subclassing s object we must implements serializable interface in subclassing explicitly

from question  

Why base class(not implementing Serializable) should have no argument constructor if its subclass implements Serializable?

You are referencing the oak variable as class tree so any property you reference from it should be of class tree not any subclassing because exactly the superclass does not have them thus compiler does not know how to address memory from branches property;worse this addressing might technically differ from subclassing to subclassing an that s why directly addressing properties of a subclassing via reference of type superclass is not possible

from question  

Accessing properties after using getdefinitionbyname

Letting subclass1 call through to a constructor of superclass over the head of its own superclass would break encapsulation of superclass;you cannot skip levels of the constructor if the class in the middle subclassing does not expose a constructor that calls a particular constructor of superclass subclassing of subclassing no longer have an ability to access these constructors

from question  

Java inheritance: Can I access the constructor with parameters in super class?

The is maybe superfluous but it shows the fact that the parent superclass should not be instanciated;the subclassing has no additional property so the subclassing tag is empty

from question  

Mapping Hibernate "Custom" Subclasses

As described here - one common reason to override readobject and writeobject is to serialize the data for a superclass that is not serializable itself;the state that you think you have in your subclassing instance isn t really visible to the serialization as its not going via your api or reflection

from question  

Why aren't my objects deserialized correctly?

Admin is a subclassing from superclass and has more privileges than normal user

from question  

Moving an instance from superclass to subclass

Back to Home
Data comes from Stack Exchange with CC-BY-SA-4.0