0% found this document useful (0 votes)
3 views3 pages

Inheritance Example in Java CodeHS

The document contains Java code demonstrating inheritance with classes Person and Student. It shows how to create instances of these classes, use methods to retrieve information, and handle polymorphism. The code also includes examples of method overriding and the use of an ArrayList to store and iterate over Person objects.

Uploaded by

kurbor.26
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Inheritance Example in Java CodeHS

The document contains Java code demonstrating inheritance with classes Person and Student. It shows how to create instances of these classes, use methods to retrieve information, and handle polymorphism. The code also includes examples of method overriding and the use of an ArrayList to store and iterate over Person objects.

Uploaded by

kurbor.26
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

5/6/25, 9:50 PM Inheritance Master | CodeHS

Name: (26) Bora Kurtuluş

Inheritance Master
[Link]

1. import [Link];
2. public class Main
3. {
4. public static void main(String[] args)
5. {
6. Person s1 = new Student("RC");
7. [Link]([Link]());
8. [Link]([Link]());
9. //[Link]([Link]()); //this will give error
10. [Link]();
11. [Link]([Link]()); //this will be executed but the
getName() will be the one in the Student class.
12. [Link](s1);
13. [Link]("-----");
14. Student s2 = new Student("bora", 17, "male", "RC");
15. method(s2);
16.
17. Person s3 = new Person("bora", 17, "male");
18. ArrayList<Person> list = new ArrayList<Person>();
19. [Link](s2);
20. [Link](s3);
21. /*for (int i = 0; i < [Link](); i++) {
22. [Link]([Link](i).getName());
23. }*/
24. for (Person obj: list) {
25. [Link]([Link]());
26. }
27.
28. [Link]([Link]());
29.
30.
31. }
32.
33. public static void method(Person obj) {
34. [Link]("method saying " + [Link]());
35. }
36.
37. }

[Link]

1. public class Person {


2.
3. private String name;
4. private int age;
5. private String sex;
6.
7. public Person(String name, int age, String sex) {
8. [Link] = name;
9. [Link] = age;
10. [Link] = sex;
11. }
12.
[Link] 1/3
5/6/25, 9:50 PM Inheritance Master | CodeHS
13. public Person() {
14. name = "unknown";
15. age = 0;
16. sex = "unknown";
17. }
18.
19. public String method() {
20. return "aaa";
21.
22. }
23. public String getName() {
24. return name;
25. }
26.
27. public int getAge() {
28. return age;
29. }
30.
31. public String getSex() {
32. return sex;
33. }
34.
35. public String sentenceGen() {
36. return "hi" + getName();
37.
38. }
39.
40. //@override
41. public String toString() {
42. return "Person with age " + age + " and gender" + sex;
43. }
44.
45.
46.
47. }

[Link]

1. public class Student extends Person {


2. private String school;
3.
4. public Student(String school) {
5.
6. [Link] = school;
7. }
8. public Student(String name, int age, String sex, String school) {
9. super(name, age, sex);
10. [Link] = school;
11. }
12.
13. public String getName() {
14. return "Student called " + [Link]();
15. }
16.
17. public String getSchool() {
18. return school;
19. }
20.
21. public String method() {
22. return [Link]();
23. //return "broaoaoa";
24. }
25.
26. public String toString() {
27. return "Student with age " + [Link]() + " and gender " +
[Link]() + " who goes to " +school;
28. }

[Link] 2/3
5/6/25, 9:50 PM Inheritance Master | CodeHS
29. }

[Link] 3/3

Common questions

Powered by AI

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 .

You might also like