[go: up one dir, main page]

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

Inheritance Master - 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)
21 views3 pages

Inheritance Master - 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
You are on page 1/ 3

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

Name: (26) Bora Kurtuluş

Inheritance Master
Main.java

1. import java.util.ArrayList;
2. public class Main
3. {
4. public static void main(String[] args)
5. {
6. Person s1 = new Student("RC");
7. System.out.println(s1.getName());
8. System.out.println(s1.getAge());
9. //System.out.println(s1.getSchool()); //this will give error
10. System.out.println();
11. System.out.println(s1.sentenceGen()); //this will be executed but the
getName() will be the one in the Student class.
12. System.out.println(s1);
13. System.out.println("-----");
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. list.add(s2);
20. list.add(s3);
21. /*for (int i = 0; i < list.size(); i++) {
22. System.out.println(list.get(i).getName());
23. }*/
24. for (Person obj: list) {
25. System.out.println(obj.getName());
26. }
27.
28. System.out.println(s1.method());
29.
30.
31. }
32.
33. public static void method(Person obj) {
34. System.out.println("method saying " + obj.getName());
35. }
36.
37. }

Person.java

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. this.name = name;
9. this.age = age;
10. this.sex = sex;
11. }
12.
https://codehs.com/editor/print/17577754/3594515/0/0 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. }

Student.java

1. public class Student extends Person {


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

https://codehs.com/editor/print/17577754/3594515/0/0 2/3
5/6/25, 9:50 PM Inheritance Master | CodeHS
29. }

https://codehs.com/editor/print/17577754/3594515/0/0 3/3

You might also like