์ํ์ ๋ง๋ค์ด๋๊ณ ๋์ค์ ๊ฐ์ ธ๋ค๊ฐ ์ฐ๊ธฐ ์ํ ์ฉ๋.
์ถ์ ๋ฉ์๋๋ฅผ ๋ฐ์ ๊ตฌํํ๋ ค๋ ์๋ธ ํด๋์ค์์๋ ๋ฌด์กฐ๊ฑด ์ค๋ฒ๋ผ์ด๋ฉ์ ํด์ผ ํ๋ค.(๊ตฌํ ์ ํ ๊ฒฝ์ฐ ๊ทธ ํด๋์ค๋ abstract๋ฅผ ๋ถ์ฌ์์ ์ถ์ ํด๋์ค๋ก ๋ง๋ค์ด์ค์ผ ํ๋ค.)
abstract class Shape{
abstract void draw();
}
class Line extends Shape{
@Override
void draw() {
System.out.println("Line");
}
}
class Circle extends Shape{
@Override
void draw() {
System.out.println("Circle");
}
}
class Rect extends Shape{
@Override
void draw() {
System.out.println("Rect");
}
}
public class AbstractEx {
public static void main(String[] args) {
//Shape s = new Shape(); ์ถ์ํด๋์ค์ ๋ํด์๋ ๊ฐ์ฒด์์ฑ๊ธ์ง
Shape s = new Circle();
s.draw();
}
}
์์
abstract class Calc{
abstract int add(int a, int b);
abstract int sub(int a, int b);
abstract double avg(int[] a);
}
public class GoodCalc extends Calc {
@Override
int add(int a, int b) {
return a+b;
}
@Override
int sub(int a, int b) {
return a-b;
}
@Override
double avg(int[] a) {
int sum = 0;
for(int i=0; i<a.length; i++)
sum += a[i];
return sum/(double)a.length;
}
public static void main(String[] args) {
GoodCalc gc = new GoodCalc();
int []arr = {10,20,30,40,50};
System.out.println("gc.add(2,3): "+gc.add(2, 3));
System.out.println("gc.sub(20,3): "+gc.sub(20, 3));
System.out.println("gc.avg(arr): "+gc.avg(arr));
}
}
728x90
๋ฐ์ํ
0