본문 바로가기
자바

java 복습 3

by BIGENGINEER 2024. 1. 14.

 

1. 인터페이스

- 인터페이스는 interface 키워드를 사용하여 정의됨.

- 인터페이스는 메소드의 선언만을 가지며, 멤버 변수는 상수만을 가질 수 있음.

public interface TV{
	public void turnOn();
}

 

- 인터페이스는 구현은 없고 어떤 것을 가지고 있는지만 선언하기 때문에 abstract 없어도 됨.

- 즉, 인터페이스가 객체를 생성하지는 못함.

 

1)

public class LedTV implements TV{ // TV 라는 인터페이스가 가진 기능들을
 LedTV도 모두 갖게 하겠다.

.
.
.
}

 

- LedTV는 TV가 가진 모든 기능들을 구현해야만 함.

- 인터페이스는 클래스에 의해 구현되며, 클래스가 인터페이스를 구현하면,

해당 클래스는 인터페이스에 정의된 모든 메소드를 구현해야 함.

 

2)

public static void main(String[] args){
	TV tv = new LedTV();
    tv.turnOn();
    ...
}

 

- 참조변수의 타입으로 인터페이스를 사용할 수 있음.

- > 인터페이스가 가지고 있는 메서드만 사용 가능

 

=> 즉, 인터페이스는 타입은 가능하나, tv가 갖고 있는 기능들만 사용 가능함.

 

- 동일한 인터페이스를 구현함 = 클래스의 사용법이 같음

 

!!! 인터페이스에서 정의한 변수는 모두 상수이기 때문에 변경할 수 없음.

 

 

3)

public interface Interface1 {
    void method1();
}

public interface Interface2 {
    void method2();
}

public class MyClass implements Interface1, Interface2 {
    @Override
    public void method1() {
        // 구현
    }

    @Override
    public void method2() {
        // 구현
    }
}

 

- 인터페이스는 다중 상속이 가능하며, 하나의 클래스가 여러 개의 인터페이스를 구현할 수 있음.

 

 

 

2. 인터페이스의 default method

- 기존의 인터페이스는 추상 메서드만 가질 수 있었음.

- 인터페이스가 default라는 키워드로 선언이 되면 메서드를 구현할 수 있음.

 

public interface Calculate {
	public int multiple(int i, int j);
    
    default int exec(int i, int j){
    	return i+j;
    } // 메소드 구현 가능

 

 

-> why 사용?

- 인터페이스가 변경이 되면 그 인터페이스를 구현하고 있는 모든 클래스들이 

해당 메서드를 구현해야하는 문제점이 있음.

 

public interface Calculate {
	public int multiple(int i, int j);
    
    default int exec(int i, int j){
    	return i+j;
    } // 메소드 구현 가능
    
    public static int exec2(int i, int j){
    	return i*j;
    }
 }

 

- 인터페이스에서 정의한 static 한 메서드는 반드시 <인터페이스명.메서드> 형식으로 호출해야함.

- > Calculate.exec2(3,4);

( 참조변수.메서드) 형식으로는 사용할 수 없음.

 

 

 

3. 내부클래스

- 클래스 안에 선언된 클래스

 

1) 중첩 클래스

public class innerE{
	class Cal{
    	int v = 0;
        public void plus(){
        	v++
        }
    }
    
    public static void main(String[] args){
    	innerE i = new innerE(); // 1. 외부 클래스의 객체 생성
        innerE.Cal cal = i.new Cal();
        cal.plus();
        System.out.println(cal.v);
    }
 }

 

- 내부 클래스에서의 객체 사용법

=>  innerE.Cal cal = i.new Cal();

 

 

2) 정적 중첩 클래스

 

public class InnerExam2{
	static class Cal{
    	int v = 0;
        public void plus(){
        	v++
     }
     
     public static void main(String[] args){
     	// static 필드이므로 외부 클래스의 객체를 생성할 필요가 없음.
        InnerExam2.Cal cal = new InnerExam2.Cal();
    }
 }

 

 

 

3) 지역 중첩 클래스

public class InnerExam3{
	public void exec(){
            class Cal{
            int v = 0;
            public void plus(){
                v++
            }
            Cal cal = new Cal();
            cal.plus();
            System.out.println(cal.v);
    }
    
    public static void main(String[] args){
    	InnerExam3 i = new InnerExam3();
        i.exec();
    }
 }

 

- 메서드 안에서 내부 클래스를 만드는 방법

 

 

4. 익명 클래스

//Action.java

public abstract class Action{
	public abstract void exec();
}

 

//MyAction
public class MyAction extends Action{
	
    public void exec() {
    	System.out.println("exec");
    }
}

 

 

//ActionExam.java

public class ActionExam { 
	public static void main(String[] args){
            Action action = new MyAction();
            action.exec();
    }
}

 

 

* 익명 내부 클래스 

public class ActionExam { 
	public static void main(String[] args){
            //Action action = new MyAction();
            //action.exec();
            
            
            // 익명 내부 클래스
            Action action = new Action(){
            	
                public void exec() {
                	System.out.println("exec");
                }
                
            };
            action.exec();
    }
}

 

- 해당 생성자 이름에 해당하는 클래스를 상속받은 이름없는 객체를 만든다. 

- 이렇게 생성된 이름없는 객체를 Action이라는 참조변수가 참조하도록 함.

- 괄호 안에는 메서드를 구현하거나 추가할 수도 있음.

 

 

=> Action 을 상속받는 클래스를 따로 작성할 필요가 없음.

한 번만쓰고 말 것이므로 MyAction 이라는 클래스를 따로 작성할 필요가 없는 것임.

'자바' 카테고리의 다른 글

코자개 - Hash  (1) 2025.04.11
자바 정처기 실기 대비  (1) 2025.04.07
코자개1. [ ==와 equals() ]  (0) 2025.04.03
java 복습 2  (0) 2024.01.14
java 복습 1  (0) 2024.01.04