본문 바로가기
자바(Java)/자바기초

[자바기초.019] Object Superclass

by 긱펀 2024. 3. 17.
반응형

[자바기초.019] Object Superclass

 

 

[1] Object Superclass

자바에서 Object class는 다른 모든 클래스의 superclass입니다. Object 클래스에는 여러가지 메소드가 있는데, 그 중에서 AP CSA 시험에서 강조하는 2가지 메소드( toString, equals )에 대해서 알아보겠습니다.(아래는 AP CSA시험에서 제공하는 Java Quick Reference입니다.)

 

[2] toString( ) 메소드

  • Object 클래스 중에 오버라이드(Override)를 많이 하는 메소드 중의 하나로, toString()이 있습니다.
  • toString()메소드는 객체(object)의 특성을 출력하는 데에 많이 사용됩니다.
  • 내가 만든 클래스에서 toString()을 오버라이드해서 사용해도 됩니다.

 

[예제1] 아래 코드를 실행해보고, 클래스간의 상속관계에서 toString()이 오버라이드 되어 있는 상태를 확인해 보세요.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Person {
    private String name;
    public Person(String name) {
        this.name = name;
    }
 
    public String toString() {
        return name;
    }
}
 
class Student extends Person {
    private int id;
 
    public Student(String name, int id) {
        super(name);
        this.id = id;
    }
 
    public String toString() {
        return super.toString() + " " + id;
    }
}
 
public class Main {
  public static void main(String[] args) {
    Person p = new Person("Sila");
    Student s = new Student("Tully"1001);
    System.out.println(p); // call Person toString
    System.out.println(s); // call Student toString    
  }
}
cs

 

[실행결과]

 

  • 위 코드는 Person 클래스가 Object 클래스의 toString() 메소드를 오버라이드 했습니다.
  • Person 클래스의 상속을 받은 Studen 클래스도 또한 toString() 메소드를 오버라이드 했습니다.
  • Person, Student 클래스는 각각 고유의 필드값도 가지고 있습니다.

 

[유제1] 아래의 조건을 만족하는 코딩을 추가하세요.

<조건>
1.Complete the subclass called APStudent that extends Student with a new attribute called APscore and override the toString() method to call the superclass method and then add on the APscore.
2.Uncomment the APStudent object in the main method to test it.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class Person {
    private String name;
    public Person(String name) {
        this.name = name;
    }
 
    public String toString() {
        return name;
    }
}
 
class Student extends Person {
    private int id;
 
    public Student(String name, int id) {
        super(name);
        this.id = id;
    }
 
    public String toString() {
        return super.toString() + " " + id;
    }
}
 
class APStudent extends Student {
    private int score;
    public APStudent(String name, int id, int score) {
        super(name, id);
        this.score = score;
    }
    // Add a toString() method here that calls the super class toString
 
}
 
public class Main {
  public static void main(String[] args) {
    Person p = new Person("Sila");
    Student s = new Student("Tully"1001);
    System.out.println(p); // call Person toString
    System.out.println(s); // call Student toString
    // Uncomment the code below to test the APStudent class
    /*
    APStudent ap = new APStudent("Ayanna", 1002, 5);
    System.out.println(ap);
    */
    
  }
}
cs

 

 

[3] equals( ) 메소드

  • Object 클래스로부터 상속받은 메소드 중 중요한 것으로는 equals(Object obj) 메소드가 있습니다.
  • 이 메소드는 두 개의 object가 같은지 비교해서, 같으면 true, 다르면 false를 되돌려 줍니다.
  • 문자열 String은 객체에 속하기 때문에, 두 개의 문자열이 같은지 비교하려면 equals를 사용해야 합니다.
  • 숫자가 같은지 비교하려면 == 를 사용하면 됩니다.

 

[예제2] 아래 코드의 실행결과를 확인해 보세요.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Person {
    private String name;
    public Person(String theName) {
        this.name = theName;
    }
}
 
public class Main {
  public static void main(String[] args) {
    Person p1 = new Person("Kairen");
    Person p2 = new Person("Jewel");
    Person p3 = new Person("Kairen");
    Person p4 = p3;
    System.out.println(p1.equals(p2));
    System.out.println(p2.equals(p3));
    System.out.println(p1.equals(p3));
    System.out.println(p3.equals(p4));
  }
}
cs

 

[실행결과]

 

 

[유제2] 아래 코드의 실행결과를 말해보자.

 

1
2
3
4
5
6
String s1 = "hi";
String s2 = "Hi";
String s3 = new String("hi");
System.out.println(s1.equals(s2));
System.out.println(s2.equals(s3));
System.out.println(s1.equals(s3));
cs

 

 

 


[유제 정답은 아래 "더보기" 클릭]

더보기

[유제1 정답]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class Person {
    private String name;
    public Person(String name) {
        this.name = name;
    }
 
    public String toString() {
        return name;
    }
}
 
class Student extends Person {
    private int id;
 
    public Student(String name, int id) {
        super(name);
        this.id = id;
    }
 
    public String toString() {
        return super.toString() + " " + id;
    }
}
 
class APStudent extends Student {
    private int score;
    public APStudent(String name, int id, int score) {
        super(name, id);
        this.score = score;
    }
    // Add a toString() method here that calls the super class toString
  @Override
  public String toString() {
    return super.toString() + " " + score;
  }
 
}
 
public class Main {
  public static void main(String[] args) {
    Person p = new Person("Sila");
    Student s = new Student("Tully"1001);
    System.out.println(p); // call Person toString
    System.out.println(s); // call Student toString
    // Uncomment the code below to test the APStudent class
    
    APStudent ap = new APStudent("Ayanna"10025);
    System.out.println(ap); 
  }
}
cs

 

[유제2 정답]

false

false

true

(String 클래스는 equals 메소드를 오버라이드해서, 두 개의 문자열을 글자 하나씩 비교하는 거로 재정의 되어 있습니다.)

 

 


 

728x90
반응형

댓글