추상메서드
: 선언부만 정의하고 구체적인 내용은 비워 놓은 메서드
추상메서드가 있는 클래스를 상속받는 자식클래스는 반드시 부모클래스의 추상메서드를 구현해야 한다
abstract 리턴타입 메서드명();
구현하지 않으므로 {} 생략하고 ; 으로 마무리
추상클래스
: 추상메서드를 멤버(필드)로 갖는 클래스
추상 클래스는 일반 메서드 필드에 존재 가능(단, 추상 메서드를 하나라도 포함해야 한다)
객체를 생성할 수 없다
abstract class 클래스명 {
...
abstract 리턴타입 메서드명();
}
구현하지 않으므로 {} 생략하고 ; 으로 마무리
추상 클래스 사용 목적
자식 클래스가 구현하도록 메서드의 기능을 비워놓고 싶을 때 사용
ex.
도형 class - draw();
사각형 class - draw(); 사각형을 그리다
원 class - draw(); 원을 그리다
선 class - draw(); 선을 그리다
Exercise 1
public class ShapeMain {
public static void main(String[] args) {
Shape shapes[] = new Shape[3];
// 부모의 객체배열 요소에 자식 값 넣기 -> 업캐스팅
shapes[0] = new Rect();
shapes[1] = new Circle();
shapes[2] = new Line();
for (int i=0; i<3; i++) {
shapes[i].draw();
}
}
}
/*
class Shape {
void draw () {
System.out.println("도형을 그리다");
}
}
-> 부모클래스인 Shape의 draw 메서드를 구현은 해 주어야 하지만 그 값이 무의미한 경우
abstract 를 사용해 메서드만 구현하고 값은 입력하지 않는다 (추상클래스)
이 경우 자식클래스에서 추상클래스의 값을 반드시 구현해야 한다
*/
// ==
abstract class Shape {
abstract void draw();
}
class Rect extends Shape {
void draw () {
System.out.println("사각형을 그리다");
}
}
class Circle extends Shape {
void draw () {
System.out.println("원을 그리다");
}
}
class Line extends Shape {
void draw () {
System.out.println("선을 그리다");
}
}
출력화면
Exercise 2
public class AbstractClass {
public static void main(String[] args) {
Pokemon pokemon = new Pikachu();
System.out.println("이 포켓몬은 " + pokemon.getName());
pokemon.attack();
pokemon.sound();
pokemon = new Squirtle(); // 변수를 여러 개 생성할 필요 없이 순차적으로 사용해 출력
System.out.println("이 포켓몬은 " + pokemon.getName());
pokemon.attack();
pokemon.sound();
}
}
abstract class Pokemon {
String name;
abstract void attack();
abstract void sound();
public String getName() {
return this.name;
}
}
class Pikachu extends Pokemon {
Pikachu (){
this.name="피카츄";
}
@Override
void attack() {
System.out.println("백만볼트");
}
@Override
void sound() {
System.out.println("피카-츄!");
}
}
class Squirtle extends Pokemon {
Squirtle (){
this.name="꼬부기";
}
@Override
void attack() {
System.out.println("물대포");
}
@Override
void sound() {
System.out.println("꼬북꼬북");
}
}
출력화면
JAVA Eclipse 01 프로그램, 프로그래밍, 기계어, JAVA
https://developernew.tistory.com/63
JAVA Eclipse 02 자바 출력메서드와 입력메서드
https://developernew.tistory.com/71
JAVA Eclipse 03 변수, 자료형, 형변환, 변수의 상수화
https://developernew.tistory.com/74
JAVA Eclipse 04 연산자 정의, 연산자 종류, 연산자 우선순위
https://developernew.tistory.com/78
JAVA Eclipse 05 논리연산자, 비트연산자
https://developernew.tistory.com/80
JAVA Eclipse 06 기타연산자 - 삼항연산자, 대입연산자, 복합대입연산자, instanceof
https://developernew.tistory.com/84
JAVA Eclipse 07 제어문 : 조건문
https://developernew.tistory.com/88
JAVA Eclipse 08 제어문 : 조건문 switch + Random 클래스
https://developernew.tistory.com/93
JAVA Eclipse 09 제어문 : 반복문 for
https://developernew.tistory.com/102
JAVA Eclipse 10 제어문 : 반복문 while, do-while
https://developernew.tistory.com/103
JAVA Eclipse 11 제어문 : 반복문의 break, continue
https://developernew.tistory.com/106
JAVA Eclipse 12 배열 : 배열의 개념 및 사용
https://developernew.tistory.com/107
JAVA Eclipse 13 배열 : 예제풀이, 로또번호 생성기
https://developernew.tistory.com/115
JAVA Eclipse 14 배열 : 다차원배열
https://developernew.tistory.com/117
JAVA Eclipse 15 배열의 복제, for ~ each문
https://developernew.tistory.com/120
JAVA Eclipse 16 카페 주문 시스템(키오스크) 배열과 제어문(반복문, 조건문)으로 풀기
https://developernew.tistory.com/124
JAVA Eclipse 17 method 메서드(메소드)
https://developernew.tistory.com/126
JAVA Eclipse 18 메서드(메소드) 오버로딩
https://developernew.tistory.com/133
JAVA Eclipse 19 객체지향 언어
https://developernew.tistory.com/135
JAVA Eclipse 20 클래스와 객체
https://developernew.tistory.com/155
JAVA Eclipse 21 인스턴스 변수와 클래스 변수
https://developernew.tistory.com/156
JAVA Eclipse 22 객체 타입 배열
https://developernew.tistory.com/157
JAVA Eclipse 23 클래스 생성자
https://developernew.tistory.com/158
JAVA Eclipse 24 상속과 다형성 - 상속/메서드 오버라이딩
https://developernew.tistory.com/159
JAVA Eclipse 25 상속과 다형성 - 다형성 개념과 다형성 관련 실습
https://developernew.tistory.com/160
'Backend > JAVA2 멘토시리즈' 카테고리의 다른 글
JAVA Eclipse 28 내부클래스 (0) | 2023.05.15 |
---|---|
JAVA Eclipse 27 인터페이스 (0) | 2023.05.12 |
JAVA Eclipse 25 상속과 다형성 - 다형성 개념과 다형성 관련 실습 (1) | 2023.05.11 |
JAVA Eclipse 24 상속과 다형성 - 상속/메서드 오버라이딩 (0) | 2023.05.11 |
JAVA Eclipse 23 클래스 생성자 (0) | 2023.05.09 |