// 클래스명[] 참조변수 = new 클래스명[요소의 갯수]
Student[] arr = new Student[3];
arr[0] = new Student();
arr[0].setNo(1);
arr[0].setName("김사과");
...
arr[1] = new Student();
arr[1].setNo(2);
...
toString()
- 객체의 정보를 문자열 형태로 표현하고자 할 때 오버라이딩을 자주 하는 편
// System.out.println(참조변수) == 참조변수.toString()
객체의 해시코드(주소) 출력
패키지명.클래스명@해시코드
// getClass().getName() : 패키지명 + 클래스명
// Integer.toHexString(hashcode) : 해시코드
equals()
clone()
특정 클래스를 구성할 때 기존 클래스의 데이터와 메소드를 부모 클래스에서 그대로 물려받아 중복적인 코드를 줄이는 것
하나의 변수 타입으로 여러 종류의 객체를
의미하는 추상화된 방식의 프로그램을 가능하게 해줌
접근제어자 class 클래스명 extends 부모클래스명{}
업캐스팅
다운캐스팅
- 부모클래스타입에서 다시 자식클래스로 복구시키는 것 (원상태로 돌리는 것)
Class Person{
String name;
person(){}
person(String name){
this.name = name;
}
}
class Student extends person{
String no;
}
public class Main{
public static void main(String[] args){
Student student = new Student(); //name(O), no(O)
Person person = student; // 업캐스팅
person.name = "김사과"; // [O]
person.no = 10; // [X]
}
}
public class Main{
public static void main(String[] args){
Person person = new Student();
Person student = new Person (); // [X]
Student studnet = (Student)person; // 다운캐스팅
person.name = "김사과"; // [O]
person.no = 10; // [O]
}
}
부모를 상속해서 만들어진 자식 객체가 여러 타입인 경우 특정 클래스가 맞는지 확인하기 위해 사용하는 메소드
참조 변수가 참조하고 있는 인스턴스(객체)의 실제 타입을 알아보기 위해 사용
연산 결과는 boolean 값으로 반환
(자식이거나 친척이면 true로 나옴)
참조변수 instanceof 클래스명