11 COMPUTER PROGRAMMING INHERIATANCE
CONTENTS OVERVIEW OF INHERITANCE INHERITANCE OF MEMBER VARIABLE RESERVED WORD SUPER METHOD INHERITANCE and OVERRIDING INHERITANCE and CONSTRUCTOR 2
Overview of Inheritance Declaration format of Class including Inheritance [public/final/abstract] class ClassName extends SupperClassName {... // Declaration of member variable... // Constructor... // Declaration of method 3
Inheritance of Member Variable Example of Member Variable Inheritance class A { int aa = 1; class B extends A { int bb = 2; class C extends B { int cc = 3; class Dabc { public static void main(string[] args) { C objc = new C(); System.out.println("objc 객체의객체속성변수 aa 의값은 " + objc.aa); System.out.println("objc 객체의객체속성변수 bb 의값은 " + objc.bb); System.out.println("objc 객체의객체속성변수 cc 의값은 " + objc.cc); 4
Inheritance of Member Variable class C1 { static int x; static int y; class C2 extends C1 { static String x; class Inheritance3 { C2.x = " 알기쉽게해설한자바 "; C2.y = 20000; C1.x = 30000; System.out.println(" 클래스변수 C2.x 값 : " + C2.x); System.out.println(" 클래스변수 C2.y 값 (C1 으로부터상속 ) : " + C2.y); System.out.println(" 클래스변수 C1.x 값 : " + C1.x); 5
Reserved Word - super class D1 { int x = 1000; void display() { System.out.println(" 상위클래스 D1 의 display() 메소드입니다 "); class D2 extends D1 { int x = 2000; void display() { System.out.println(" 하위클래스 D2 의 display() 메소드입니다 "); void write() { this.display(); super.display(); System.out.println("D2 클래스객체의 x 값은 : " + x); System.out.println("D1 클래스객체의 x 값은 : " + super.x); class InheritanceSuper { D2 d = new D2(); d.write(); 6
Method Inheritance and Overriding class A { int i; int j; void setij(int x, int y) { i = x; j = y; class B extends A { int total; void sum() { total = i + j; class Inheritance4 { B subob = new B(); subob.setij(10, 12); subob.sum(); System.out.println(" 두수의합계는 : " + subob.total); 7
Method Inheritance and Overriding class A { void show(string str) { System.out.println(" 상위클래스의메소드 show(string str) 수행 " + str); class B extends A { void show() { System.out.println(" 하위클래스의메소드 show() 수행 "); class OverrideExam1 { B over = new B(); over.show(" 알기쉽게해설한자바 "); over.show(); 8
Method Inheritance and Overriding class A { void show() { System.out.println(" 상위클래스의메소드 show(string str) 수행 "); class B extends A { void show() { System.out.println(" 하위클래스의메소드 show() 수행 "); class OverrideExam2 { B over = new B(); over.show(); 9
Method Inheritance and Overriding class A { int i, j; A(int a, int b) { i = a; j = b; void show() { System.out.println(" 상위클래스의메소드 show() 수행 "); class B extends A { int k; B(int a, int b, int c ) { super(a,b); k = c; void show() { System.out.println(" 하위클래스의메소드 show() 수행 "); System.out.println("===super 를이용한상위클래스메소드호출 ==="); super.show(); class OverrideExam3 { B over1 = new B(10, 20, 30); System.out.println("i, j, k 의값 : " + over1.i + " " + over1.j + " " + over1.k); over1.show(); 10
Inheritance and Constructor class A1 { double d1; A1() { System.out.println(" 클래스 A1 의생성자수행 "); d1 = 10*10; class A2 extends A1 { double d2; A2() { System.out.println(" 클래스 A2 의생성자수행 "); d2 = 10*10*10; class A3 extends A2 { double d3; A3() { System.out.println(" 클래스 A3 의생성자수행 "); d3 = 10*10*10*10; class Constructors1 { A3 super1 = new A3(); System.out.println("10 의 2 제곱 : " + super1.d1); System.out.println("10 의 3 제곱 : " + super1.d2); System.out.println("10 의 4 제곱 : " + super1.d3); 11
Inheritance and Overriding class A1 { int d1; int s; A1(int s1) { System.out.println(" 클래스 A1 의생성자수행 "); s = s1; d1 = s * s ; class A2 extends A1 { int d2; int t; A2(int s1, int t1) { super(s1); System.out.println(" 클래스 A2 의생성자수행 "); t = t1; d2 = t * t ; class Constructors2 { A2 super2 = new A2(10,20); System.out.println("10 의제곱은 : " + super2.d1); System.out.println("20 의제곱은 : " + super2.d2); 12
Type Conversion of Object Object type conversion in classes of inheritance relation class Acast { int a=1; class Bcast extends Acast { int b=2; class Ccast extends Bcast { int c=3; class TestCasting { public static void main(string[] args) { Acast refa; refa = new Ccast(); Acast 타입의객체 refa 선언 System.out.println("refA.a 의값은 "+refa.a); 13
CONCLUDE OVERVIEW OF INHERITANCE INHERITANCE OF MEMBER VARIABLE RESERVED WORD SUPER METHOD INHERITANCE and OVERRIDING INHERITANCE and CONSTRUCTOR 14