✨
Java
  • 자바의 역사
  • Ch1
    • Before Start Java
      • History of Java
      • Feature of Java
      • API Documentation
      • Hello World!
      • eclipse shortcut
      • Eclipse IDE
      • GitHub to eclipse
  • Ch2
    • Variable
      • Variable
    • Variable Type
    • Character and String
    • Primitive type and range
    • printf and formatter
    • Scanner
    • Overflow of Int
    • Transition between different variable types
    • Object Array
  • CH3
    • Operator
  • CH4
    • 조건문과 반복문
    • if statement
    • switch
    • Math.random()
    • for statement
    • while statement
    • break, continue, named iterated statement
  • CH5
    • Array
    • 배열 활용
    • String Array
  • OOP
    • Intro
  • Class and Object
  • Make multiple Classes in one file
  • How to use Object
  • Object Array
  • Variable type according to declared location
  • Class Variable and Instance Variable
  • Method
  • Call Stack
  • Parameter
  • Static method(Class method) and Instance method
  • (Method)Overloading
  • Constructor
  • Constructor this()
  • Reference type variable "this"
  • Initialize variable
  • Inheritance
  • Composite
  • Single Inheritance, Object Class
  • (Method)Overriding
  • super, super()
  • package, class path
  • import, static import
  • modifier
  • access modifier
  • Encapsulation
  • Polymorphism
  • reference type transition
  • instanceof operator
  • Parameter polymorphism
  • Multiple object using one array
  • Abstract Class, Abstract Method
  • Creating Abstract Class
  • Interface
  • Interface Polymorphism
  • Interface Pros
  • Default method and static method
  • Inner Class
  • Anonymous Class
  • java.lang package and useful class
    • Object class
    • hashCode(), toString()
    • String class
    • StringBuffer class
    • StringBuilder class
    • Math class
    • Wrapper class
    • Number class
    • String to Number, String to Wrapper class
    • Auto-boxing and (auto)Unboxing
  • Collection Framework
    • Collections framework
    • List, Set, Map Interface
    • List의 removeAll() 과 clear() 비교
    • List Interface - ArrayList
    • How to view Java API source
    • List Interface - LinkedList
    • Stack and Queue
    • Iterator, ListIterator, Enumeration
    • Array
    • Comparator와 Comparable
    • Stack
    • String
    • String + char = String
    • String.toCharArray()
    • BufferedReader / BufferWriter
    • Scanner로 String 입력 - next( )와 nextLine( )
    • 공백없이 2차원배열 데이터 String으로 한번에 한줄씩 입력받기(문자/숫자)
    • 공백을 사이에 두고 빠른 2차원 배열 입출력
    • arr[index++]과 arr[index] index++의 차이
    • int와 long의 차이
    • Untitled
    • 타입 간의 변환
    • Array 와 ArrayList
    • valueOf()
    • Char
    • 변수, 객체 초기화(초기값 설정)
  • error troubleshooting
    • No enclosing instance of type
    • ASCII Code Table
    • java.lang.NumberFormatException
    • No enclosing instance..
  • reference
    • String을 생성하는 2가지 방법과 차이점
    • StackOverflowError(스택, 힙 메모리)
    • swtich-case 반복문
Powered by GitBook
On this page

Was this helpful?

Interface Pros

PreviousInterface PolymorphismNextDefault method and static method

Last updated 4 years ago

Was this helpful?

  1. 변경에 유리하며 유연한 코드가 된다.

-두 대상(객체) 간의 '연결,대화,소통'을 돕는 중간 역할을 한다.

-선언(설계, 껍데기)와 구현(알맹이)을 분리시킬 수 있게 한다.

ex) 강한 결합, 변경에 불리

class B {
    public void method() {
        System.out.println("methodInB");
    }
}

ex) 인터페이스를 이용한 느슨한 결합, 변경에 용이

interface I {
    public void method();
}
class B implements I {
    public void method() {
        System.out.println("methondInB");
    }
}

사용자는 자판기를 사용한다. 자판기의 내용물(음료수/과자/샐러드 등)이 바뀐다.

class A {
    public void method(I i) {
        i.methodB();
    }
}
//선언부와 구현을 분리.(껍데기와 알맹이를 분리하여 사용한다.)
//1. 메서드 선언(껍데기)
interface I{public abstract void method();}
//2. 메서드 구현(알맹이)
class B implements I {
    public void method() {
        System.out.println("B클래스의 메서드 in interface I");
    }
}

class C implements I {
    public void method() {
        System.out.println("C클래스의 메서드 in interface I");
    }
}
//main
public class InterfaceTest {
    public static void main(String[] args) {
        A a = new A();
        //class A does not need to modify code.
        a.method(new B());
        a.method(new C());
        //C c = new C();
        //a.method(c);
    }
}

2. 개발 시간을 단축할 수 있다. A->I(B).

A는 B가 완성될 때까지 기다리지 않고도 코드를 작성할 수 있다. B를 구현하지 않아도 Interface I 안에 B가 있다면 인터페이스(I)는 추상메서드의 집합이고 껍데기이다. 추상메서드 B를 구현하면 된다.

Q. iv는 어떻게 사용할 수 있는가?

A. (캡슐화)iv 직접 접근은 불가하며 private 제어자를 통해 메서드를 통해서만 접근할 수 있도록 한다. .

3. 변경에 유리한 유연한 설계가 가능하다. A->I(B), A->I(C), A->I(D)

4. 표준화가 가능하다. Java Application->JDBC(인터페이스 집합)->Oracle_DB , Java Application->JDBC->MySQL_DB

추후 JSP나 Spring을 배우면 JDBC를 다루게 된다.

5. 서로 관계없는 클래스들을 관계를 맺어줄 수 있다.

상속 계층도에서 공통점을 찾기 어려울 때, 공통적인 부분들을 골라서 인터페이스를 구현하게 한다.

//multiple overloading
void repair(Tank t) {...}
void repair(SCV s) {...}
void repair(Dropship d) {...}
//parameter is the class which implments Repairable interface
void repair(Repairable r) {
    if(r instanceof Unit) {//instanceof : check casting
        Unit u = (Unit)r;
        while(u.hitPoint != u.MAX_HP) {
            u.hitPoint++;//Unit의 HP를 증가시킨다.
        }
    }
}

interface Repairable {}

class SCV extends GroupUnit implements Repairable {...}
class Tank extends GroupUnit implements Repairable {...}
class Dropship extends AirUnit implements Repairable {...}