1. 추상화의 이해
  - 구체적인 개념으로부터 공통된 부분들만 추려내어 일반화 할 수 있도록 하는 것을 의미
  - 일반적으로 사용할 수 있는 단계가 아닌 아직 미완성적 개념.
  - 적용분야 : 메소드, 클래스

 

* 추상메소드의 구성
  - 선언부만 작성하고 구현부는 작성하지 않은채로 남겨둔것. 몸통만 있음.
  - 꼭 필요하지만 자손마다 다르게 구현될 것으로 예상되는 경우에 주로 사용.
  - 추상메소드를 하나라도 가지게 되는 클래스가 추상클래스
  - { } 생략
  - 추상 클래스는 추상 메소드가 1개이상 선언되어 있다.
  - 추상 클래스는 객체를 생성할 수 없다.
  - 추상 클래스는 반드시 상속받아서 자식클래스에서 사용해야 한다.
  - 추상클래스가 객체를 생성하려면 추상 메소드를 Overriding(재정의) 해야 한다.


* 이클립스에서 자동생성 기능 ctrl + space bar

 

2. 다형성
 - 어떤 값을 할당할 때 좌변(장소)과 우변(값,객체)은 데이터 타입이 일치해야함.
 - 어떤 타입이 다른 타입의 값을 할당 받는 기능
 - 숫자와 비슷한 방법으로 클래스도 형변환 가능
 - 부모클래스로 자식객체를 생성할 수 있다.

 

=============================================================

 

package oop6abstract;

abstract class Travel {
    int bb=10;
    public void pl(String str){
        System.out.println(str);
    } 
    public void pl(){
        System.out.println("----------------------");
    }
    public abstract String travelWhere(); //추상메소드
}//추상클래스

class Type1 extends Travel{
    public String travelWhere()
    {
        return "제주도 올레길 투어";
    }//추상메소드 구현 overriding
}
//============================
class Type2 extends Travel{
    @Override
    public String travelWhere() {
        return "지리산 둘레길 투어";
    }
   
}
//===========================
class Type3 extends Travel{
  @Override
  public String travelWhere() {
      return "여의도 봄꽃축제";
  }
 
}
//===========================
class Type4 extends Travel{
    @Override
    public String travelWhere() {
        return "안양 충훈부 벚꽃축제 (1-2)";
    } 
}
//===========================
class Type5 extends Travel{
    int a=10;
    @Override
    public String travelWhere() {
        return "해외여행!!!";
    } 
  }
//============================
public class AbstractEx {

    public static void main(String[] args) {
        //Travel travel=new Travel();
        //추상클래스는 객체생성 할수 없다
/*
        Type1 one=new Type1();
        one.pl("여행가자");
        System.out.println(one.travelWhere());
        one.pl();
       
        Type2 two=new Type2();
        two.pl("두번째 여행");
        two.pl(two.travelWhere());
*/       
        //다형성 예제
        Travel travel=new Type3();
        travel.pl(travel.travelWhere());  //여의도
       
        travel=new Type4();
        travel.pl(travel.travelWhere());//안양

        travel=new Type1();
        travel.pl(travel.travelWhere());//제주
       
        travel=new Type2();
        travel.pl(travel.travelWhere());//지리산
       
        //Type1 type=new Travel();에러
        //자식클래스는 부모클래스 객체를 생성할수는 없다
       
        travel=new Type5();
        //travel.aa=20; 에러
        //travel.bb=20; 에러아님

       
    }

}

 

=========================================================

 

파일명 : Product.java

 

package oop6abstract;

public abstract class Product {
    // 추상 메소드(각각의 클래스마다 고유하게 구현)
    public abstract void print();

    // 일반 메소드(공통으로 공유)
    public void line1(){
        System.out.println("---------------------------");
    }   
    public void line2(){
        System.out.println("***************************");
    }   
    public void copyright(){
        System.out.println("Created By GREEN 2012");
    }   
    public void pl(String str){
        System.out.println(str);
    }

 

 

파일명 :  Pc.java

 

package oop6abstract;

public class Pc extends Product{
    private String name;
    private String cpu;
    private String ram;
   
    public Pc() {}

    public Pc(String name, String cpu, String ram) {
        this.name = name;
        this.cpu = cpu;
        this.ram = ram;
    }

    public void print()
    {
        System.out.print(this.name+" ");
        System.out.print(this.cpu+" ");
        System.out.print(this.ram+" ");
        System.out.println();
    }
}

}

 

파일명 :  Lcd.java

 

package oop6abstract;

public class Lcd extends Product{
    private String name;
    private String inch;
    //기본생성자
    public Lcd() {}   
    //private속성의 멤버변수 초기화해주는 생성자
    public Lcd(String name, String inch) {
        this.name = name;
        this.inch = inch;
    }   
    //override구현, 멤버변수 출력 메소드
    public void print()
    {
        System.out.print(this.name+" ");
        System.out.print(this.inch+" ");
        System.out.println();
    }
}

파일명 : Notebook.java

 

package oop6abstract;

public class Notebook extends Product{
    private String name;
    private String hdd;
    public Notebook() {}
    public Notebook(String name, String hdd) {
        this.name = name;
        this.hdd = hdd;
    }
    //override구현, 멤버변수 출력 메소드
    public void print()
    {
        System.out.print(this.name+" ");
        System.out.print(this.hdd+" ");
        System.out.println();
    }
}

 

파일명 : AbstractEx2.java

 

package oop6abstract;

public class AbstractEx2 {

    public static void main(String[] args) {
        Product product = new Pc("고성능 PC", "i5", "2GB");
        //호출할 수 있는 메소드는 부모클래스 Product 클래스안에 있는
        //메소드와 오버로딩된 메소드만 가능
        product.line1();
        product.print();
        product.line2();
        product.copyright();
       
        product=new Lcd("LCD모니터","19인치");
        product.line1();
        product.print();
        product.line2();
        product.copyright();

        product=new Notebook("SAMSUNG","60기가");
        product.line1();
        product.print();
        product.line2();
        product.copyright();
    }
}

 

 

 

 

'..열심히 공부하세.. > JAVA 문법' 카테고리의 다른 글

[30] Interface 구현  (0) 2012.05.03
[29] 내부클래스  (0) 2012.05.03
[Buyer클래스 예제]  (0) 2012.05.01
[27] super, super()  (0) 2012.05.01
[26] 상속관계에서의 생성자  (0) 2012.05.01

+ Recent posts