Java/Java 기타

자바에서 @Override 사용하는 한 가지 방법

kimc 2022. 5. 21. 00:19

```

자바에서 @Override 사용하는 한 가지 방법

```

이번 글을 통해 배워갈 내용

  1. Method Overriding 정의
  2. @Overriding Annotation 예시

 


 

Annotation이란

https://codemasterkimc.tistory.com/397

 

자바 Annotation의 정의와 종류 그리고 사용 예시

``` Annotation의 정의 Annotation의 종류 Annotation의 사용 예시 ``` 이번 글을 통해 배워갈 내용  Annotation의 정의  Annotation의 종류  Annotation의 사용 예시 Annotation의 정의 Annotation은 메타데..

codemasterkimc.tistory.com

 

 

Method Overriding 정의

자식 클래스 (Child Class, Sub Class)가 부모 클래스와 동일한 메서드를 선언하여 가지고 있다면
이를 메서드 오버 라이딩 (Method Overriding)이라고 합니다

 

Method Overriding 사용용도
1. Runtime Polymerphism을 사용
2. Superclass에서 이미 제공된 메서드를 재정의 하여 특정한 용도로 사용할 수 있습니다.

Method Overriding 사용조건
Method는 부모 Method와 동일한 이름, 동일한 인자 조건을 가지고 IS-A 관계를 만족해야 합니다.

 

@Overriding Annotation
@Overriding Annotation을 사용해서 컴파일러에게 Superclass의 method를 Override 한다고 알림으로써 컴파일 시 컴파일러가 만약 해당되는 메서드를 못 찾더라도 Override 되었다는 것을 인지함으로써 프로그램을 더 안전하게 해 줍니다.

 

@Overriding Annotation 사용 예시

 

 

Animal interface 생성

public interface Animal {
	// hello method를 가지고 있음
    public void hello();
}

 

Animal interface를 구현한 Cat 클래스

public class Cat implements Animal{
	// hello method를 상속하였음
    @Override
    public void hello() {
        System.out.print("hey");
    }
}

 

Cat 객체를 만들고 실행 
hey 출력

public class App {
    public static void main(String[] args) {
        Animal myCat = new Cat();
        myCat.hello();
    }
}

 

참조

https://docs.oracle.com/javase/tutorial/java/IandI/override.html

 

Overriding and Hiding Methods (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritan

The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available. See Java Language Changes for a summary of updated

docs.oracle.com

https://www.geeksforgeeks.org/overriding-in-java/

 

Overriding in Java - GeeksforGeeks

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

www.geeksforgeeks.org

 

 

읽어주셔서 감사합니다

 

무엇인가 얻어가셨기를 바라며

 

오늘도 즐거운 코딩 하시길 바랍니다 ~ :)

 


 

728x90