문서의 이전 판입니다!
Phone.java : 부모 클래스
package chapter08;
public class Phone {
String name;
String color;
String company;
void call() {
System.out.println("전화를 건다");
}
void receive() {
System.out.println("전화를 받다");
}
}
SmartPhone.java : 자식 클래스
package chapter08;
public class SmartPhone extends Phone {
public void installApp() {
System.out.println("앱 설치");
}
}
SmartPhoneMain.java
package chapter08;
public class SmartPhoneMain {
public static void main(String[] args) {
Phone p = new Phone();
p.name = "전화기";
p.company = "현대";
p.color = "화이트";
System.out.println("Phone 출력");
System.out.println(p.name);
System.out.println(p.company);
System.out.println(p.color);
p.call();
p.receive();
SmartPhone sp = new SmartPhone();
sp.name = "갤럭시";
sp.company = "삼성";
sp.color = "블랙";
System.out.println("SmartPhone 출력");
System.out.println(sp.name);
System.out.println(sp.company);
System.out.println(sp.color);
sp.call();
sp.receive();
sp.installApp();
}
}
package chapter08;
public class SuperEx {
public static void main(String[] args) {
Child child = new Child();
child.print();
}
}
class Parent {
int number = 3;
Parent() {
System.out.println("부모 객체 생성");
}
}
class Child extends Parent {
//int number = 2;
Child() {
System.out.println("자식 객체 생성");
}
void print() {
int number = 1;
System.out.println(number); // 메서드 지역변수 number
System.out.println(this.number); // 자신 객체의 number
System.out.println(super.number); // 부모 객체의 number
}
}
package chapter08;
public class Child extends Parent {
// int number = 2;
Child(){
System.out.println("자식 객체 생성");
}
viod print() {
int number = 1;
System.out.println(number); // 메서드 지역변수 number
System.out.println(this.number); // 자신 객체의 number
System.out.println(super.number); // 부모 객체의 number
}
}
super()
package chapter08;
public class SuperEx2 {
}
class Parent2 {
String name;
Parent2(String name) {
this.name = name;
}
}
class Child2 extends Parent2 {
Child2(String name) {
super(name);
}
}
8.3 다형성
별도로 명시하지 않을 경우, 이 위키의 내용은 다음 라이선스에 따라 사용할 수 있습니다: CC Attribution-Share Alike 4.0 International