AnnSEo.log

(컬렉션을 보다가) 왜 인터페이스를 extends를 하지? 본문

자바

(컬렉션을 보다가) 왜 인터페이스를 extends를 하지?

AnnSEo2105 2021. 10. 6. 16:23

긱포긱에서 컬렉션을 보고 있는데

https://www.geeksforgeeks.org/how-to-learn-java-collections-a-complete-guide/

 

How to Learn Java Collections - A Complete Guide - 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

지금까지 대충 인터페이스는 implements 클래스는 extends 이렇게 생각하고 있었는데!

아니 이터러블 인터페이스이랑 컬렉션 사이에 extends 관계라고 써 있네!! 뭐지!

 

 

그렇게 시작된 나의 구글링

몰래 블로그 하다 걸렸지만 계속 할꺼얌~^^!!

 

첫 시작은 구글에서 what is difference between extends and implements in java 로 검색해서 나온 첫 페이지였다.

 

https://stackoverflow.com/questions/10839131/implements-vs-extends-when-to-use-whats-the-difference 

 

Implements vs extends: When to use? What's the difference?

Please explain in an easy to understand language or a link to some article.

stackoverflow.com

 

업보트를 많이 받은 걸 보니, 내가 기존에 이해하던 대로의 내용인거 같았다.

인터페이스는 선언한 부분에서 메서드 구현하는게 아니고 implements 한 곳에서 구현부를 작성하는 거고,

클래스는 선언할 때 구현부 다 작성하고 하위 클래스에서 오버라이드 해도 되는 식. 

클래스 다중상속이 안되고 인터페이스는 여러 개 implements가 된다는 거 정도.

 

 

그래서 다시 구글링 구글링

이번에는 why interface extends interface in java 라고 검색했다.

 

https://stackoverflow.com/questions/13437131/java-why-interface-extends-interface 

 

Java why interface extends interface

I am wondering under what circumstances do we extend an interface from an interface? Because, for example interface A{ public void method1(); } interface B extends A{ public void method2(...

stackoverflow.com

 

아 여기 보니 좀 명확해졌다.

 

일단 인터페이스가 뭘 하는지에 따라 달라지는데

예를 들어, 

자동차라는 인터페이스가 있다고 치고 운전할 수 있는 것이라는 인터페이스가 있다면?

-> 결국 자동차라는 건 운전할 수 있는게 당연하잖수? -> 인터페이스 상속이 없으면 다양한 브랜드의 자동차마다 그걸 다 아래처럼 하나하나 다 implements 해줘야 한다.

class ChevyVolt implements Vehicle, Drivable
class FordEscort implements Vehicle, Drivable
class ToyotaPrius implements Vehicle, Drivable

쉐뭐시기도 임플, 포드도 임플, 토요타도 임플..

 

 

이걸 이렇게 하느니 이거 한 방으로 끝내자는 거다. 어차피 자동차는 운전할 수 있는 거니까 여기서 뭐 하나 빠지는게 없다. 

interface Vehicle extends Drivable

 

 

 

여튼 결론은 모든 컬렉션은 이터러블하다는 것이였고 것이었다!