A Drowsy Day

자바 상속(Inheritance) 본문

Computer/IT/Java

자바 상속(Inheritance)

LuMen4284™ 2015. 10. 10. 12:23

상속이란?

기존의 클래스를 재사용하여 새로운 클래스를 작성하는 것 입니다.

Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of parent object.(조상 클래스로 부터 모든 멤버(속성[변수]+기능[메서드])를 상속 받습니다)

상속을 하는 이유?

코드의 재사용성을 높이고 코드의 중복을 제거하여 프로그램의 생산성과 유지보수가 향상됩니다.

For Method Overriding (so runtime polymorphism(다향성) can be achieved)

For Code Re-usability(재사용성)

상속을 하는 방법?

새로 작성하고자 하는 클래스의 이름 뒤에 상속받고자 하는 클래스의 이름을 키워드 'extends'와 함께 써 주기만 하면 됩니다.

The "extends" keyword indicates that you are making a new class that derives from an existing class.

ex)

      1. class Subclass-name extends Superclass-name  
      2. {  
      3.    //methods and fields  
      4. }  

A class that is inherited is called a super class. The new class is called a subclass

참고사항

※ 생성자와 초기화 블럭은 상속되지 않습니다. (멤버 - 속성 및 기능만 상속!!)

※ 자손 클래스의 멤버 개수는 조상 클래스보다 항상 같거나 많습니다.
   이유 : 자손 클래스는 조상 클래스의 모든 멤버를 상속 받기 때문에 항상 조상  클래스보다 같거나 많은 멤버를 갖게 됩니다. 상속에 상속을 거듭할수록 상속받는 클래스의 멤버 개수는 점점 늘어납니다.


Comments