Explore the reasons behind the unexpected `0` and `null` outputs in your Java inheritance class methods, and learn how to fix them effectively.
---
This video is based on the question https://stackoverflow.com/q/64057454/ asked by the user 'Timon' ( https://stackoverflow.com/u/13906464/ ) and on the answer https://stackoverflow.com/a/64057572/ provided by the user 'Hulk' ( https://stackoverflow.com/u/2513200/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Why the inheritance class methods print 0 and null in Java?
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Understanding Why Inheritance Class Methods Print 0 and null in Java
When working with inheritance in Java, encountering unexpected outputs can be frustrating. One common issue developers face is seeing 0 for integer fields and null for string fields when invoking methods on inherited classes. This is exactly what a programmer named Jane experienced with her HourlyEmployee class, which extends the Employee class. Today, we will break down this issue, understand why it occurs, and offer a solution to resolve it.
The Problem Explained
Jane has created an Employee class that includes an employee ID, a name, and corresponding setter and getter methods. Meanwhile, the HourlyEmployee class extends Employee and adds two additional fields: rate and hours. However, when Jane instantiated an object of HourlyEmployee and set the employee ID and name, she received outputs of 0 for the ID and null for the name.
Here’s a summary of the underlying issues causing those outputs:
Field Shadowing: The HourlyEmployee class declares its own empId and name fields, which are separate and unrelated to the Employee class's fields. The HourlyEmployee fields overshadow the parent class's fields.
Unconnected Getter and Setter: The class does not override the getter and setter methods for empId and name, thereby referring to the fields in the superclass, which remain unchanged.
Constructor Invocation: The default constructor of the Employee class is invoked implicitly, failing to initialize the fields from the Employee class.
Solution to the Problem
To fix these issues, we need to make some adjustments to the HourlyEmployee class. Below are the recommended steps:
1. Remove Shadowing Fields
First, we should eliminate the empId and name fields from the HourlyEmployee class. This allows the class to utilize the inherited fields from the Employee class.
2. Use Setter and Getter Methods
Since HourlyEmployee is a subclass of Employee, it should leverage the inherited methods to set and get the employee ID and name. The field shadowing issue will be resolved in doing so.
3. Proper Constructor Implementation
The new constructor in the HourlyEmployee class should invoke the superclass constructor explicitly, ensuring the employee ID and name are initialized correctly.
Here is an updated implementation of the HourlyEmployee class:
[[See Video to Reveal this Text or Code Snippet]]
Example Usage of Updated Class
Here’s how the HourlyEmployee class can now be utilized properly:
[[See Video to Reveal this Text or Code Snippet]]
Conclusion
By addressing issues related to field shadowing, ensuring the correct use of constructors, and leveraging inherited methods, developers can resolve the confusion regarding unexpected outputs in Java inheritance. Understanding how object-oriented principles like inheritance work in Java is crucial for writing efficient and error-free code.
This guide clarified the common pitfalls leading to the printing of 0 and null when working with inheritance in Java and provided effective solutions. If you have any lingering questions or further issues, feel free to reach out or leave a comment!
Информация по комментариям в разработке