본문 바로가기
IT/Java

implements, extends 실 사용 예제

by 성준하이 2023. 9. 27.
반응형

예전 포스팅에서 자바 상속 관련해서 implements 와 extends 에 대해서 다룬 글이 있다.

자세한 내용은 아래 참고 포스팅을 참고 바란다.

 

implements 의 예제코드와 extends의 예제 코드를 정리해두려고 한다.

 

implements

구현부의 interface 를 만들고 class에서 구현해둔 interface를 상속받아 설계를 할수 있다.

예제 코드는 아래와 같다.

//Parent Interface
public interface ParentInterface {
     public void testParentInterface();
}
//Child Class
public class ChildClass implements ParentInterface{
     @Override
     public void testParentInterface() {
         // TODO Auto-generated method stub
     }
}

위에 Parent로 interface를 만들고 안에 testParentInterface 를 만들었다.

그리고 Child Class 에서는 interface를 implements로 받는다.

그럼 자동으로 class에서 에러가 뜨는데 마우스오버 하여 자동으로 코드를 추가해주면 기본적으로 위와 같은 코드가 생성이 된다.

 

extends

만들어둔 class를 자식 class에서 그대로 가져다 쓰기 위해 사용한다.

예제 코드는 아래와 같다.

//Parent Class
public class ParentClass {
     public void testParentMethod() {
          System.out.println("TestMethod");
     }
}
//Child Class
public class ChildClass extends ParentClass{
     public void testChildMethod() {
          System.out.println("child Test");
          super.testParentMethod();
     }
}

참고 포스팅

https://thenicesj.tistory.com/133

 

implements, extends 란?

java를 사용해본 분들이라면 상속의 개념에 대해서는 들어봤을것이다. 그리고 사용해보지 않으신분들도 상속이라는게 뭔지는 알것이다. 프로그래밍언어에서 말하는 상속이란, 부모 클래스와 자

thenicesj.tistory.com

 

반응형

'IT > Java' 카테고리의 다른 글

추상클래스와 인터페이스 차이점 (abstract VS interface)  (34) 2023.09.29
JUNIT 테스트 메서드 순서 정하기  (44) 2023.09.28
Inner Class(이너클래스, 내부클래스)  (33) 2023.09.24
@Resource 관련  (58) 2023.09.22
[Java] Iterator  (62) 2023.09.21

댓글