Using a Superclass Reference for a Subclass Object
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:
- getUserName()
- getUserSalary().
while the Employee class has an additional method called getEmployeeInformation() and overrides the getUserSalary() method.
THen with User user = new Employee();:
user.getUserName() will work, as it's defined in the User class.
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.
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! :)
The above is the detailed content of Using a Superclass Reference for a Subclass Object. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Start Spring using IntelliJIDEAUltimate version...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...
