Lädt...


🔧 Using a Superclass Reference for a Subclass Object


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Consider a scenario where we create a class named User and then create a subclass that extends User called Employee.
Typically, we create an instance of User with:

User user = new User();

Here User is the type of the variable user, user is the variable that holds the instance of the class, and new User() creates a new instance of User.
Simply put, the user is an instance of User.

But what if the superclass User needs to use methods defined in its subclass Employee? Can this happen?
The short answer is yes, but only for overridden methods (methods that exist in both the superclass and the subclass). This is what enables polymorphism.
Since the relationship in inheritance is "is-a" relationship, Employee is-a User. so, nothing prevents User from holding a reference to an instance of its subclass Employee, as long as it is a compatible type.

This is done as follows:

User user = new Employee();

Now, let’s say the User class has two methods:

  1. getUserName()
  2. getUserSalary().

while the Employee class has an additional method called getEmployeeInformation() and overrides the getUserSalary() method.

THen with User user = new Employee();:

  1. user.getUserName() will work, as it's defined in the User class.
  2. user.getUserSalary() will also work, but the output will be from the overridden getUserSalary() method in the Employee class, not from the one in User. This is the essence of polymorphism.
  3. user.getEmployeeInformation() will not work. It will throw a compilation error as it's specific to the Employee class.

A compilation error occurs when the compiler finds issues such as syntax errors, type mismatches, or other violations that prevent successful code compilation.

If we try to do the reverse and reference a subclass object Employee with a superclass reference User, it will not work without manual casting! This requires explicit casting, as User is not necessarily an Employee.

Upcasting VS. Downcasting

After explaining this in a simple way, with a simple example, let's focus on the terms.

"Superclass reference to subclass object" is typically called upcasting. Simply put, upcasting is typecasting a child object to a parent object, and it happens implicitly (meaning the compiler handles it automatically, so we do not need any specific casting syntax). It's like when we did User user = new Employee();.

What About Downcasting?

Downcasting is the exact opposite of upcasting.

Remember when we said that creating an Employee reference from a User instance is invalid? This is called downcasting, and it must be done explicitly using casting syntax. While upcasting is very safe, downcasting causes risks. This does not mean it's not useful, but it must be used with caution.

I won’t go into too much detail about downcasting since this article is focused on upcasting, but the key point is showing the main difference between downcasting and upcasting.

And that’s a wrap! :)

...

🔧 Using a Superclass Reference for a Subclass Object


📈 90.97 Punkte
🔧 Programmierung

🪟 Destiny 2 player count doubles overnight after Into the Light, The Final Shape's Prismatic subclass reveal


📈 30.17 Punkte
🪟 Windows Tipps

🪟 Destiny 2 update nerfs the Warlock's Stasis subclass


📈 30.17 Punkte
🪟 Windows Tipps

⚠️ [dos] iMessage - NSArray Deserialization can Invoke Subclass that does not Retain References


📈 30.17 Punkte
⚠️ PoC

🔧 Understanding and Fixing "Object Reference Not Set to an Instance of an Object" in C#


📈 26.65 Punkte
🔧 Programmierung

🔧 Object reference not set to an instance of an object


📈 26.65 Punkte
🔧 Programmierung

🔧 Help Needed: "Object reference not set to an instance of an object" Error in Visual Studio


📈 26.65 Punkte
🔧 Programmierung

🔧 Why JavaScript Says "[object Object]" and Not Just "[object]" 🤔


📈 24.76 Punkte
🔧 Programmierung

🕵️ CVE-2022-44108 | pdftojson 94204bb Object.cc Object::copy(Object*) stack-based overflow


📈 24.76 Punkte
🕵️ Sicherheitslücken

🎥 Attribution is in the object: using RTF object dimensions to track APT phishing weaponizers


📈 20.58 Punkte
🎥 IT Security Video

💾 LISTSERV 17 Insecure Direct Object Reference


📈 18.4 Punkte
💾 IT Security Tools

💾 LongBox Limited Access Manager Insecure Direct Object Reference


📈 18.4 Punkte
💾 IT Security Tools

⚠️ Chamilo LMS Insecure Direct Object Reference


📈 18.4 Punkte
⚠️ PoC

⚠️ SchoolPlus 1.0 Insecure Direct Object Reference


📈 18.4 Punkte
⚠️ PoC

💾 Quick Job 2.4 Insecure Direct Object Reference


📈 18.4 Punkte
💾 IT Security Tools

💾 Transport Management System 1.0 Insecure Direct Object Reference


📈 18.4 Punkte
💾 IT Security Tools

💾 Employee Performance Evaluation System 1.0 Insecure Direct Object Reference


📈 18.4 Punkte
💾 IT Security Tools

💾 Tapplock Smart Lock Insecure Direct Object Reference


📈 18.4 Punkte
💾 IT Security Tools

⚠️ ZKTime Web Software 2.0 Insecure Direct Object Reference


📈 18.4 Punkte
⚠️ PoC

⚠️ InfraPower PPS-02-S Q213V1 Insecure Direct Object Reference


📈 18.4 Punkte
⚠️ PoC

⚠️ Bhojon Restaurant Management System 2.8 Insecure Direct Object Reference


📈 18.4 Punkte
⚠️ PoC

💾 Elber Wayber Analog/Digital Audio STL 4.00 Insecure Direct Object Reference


📈 18.4 Punkte
💾 IT Security Tools

⚠️ AdminSeg 2.15 Insecure Direct Object Reference


📈 18.4 Punkte
⚠️ PoC

⚠️ Fortify SSC 17.10 / 17.20 / 18.10 Project Insecure Direct Object Reference


📈 18.4 Punkte
⚠️ PoC

⚠️ Atlassian Confluence XSS / Insecure Direct Object Reference


📈 18.4 Punkte
⚠️ PoC

⚠️ Simbarashe Financial Services 2.9.0 Insecure Direct Object Reference


📈 18.4 Punkte
⚠️ PoC

💾 Agop CMS 1.0 Insecure Direct Object Reference


📈 18.4 Punkte
💾 IT Security Tools

💾 Sony BRAVIA Digital Signage 1.7.8 Insecure Direct Object Reference


📈 18.4 Punkte
💾 IT Security Tools

🕵️ BBE Theme <= 1.52 - Direct Object Reference


📈 18.4 Punkte
🕵️ Sicherheitslücken

⚠️ Trend Micro Hosted Email Security (HES) Interception / Insecure Direct Object Reference


📈 18.4 Punkte
⚠️ PoC

⚠️ [webapps] - InfraPower PPS-02-S Q213V1 - Insecure Direct Object Reference


📈 18.4 Punkte
⚠️ PoC

matomo