1. 추상클래스, 추상메소드

 

추상클래스

- 추상메소드를 포함하는 클래스

-객체를 생성할 수 없고 오로지 상속의 목적으로 만든다.

- 만드는 형식:클래스명 앞에 abstract를 붙여 만든다.

 

추상메소드

- body(몸체)를 갖지 않는 메소드

- 추상메소드는 자식클래스에서 반드시 오버라이딩해야 한다.

- 만드는 형식:메소드명앞에 abstract를 붙이고 body부분을 만들지 않는다.

 

예)

//추상클래스
abstract class MyParent
{
     //추상메소드
     public abstract void print();
        ...
}

- 만드는 이유:자식클래스에서 공통으로 구현해야 할 기능의 메소드를 가져야 하는 경우에 추상메소드를 사용한다.
 (자식에게 강제적으로 메소드를 오버라이딩하도록 제약을 줄 수 있다.)

 

ex>

//추상클래스
abstract class MyShape
{
    private String color;

    public MyShape(String color){
        this.color=color;
    }
    public String getColor(){
        return color;
    }

    //추상메소드
    public abstract void draw();
}
class MyRect extends MyShape
{
    public MyRect(String color){
        super(color);
    }

    //추상메소드를 반드시 오버라이딩 해야 한다!
    public void draw(){
        System.out.println("사각형 그리기");
    }
}
class MyCircle extends MyShape
{
    public MyCircle(String color){
        super(color);
    }

    //추상메소드를 반드시 오버라이딩 해야 한다!
    public void draw(){
        System.out.println("타원 그리기");
    }
}
class Test03_abstract
{
    public static void main(String[] args)
    {
        //new MyShape();
         //==> 오류발생:객체를 생성할 수 없다.
        MyRect mr=new MyRect("빨강");

        mr.draw();

        MyShape ms=mr;    //가능!
    }
}

 

 

 

2.  인터페이스(**)

 

- 자식클래스들이 가져야 기능들의 목록을 갖음(메뉴판)

 

- 상수와 추상메소드들로만 이루어진다. (인터페이스는 빈껍데기)

 

- 만드는 형식
      interface 인터페이스명{
          상수;
          ..
          추상메소드();
          ..
      }

 

- 인터페이스는 객체를 생성할 수는 없지만 자식객체를 참조할 수는 있다.

 

- 사용이유
1) 자식클래스들이 가져야 할 기능들의 뼈대를 제공한다.

2) 클래스에서는 다중상속이 지원되지 않지만 인터페이스는 다중상속이 가능하다.

3) 인터페이스를 적절히 사용하므로써 유지보수가 수월해 진다.

 

 

ex>

//도형이 구현해야 할 기능들의 목록을 지정
interface Shape        
{      
    public static final int RED=1;

    //public static final 이 생략됨
    int BLUE=2;

    public abstract void draw();

    //public abstract 이 생략됨
    void paint();
    void resize();
}
//인터페이스를 상속받을때 implements를 사용!
class Rectangle implements Shape
{   
    public void draw(){
        System.out.println("사각형그리기");
    }

    public void paint(){
        System.out.println("사각형칠하기");
    }
    public void resize(){
        System.out.println("사각형 크기 바꾸기");
    }
}
class Test04_interface{

    public static void main(String[] args)    
    {
        //Shape sh=new Shape();
        Shape sh=new Rectangle();
        sh.draw();
        sh.paint();
        System.out.println("RED:"+Shape.RED);
    }
}

 

 

ex> 중요!! 여러번 눈에 익히자.

interface MyDbms{
    //db접속기능
    void connect();

    //db접속해제
    void disconnect();

    //sql명령어 실행
    void execute(String sql);
}
class Oracle implements MyDbms{

    public void connect(){
        System.out.println("오라클 DBMS와 연결됨!");
    }
    public void disconnect(){
        System.out.println("오라클 DBMS와 연결이 해제됨!");
    }
    public void execute(String sql){
        System.out.println("오라클 명령어를 사용해 " + sql +"을 실행함");
    }
}
class MySQL implements MyDbms{

    public void connect(){
        System.out.println("MySQL DBMS와 연결됨!");
    }
    public void disconnect(){
        System.out.println("MySQL DBMS와 연결이 해제됨!");
    }
    public void execute(String sql){
        System.out.println("MySQL 명령어를 사용해 " + sql +"을 실행함");
    }
}
class  Test05_interface{
    public static void main(String[] args)
    {
        MyDbms db=new Oracle();
        db.connect();
        db.execute("회원추가");
        db.disconnect();
    }
}

 

 

ex> //인터페이스는 다중상속이 가능하다.

interface Shape{
    void draw();
    void paint();
}
interface Point{

    void setPoint(int x,int y);
}

//인터페이스끼리는 extends를 사용한다.
interface Rect extends Shape,Point       
{
    void resize();
}
class MyRect implements Rect
{
    private int x,y;

    public void draw(){
        System.out.println(x+","+y+"의 좌표에 사각형 그리기");
    }
    public void paint(){
        System.out.println("사각형칠하기");
    }
    public void setPoint(int x,int y){
        this.x=x;
        this.y=y;
    }
    public void resize(){
        System.out.println("사각형크기 바꾸기");
    }
}
class Test06_interface
{
    public static void main(String[] args)
    {
        MyRect mr=new MyRect();
        mr.setPoint(100,200);
        mr.paint();
        mr.draw();
    }
}

 

 


3.  Object : 모든 클래스의 부모클래스

 

class Person{

    private String name;
    private int age;
   
    public Person(String name,int age)
    {
        this.name=name;
        this.age=age;
    }
    public String getName()
    {
        return name;
    }
    public int getAge()
    {
        return age;
    }
    public String toString()
    {
        String aa="name:"+name +",age:"+age;
        return aa;
    }
}
class Test07_Object
{
    public static void main(String[] args)
    {
        Object ob1=new Object();
        //Object ob2=new Object();
        Object ob2=ob1;

        if(ob1==ob2)
        {
            System.out.println("두객체는 같아요1");
        }
        if(ob1.equals(ob2))
        {
            System.out.println("두객체는 같아요2");
        }
        //public String toString()
        String aa=ob1.toString();
        System.out.println(aa);

        Person per=new Person("홍길동",10);
        //String bb=per.toString();

        //name:홍길동,age:10
        System.out.println(per);

        String cc="안녕하세요";
       
        Integer in=new Integer(100);
        //String클래스는 toString메소드를 xxx했다.
        System.out.println("cc:" + cc + ",in:"+ in);
    }
}

 

 

ex>

class Person{

    private String name;
    private int age;

    public Person(String name,int age)
    {
        this.name=name;    this.age=age;
    }
    public String getName()
    {
        return name;
    }
    public int getAge()
    {
        return age;
    }
    public boolean equals(Object obj)
    {
        Person pp=(Person)obj;

        if(name.equals(pp.name) && age==pp.age)
        {
            return true;
        }else{
            return false;
        }
    }
}
class Test08_Object{

    public static void main(String[] args)
    {
        Person per=new Person("홍길동",10);
        Person per1=new Person("홍길동",10);

        if(per.equals(per1)){
            System.out.println("두사람은 같아요!");
        }else{
            System.out.println("두사람은 달라요!");
        }

        String str=new String("안녕");
        String str1=new String("안녕");

        if(str==str1)
        {
            System.out.println("두문자열은 같아요1!");
        }
        if(str.equals(str1))
        {
            System.out.println("두문자열은 같아요2!");
        }
    }
}

 

 

 

 

 

'JAVA' 카테고리의 다른 글

lang 패키지  (0) 2014.09.12
패키지  (0) 2014.09.12
클래스간 형변환 / 다형성  (0) 2014.09.12
오버라이딩 / final의 용도  (0) 2014.09.12
상속  (0) 2014.09.12

+ Recent posts