class B1 { int x; }
class B2 extends B1 { String x; } //동일 class 동급
public class InhEx05 {
public static void main(String[] args) {
B2 b2 = new B2(); //cha cha 자식것만
b2.x = "자바 상속 extends";
System.out.println("b2.x : " + b2.x);
B1 b1 = new B1();
b1.x = 5000;
System.out.println("b1.x : " + b1.x);
}
}
b2.x : 자바 상속 extends
b1.x : 5000
InhEx06.java //static pa cha
class C1 {
static int x; //static 정적,공유되는,클래스명으로 접근
static int y;
}
//클래스 변수도 상속됨
class C2 extends C1 {
static String x;
}
public class InhEx06 {
public static void main(String[] args) {
C2.x = "알기쉽게 해설한 자바";
//C1 클래스에서 선언된 int x는 가려짐
C2.y = 20000;
C1.x = 30000;
System.out.println("C2.x : " + C2.x);
System.out.println("C2.y : " + C2.y);
System.out.println("C1.x : " + C1.x);
}
}
C2.x : 알기쉽게 해설한 자바
C2.y : 20000
C1.x : 30000
public class InhEx07 {
public static void main(String[] args) {
Pt pt = new Pt();
pt.display(); //외부에서 접근
Cd ch = new Cd();
ch.display();
Pt ch1 = new Cd();
ch1.display();
}
}
class Pt {
private int a = 10; //private로 Pt클래스 이내에서만. 외부 pt.으로 접근 불가
public int b = 20;
//1.pa cha. 2.외부접근 확인 3.접근제어자에 따라 내부접근 정리
void display() { //pt.display();외부에서 접근 후 {}는 해당 클래스 내에서 실행
System.out.println("Pt_a: " + a); //10
System.out.println("Pt_b: " + b); //20
System.out.println("====================="); //"====="
}
public int getA() { //외부접근 후 return내부접근
return a;
}
}
class Cd extends Pt {
public int c = 30;
void display() {
// System.out.printlin("Cd_a: " + a); 상속받은 private 필드 가려짐.
System.out.println("Cd_a: " + getA()); //getA() 소속없을시 부모클래스도 호출가능
System.out.println("Cd_b: " + b); //public이라 접근가능
// 상속받은 public 필드 참조
System.out.println("Cd_c: " + c); //public이라 접근가능
// 자식 클래스에서 선언한 public 필드 참조
System.out.println("=====================");
}
}
this 키워드 : 나의 인스턴스 this( ) 호출문 : 나의 인스턴스 안의 기본생성자 호출 this(매개변수...) 호출문 : 나의 인스턴스 안의 해당 케이스의 기타 생성자 호출
super 키워드 : 부모 인스턴스 super( ) : 부모 인스턴스의 기본생성자 호출(자동숨김) super(매개변수...): 부모 인스턴스 안의 해당 케이스의 기타 생성자 호출 (반드시 기술해야한다)
자식의 생성자 안에만 최초명령
Super02.java super, this
class One {
int d1;
int s;
One(){
System.out.println("One의 디폴트 생성자");
}
One(int s1) {
System.out.println("One의 생성자");
s = s1;
d1 = s * s;
}
}
class Two extends One {
int d2; //400
int t; //20
Two() {
this(10, 20); //Two(10,20)
System.out.println("Two의 디폴트 생성자");
}
Two(int s1, int t1) {//10, 20
super(s1); //주석처리 후 실행해 볼 것 //One(10);
System.out.println("Two의 생성자");
t = t1;
d2 = t * t;
}
}
public class Super02 {
public static void main(String[] args) {
Two super1 = new Two();
Two super2 = new Two(10, 20);
System.out.println("10의 제곱 : " + super2.d1);
System.out.println("20의 제곱 : " + super2.d2);
}
}
package inheritance;
public class CastingTest01 {
public static void main(String[] args) {
//레퍼런스 student를 이용하면 name, dept에 접근 가능
Student student = new Student("Duly");
System.out.println(student.name);
//Student 객체의 멤버 중 오직 Person 클래스의 멤버만 접근이 가능
Person person = student;
person.name = "Gogildong";
System.out.println(person.name);
//아래 문장은 컴파일 타임 오류
//person.dept = "Computer Eng"; //Pa-Cha라서 외부접근 불가
// ((Student)person).dept = "Computer Eng";
//외부 접근 필요시 일시적으로 다운캐스팅(다운할 클래스의 자료가 있어야 가능)후 호출하면 가능
//System.out.println(person.dept);
}
}
class Person {
String name;
public Person(String name) {
this.name = name;
}
}
class Student extends Person {
String dept;
public Student(String name) {
super(name); //생략해서 super()로 호출하더라도 Pa에서 기본생성자가 없기때문에 오류남
}
}
Duly
Gogildong
CastingTest02.java
package inheritance;
public class CastingTest02 {
public static void main(String[] args) {
Guest guest = new Guest();
Food foodone = new Food();
guest.foodEat(foodone);
foodone = new Jjigae();
guest.foodEat(foodone);
foodone = new Tang();
guest.foodEat(foodone);
}
}
class Food {
public void cook() {
System.out.println("Food 간맞추기");
}
}
class Jjigae extends Food {
public void cook() {
System.out.println("Jjigae 간맞추기");
}
}
class Tang extends Food {
public void cook() {
System.out.println("Tang 간맞추기");
}
}
class Guest {
//public Guest(){} 숨겨져있음
public void foodEat(Food somefood) {
System.out.println("Guest 간맞추기");
somefood.cook();
}
}