logo

Java의 상속 유형

계승 객체 지향 프로그래밍의 가장 강력한 기능입니다. 이를 통해 한 클래스의 속성을 다른 클래스에 상속할 수 있습니다. 이 섹션에서는 다음 내용을 논의하겠습니다. Java의 상속 유형 실제 사례를 통해 심층적으로 설명합니다. 또한 다양한 유형의 상속 개념을 구현하기 위해 Java 프로그램을 만들 것입니다.

계승

계승 기존 클래스에서 새로운 클래스를 구동하는 메커니즘입니다. 기존 (이전) 클래스는 다음과 같이 알려져 있습니다. 기본 클래스 또는 슈퍼 클래스 또는 부모 클래스 . 새로운 클래스는 다음과 같이 알려져 있습니다. 파생 클래스 또는 하위 클래스 또는 어린이 수업 . 이를 통해 한 클래스(부모)의 속성과 동작을 다른 클래스(자식)에서 사용할 수 있습니다.

자바를 잡아보세요

속성이 상속되는 클래스를 클래스라고 합니다. 부모 클래스 부모 클래스의 속성을 상속받는 클래스는 다음과 같습니다. 어린이 수업 . 따라서 부모-자식 또는 부모-자식 클래스로 알려진 부모-자식 클래스 간의 관계를 설정합니다. 이자 관계.

이름이 두 개의 클래스가 있다고 가정합니다. 아버지 그리고 어린이 그리고 우리는 Child 클래스에서 Father 클래스의 속성을 상속받기를 원합니다. 우리는 다음을 사용하여 이를 달성할 수 있습니다. 연장하다 예어.

 //inherits the properties of the Father class class Child extends Father { //functionality } 

Java의 상속 유형

언제 상속을 사용해야 할까요?

상속은 다음을 제공합니다. 재사용 성 특히 재사용할 코드의 양이 많은 경우에는 더욱 그렇습니다. 또한 이는 서로 다른 클래스 간의 관계를 설정합니다. 이자 관계. 우리가 달성하고 싶다면 그것을 사용할 수도 있습니다 메서드 재정의 .

기억해야 할 점

  • Java에서는 생성자를 상속할 수 없습니다.
  • Private 멤버는 Java에서 상속되지 않습니다.
  • Java에서는 순환 상속이 허용되지 않습니다.
  • 하위 개체에 상위 참조를 할당합니다.
  • 생성자는 생성자에 존재하는 super()로 인해 실행됩니다.

상속 유형

Java는 다음 네 가지 유형의 상속을 지원합니다.

  • 단일 상속
  • 다단계 상속
  • 계층적 상속
  • 하이브리드 상속
Java의 상속 유형

참고: Java에서는 다중 상속이 지원되지 않습니다.

적절한 예를 들어 각각에 대해 논의해 보겠습니다.

단일 상속

단일 상속에서는 하위 클래스가 하나의 상위 클래스에서만 파생됩니다. 단일 부모 클래스의 속성과 동작을 상속합니다. 때로는 다음과 같이 알려져 있습니다. 단순 상속 .

타이프스크립트 날짜
Java의 상속 유형

위 그림에서 Employee는 상위 클래스이고 Executive는 하위 클래스입니다. Executive 클래스는 Employee 클래스의 모든 속성을 상속합니다.

Java 프로그램에서 단일 상속 메커니즘을 구현해 보겠습니다.

Executive.java

 class Employee { float salary=34534*12; } public class Executive extends Employee { float bonus=3000*6; public static void main(String args[]) { Executive obj=new Executive(); System.out.println('Total salary credited: '+obj.salary); System.out.println('Bonus of six months: '+obj.bonus); } } 

산출:

 Total salary credited: 414408.0 Bonus of six months: 18000.0 

다단계 상속

~ 안에 다단계 상속 , 클래스가 다른 클래스에서 파생된 클래스에서 파생되는 것을 다중 수준 상속이라고 합니다. 간단히 말해서, 둘 이상의 부모 클래스를 갖는 클래스를 다중 수준 상속이라고 말할 수 있습니다. 클래스는 서로 다른 레벨에 있어야 합니다. 따라서 단일 기본 클래스와 단일 파생 클래스가 존재하지만 여러 개의 중간 기본 클래스가 있습니다.

Java의 상속 유형

위 그림에서 Marks 클래스는 Students 클래스의 멤버 또는 메서드를 상속합니다. Sports 클래스는 Marks 클래스의 멤버를 상속합니다. 따라서 Student 클래스는 Marks 클래스의 상위 클래스이고 Marks 클래스는 Sports 클래스의 상위 클래스입니다. 따라서 Sports 클래스는 Marks 클래스와 함께 Student 속성을 암시적으로 상속합니다.

Java 프로그램에서 다중 레벨 상속 메커니즘을 구현해 보겠습니다.

MultilevelInheritanceExample.java

 //super class class Student { int reg_no; void getNo(int no) { reg_no=no; } void putNo() { System.out.println('registration number= '+reg_no); } } //intermediate sub class class Marks extends Student { float marks; void getMarks(float m) { marks=m; } void putMarks() { System.out.println('marks= '+marks); } } //derived class class Sports extends Marks { float score; void getScore(float scr) { score=scr; } void putScore() { System.out.println('score= '+score); } } public class MultilevelInheritanceExample { public static void main(String args[]) { Sports ob=new Sports(); ob.getNo(0987); ob.putNo(); ob.getMarks(78); ob.putMarks(); ob.getScore(68.7); ob.putScore(); } } 

산출:

 registration number= 0987 marks= 78.0 score= 68.7 

계층적 상속

여러 클래스가 단일 기본 클래스에서 파생된 경우 이를 호출합니다. 계층적 상속 .

Java의 상속 유형

위 그림에서 Science, Commerce 및 Arts 클래스는 Student라는 단일 상위 클래스를 상속합니다.

Java 프로그램에서 계층적 상속 메커니즘을 구현해 보겠습니다.

바이너리를 bcd로

HierarchicalInheritanceExample.java

 //parent class class Student { public void methodStudent() { System.out.println('The method of the class Student invoked.'); } } class Science extends Student { public void methodScience() { System.out.println('The method of the class Science invoked.'); } } class Commerce extends Student { public void methodCommerce() { System.out.println('The method of the class Commerce invoked.'); } } class Arts extends Student { public void methodArts() { System.out.println('The method of the class Arts invoked.'); } } public class HierarchicalInheritanceExample { public static void main(String args[]) { Science sci = new Science(); Commerce comm = new Commerce(); Arts art = new Arts(); //all the sub classes can access the method of super class sci.methodStudent(); comm.methodStudent(); art.methodStudent(); } } 

산출:

 The method of the class Student invoked. The method of the class Student invoked. The method of the class Student invoked. 

하이브리드 상속

하이브리드 수단은 둘 이상으로 구성됩니다. 하이브리드 상속은 두 가지 이상의 상속 유형을 결합한 것입니다.

Java의 상속 유형

위 그림에서 GrandFather는 슈퍼 클래스입니다. Father 클래스는 GrandFather 클래스의 속성을 상속합니다. 아버지와 할아버지는 단일 상속을 나타내기 때문입니다. 또한, Father 클래스는 Son 및 Daughter 클래스에 상속됩니다. 따라서 아버지는 아들과 딸의 부모 클래스가 됩니다. 이러한 클래스는 계층적 상속을 나타냅니다. 결합하여 하이브리드 상속을 나타냅니다.

자바 목록이 비어 있음

Java 프로그램에서 하이브리드 상속 메커니즘을 구현해 보겠습니다.

딸.자바

 //parent class class GrandFather { public void show() { System.out.println('I am grandfather.'); } } //inherits GrandFather properties class Father extends GrandFather { public void show() { System.out.println('I am father.'); } } //inherits Father properties class Son extends Father { public void show() { System.out.println('I am son.'); } } //inherits Father properties public class Daughter extends Father { public void show() { System.out.println('I am a daughter.'); } public static void main(String args[]) { Daughter obj = new Daughter(); obj.show(); } } 

산출:

 I am daughter. 

다중 상속(지원되지 않음)

Java는 모호성으로 인해 다중 상속을 지원하지 않습니다. 예를 들어, 다음 Java 프로그램을 고려하십시오.

Demo.java

 class Wishes { void message() { System.out.println('Best of Luck!!'); } } class Birthday { void message() { System.out.println('Happy Birthday!!'); } } public class Demo extends Wishes, Birthday //considering a scenario { public static void main(String args[]) { Demo obj=new Demo(); //can't decide which classes' message() method will be invoked obj.message(); } } 

위 코드는 호출할 message() 메서드를 컴파일러가 결정할 수 없기 때문에 오류를 발생시킵니다. 이러한 이유로 Java는 클래스 수준에서 다중 상속을 지원하지 않지만 상호 작용 .