티스토리 뷰
1. 상속을 UML로 표시하면?
class Man {
. . .
}
class BusinessMan extends Man {
. . .
}
위와 같은 파일이 있다면 UML은 아래와 같다.
2. 부모 클래스와 자식 클래스의 다른 용어들은?
▷ 상속의 대상이 되는(상속 해주는?) 부모 클래스, 상위 클래스, 기초 클래스
ex) Man 클래스
▷ 상속을 하는 클래스(상속 받는?) 자식 클래스, 하위 클래스, 유도 클래스
ex) BusinessMan 클래스
3. this 키워드와 super 키워드의 차이는 무엇인가요?
this 키워드는 자기 자신을 의미(자식 클래스)super 키워드는 자식 클래스에서 부모클래스를 불러올때 사용한다.
예시
package edu.kosmo.ex.practices;
class Man{
String name;
public void tellYourName() {
System.out.println("My name is " + name);
}
}
class BusinessMan extends Man{
String company;
String position;
public BusinessMan(String name, String company, String position) {
super(name);
this.company = company;
this.position = position;
}
public void tellYourInfo() {
System.out.println("My company is " + company);
System.out.println("My position is " + position);
tellYourName();
}
}
public class MyBusinessMan {
public static void main(String[] args) {
BusinessMan man
= new BusinessMan("YOON", "Hybrid ELD", "Staff Eng.");
man.tellYourInfo();
}
}
4. 단일 상속과 다중 상속 이란 무엇인가요? UML 로의 표기는?
자바는 다중 상속을 지원하지 않는다.
한 클래스에서 상속할 수 있는 최대 클래스의 수는 한 개이다(단일 상속).
ex) class B extends A, C → 이런 식으로 사용할 수 없다.
대신,
class B extends A
class c extends B
이런 식으로는 사용가능. 다중 상속이 아님.
다중 상속이 안되는 이유는 만약 A, C에 같은 이름을 가진 변수를 가졌을 때 B에서 둘을 다르게 인식하지 못하기 때문
또한 다중 상속을 받으면 프로그램이 너무 복잡해지기 때문.
5. 다음은 2차원 상의 한 점을 표현하는 Point 클래스이다.
Point를 상속받아 색을 가진 점을 나타내는 ColorPoint 클래스를 작성하라.
다음 main() 메소드를 포함하고 실행 결과와 같이 출력되게 하라.
class Point {
private int x, y;
public Point(int x, int y) { this.x = x; this.y = y; }
public int getX() { return x; }
public int getY() { return y; }
protected void move(int x, int y) { this.x =x; this.y = y; }
public static void main(String[] args) {
ColorPoint cp = new ColorPoint(5, 5, "YELLOW");
cp.setXY(10, 20);
cp.setColor("RED");
String str = cp.toString();
System.out.println(str+"입니다. ");
}
/*
=======================
RED색의 (10,20)의 점입니다.
*/
package edu.kosmo.ex.practices;
public class Point {
private int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
protected void move(int x, int y) {
this.x = x;
this.y = y;
}
public static void main(String[] args) {
ColorPoint cp = new ColorPoint(5, 5, "YELLOW");
cp.setXY(10, 20);
cp.setColor("RED");
String str = cp.toString();
System.out.println(str + "입니다.");
}
}
// RED색의 (10,20)의 점입니다.
class ColorPoint extends Point{
private String color;
public ColorPoint(int x, int y, String color) {
super(x, y);
this.color = color;
}
public void setXY(int x, int y) {
super.move(x, y);
}
public void setColor(String color) {
this.color = color;
}
public String toString() {
return color + "색의 " + "(" + getX() + "," + getY() + ")" + "의 점";
}
}
package edu.kosmo.ex.practices;
public class Point {
private int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
protected void move(int x, int y) {
this.x = x;
this.y = y;
}
public static void main(String[] args) {
ColorPoint cp = new ColorPoint(5, 5, "YELLOW");
cp.setXY(10, 20);
cp.setColor("RED");
String str = cp.toString();
System.out.println(str + "입니다.");
}
}
// RED색의 (10,20)의 점입니다.
class ColorPoint extends Point{
private String color;
public ColorPoint(int x, int y, String color) {
super(x, y);
this.color = color;
}
public void setXY(int x, int y) {
super.move(x, y);
// private라 super.x = x; 이런 식으로 바로 넣어 줄 수 없다.
}
public void setColor(String color) {
this.color = color;
}
public String toString() {
// String str = null;
// str == color + "색의 (" + super.getX() + "," + super.getY() + ")의 점"
return color + "색의 (" + getX() + "," + getY() + ")의 점";
}
}
6. 다음 main() 메소드와 실행 결과를 참고하여 TV를 상속받은 ColorTV 클래스를 작성하라.
다음 TV 클래스가 있다.
class TV{
private int size;
public TV(int size) { this.size = size; }
protected int getSize() { return size; }
}
public static void main(String[] args) {
ColorTV myTV = new ColorTV(32, 1024);
myTV.printProperty();
}
/*
===========
32인치 1024컬러
*/
package edu.kosmo.ex.practices;
class TV{
private int size;
public TV(int size) {
this.size = size;
}
protected int getSize() {
return size;
}
}
class ColorTV extends TV{
private int color;
public ColorTV(int size, int color) {
super(size);
this.color = color;
}
public void printProperty() {
System.out.println(super.getSize() + "인치 " + this.color + "컬러");
}
}
public class TVTest {
public static void main(String[] args) {
ColorTV myTV = new ColorTV(32, 1024);
myTV.printProperty();
}
}
// 32인치 1024컬러
☆ private > default > protected > public
default는 같은 패키지 안에서만 가능.(상속 받아도 다른 패키지라면 사용 불가)
protected는 다른 패키지라도 상속 받으면 사용가능.
7. 다음 main() 메소드와 실행 결과를 참고하여 ColorTV를 상속받는 IPTV 클래스를 작성하라.
public static void main(String[] args) {
IPTV iptv = new IPTV("192.1.1.2", 32, 2048); //"192.1.1.2" 주소에 32인치, 2048컬러
iptv.printProperty();
}
/*
=============================================
나의 IPTV는 192.1.1.2 주소의 32인치 2048컬러
*/
package edu.kosmo.ex.practices;
class TV{
private int size;
public TV(int size) {
this.size = size;
}
protected int getSize() {
return size;
}
}
class ColorTV extends TV{
private int color;
public ColorTV(int size, int color) {
super(size);
this.color = color;
}
public void printProperty() {
System.out.println(super.getSize() + "인치 " + this.color + "컬러");
}
}
class IPTV extends ColorTV{
private String adress;
public IPTV(String adress, int size, int color) {
super(size, color);
this.adress = adress;
} // super는 무조건 자기자신에 관련된 로직보다 무조건 위에 와야한다.
public void printProperty() {
System.out.print(this.adress + " 주소에 ");
super.printProperty();
}
}
public class TVTest {
public static void main(String[] args) {
ColorTV myTV = new ColorTV(32, 1024);
myTV.printProperty();
IPTV iptv = new IPTV("192.1.1.2", 32, 2048);
iptv.printProperty();
}
}
// 32인치 1024컬러
// 192.1.1.2 주소에32인치 2048컬러
super는 무조건 자기자신과 관련된 로직보다 무조건 위에 와야한다.
this.adress = adress;
super(size, color);
위 순서로 작성하면 컴파일 오류 발생.
'수업문제' 카테고리의 다른 글
[문제] 10월 25일 (instanceof, 상속) (0) | 2021.10.25 |
---|---|
[문제] 10월 22일 (다형성, polymorphism, 오버라이딩) (0) | 2021.10.22 |
[문제]10월 20일 (배열, arraycopy, for-each문) (0) | 2021.10.21 |
[문제] 10월 19일 (String) (0) | 2021.10.19 |
- Total
- Today
- Yesterday
- Request
- response
- 채팅
- el
- SOCKET
- equals
- 부트스트랩
- abstract
- 프로토콜
- compareTo
- string
- 사칙연산 계산기
- 래퍼 클래스
- 참조형
- Session
- JSP
- 쓰레드
- 제네릭
- exception
- 쿠키
- 예외처리
- 입출력
- 진척도 70번
- object
- TreeSet
- toString
- Servlet
- Generic
- hashset
- 세션
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |