상속
Object 클래스
package Object;
import java.lang.reflect.*;
import java.util.*;
public class Practice {
public static void main(String[] args) {
Class clz = Practice.class;
Class superClass = clz.getSuperclass();
System.out.println("부모 클래스 : "+ superClass);
Field[] fields = superClass.getDeclaredFields();
for(Field f: fields) {
System.out.println("전력변수 = "+ f.getName());
}
Method[] methods = superClass.getDeclaredMethods();
for (Method m : methods) {
String name = m.getName();
String parameterTypes = Arrays.toString(m.getParameterTypes());
System.out.println("함수 = "+ name+" : "+parameterTypes);
}
}
}
결과 :
부모 클래스 : class java.lang.Object 함수 = finalize : [] 함수 = wait : [long] 함수 = wait : [long, int] 함수 = wait : [] 함수 = equals : [class java.lang.Object] 함수 = toString : [] 함수 = hashCode : [] 함수 = getClass : [] 함수 = clone : [] 함수 = notify : [] 함수 = notifyAll : [] 함수 = registerNatives : [] |
- 부모 클래스 : class java.lang.Object
- 상속이 없으면 Object클래스가 부모 클래스임
- Object는 클래스들의 기본, 최상위 타입
- hashCode() 함수
- 객체에 해시코드 값을 반환하는 함수
- 해시코드란 : 객체를 구별하기 위한 값, 메모리 주소를 바탕으로 정수화된 값
- 객체의 해시코드 값의 중복이 일어날 가능성이 있음
-> 중복이란 말이 똑같은 곳 가리킨다는 것인가??? 근데 주의라고 적혀있으면 뭐지
- 객체의 해시코드 값의 중복이 일어날 가능성이 있음
- toString()함수
- 해당 객체의 정보를 문자열로 반환하는 함수
- 객체 정보 : '클래스명@해시코드(16진수로)' -> 대부분 재정의를 하여 사용함
package Object; public class PracticeHash_toString { public static void main(String [] args) { PracticeHash_toString a = new PracticeHash_toString(); int hashCode = a.hashCode(); // int to 16진수 : Integer.toHexString String hexHashCode = Integer.toHexString(hashCode); String toString = a.toString(); System.out.println("클래스정보 : " +toString + "\nHeshcode 값 : " + hexHashCode ); } }
- 결과
클래스정보 : Object.PracticeHash_toString@7d6f77cc
Heshcode 값 : 7d6f77cc
- 결과
super, protected
- 부모 클래스 : Practice_Parent
package Object;
public class Practice_Parent {
//전역 변수
protected String name = "parent";
public int value = 1;
private Type type = Type.A;
//enum 타입
public enum Type {A,B,C,D};
}
- 자식 클래스 : Practice_child
package Object;
public class Practice_child extends Practice_Parent{
//전역 변수
private int value = 2;
{
//비교1
System.out.println("name = " + name); // 부모 필드
System.out.println("value = " + value); // 자식 필드
//비교 2
System.out.println("super.name = " + super.name);
System.out.println("super.value = "+ super.value);
//비교 2
System.out.println("this.name = "+ this.name); //부모 필드
System.out.println("this.value = " + this.value); //자식필드
System.out.println("부모 클래스의 enum 접근" + Type.A);
}
public static void main(String[] args) {
new Practice_child();
}
}
- 결과
name = parent value = 2 super.name = parent super.value = 1 this.name = parent this.value = 2 부모 클래스의 enum 접근A |
- 자식에 없는 변수를 부모꺼 씀
- 만약 자식에도 변수가 있고, 부모에도 있다면 자식에서 사용시 자식꺼 먼저 사용함
- 부모꺼 사용하고 싶을 땐, super이용
- super없으면 부모보단 자식이 우선순위가 높음 (자식에서 사용할 때)
출처 :
자바 프로그래밍 100% 실전 가이드 세트(애프터스킬 시리즈)(전2권) | 심상원 | 아이콕스 - 교보문고 (kyobobook.co.kr)
'study > java' 카테고리의 다른 글
[Tomcat] 설치 및 eclipse 설정 (0) | 2022.01.01 |
---|---|
JAVA 상속(2) (0) | 2021.12.04 |
[백준] 2750_수 정렬하기 (java) (0) | 2021.11.13 |
java Ajax 연습하기 (0) | 2020.07.25 |
jfreechart bar그래프 두 개 그리기 (0) | 2020.07.23 |