Inheritance Example in Java CodeHS
Inheritance Example in Java CodeHS
The output can be predicted as follows: `Student called unknown` (from `s1.getName()`), `0` (from `s1.getAge()`), followed by `hiStudent called unknown`, `Person with age 0 and genderunknown`, `-----`, `method saying Student called bora` (from `method(s2)`), `bora` and `Student called bora` (from the `for` loop output). This prediction relies on understanding inheritance, overridden methods, constructor effects, and polymorphism in the code .
Polymorphism in Java is the ability of objects of different types to be treated as instances of the same class through inheritance. In the provided code, polymorphism is demonstrated by the fact that a `Student` object is assigned to a `Person` reference (e.g., `Person s1 = new Student("RC");`). Even though `s1` is of type `Person`, it calls methods overridden in the `Student` class, such as `getName()`, showing polymorphic behavior .
Executing `System.out.println(s1.sentenceGen());` where `s1` is a `Student` object would output `"hiStudent called RC"`. This occurs because the `sentenceGen()` method in `Person` is not overridden in `Student`. It calls `getName()`, overridden in `Student` to prepend "Student called " to the `name`, demonstrating polymorphic method execution .
The `method()` execution illustrates superclass-subclass relationships by showing that even when a subclass has a method with the same name and signature, calling `method()` on a `Student` object executes the `Person`'s `method()`, returning "aaa". This happens because `Student` explicitly calls `super.method()`, demonstrating how subclasses can control whether to use superclass method implementations or replace them .
The call `System.out.println(s1.getSchool());` results in a compilation error because `s1` is declared as a `Person` reference. Although it is actually a `Student` instance, the `Person` class does not have a `getSchool()` method. Therefore, the method is not visible through a `Person` reference, illustrating the concept of compile-time type checking in Java .
Encapsulation is the principle of wrapping data (variables) and code (methods) as a single unit, while restricting access to some components. In the `Person` and `Student` classes, encapsulation is applied by making class variables private (e.g., `private String name` in `Person`, `private String school` in `Student`) and providing public getter methods to access these variables. This practice ensures controlled access and modification of object properties .
The inheritance structure requires that a `Student` constructor with parameters calls the `Person` constructor to initialize inherited fields. In `Student`, the constructor `Student(String name, int age, String sex, String school)` explicitly calls `super(name, age, sex)` to ensure that `name`, `age`, and `sex` are initialized according to the `Person` class's definition, before setting its own fields like `school` .
Method overriding occurs when a subclass provides a specific implementation for a method that is already defined in its superclass. In the given classes, `Student` overrides the `getName()` method of `Person`, providing a different output while maintaining the same method signature. In contrast, method overloading would involve having multiple methods with the same name but different parameter lists within the same class or across subclasses, which is not exemplified in the code provided .
When `s1` (of type `Person` but initialized as `Student`) and `s2` are passed to `System.out.println`, Java automatically calls their `toString()` method. The `Student` class overrides `Person`'s `toString()`, so the version of `toString()` called depends on the actual object type. For `s1` and `s2`, the `Student`'s `toString()` will execute, displaying `"Student with age 0 and gender unknown who goes to RC"` for `s1` and `"Student with age 17 and gender male who goes to RC"` for `s2` .
The `ArrayList<Person> list` demonstrates polymorphism by allowing storage of objects of type `Student`, which is a subclass of `Person`, together with objects of type `Person`. Polymorphic behavior is evident when iterating over this list, as the overridden methods of the `Student` class can be invoked through its `Person` references, as seen in `for (Person obj: list) { System.out.println(obj.getName()); }`, which prints different outputs depending on the actual object type .