1 ObjectOriented Programming Inheritance 2005 Pearson Education Inc
1 Object-Oriented Programming: Inheritance 2005 Pearson Education, Inc. All rights reserved.
2 Introduction • Inheritance – Create new class from existing class – Subclass extends super class. the keyword extends is used to derive a class in java. 2005 Pearson Education, Inc. All rights reserved.
3 Simple and Multilevel Inheritance Simple Inheritance Multilevel Inheritance 2005 Pearson Education, Inc. All rights reserved.
4 Superclasses and subclasses • Example: – superclass: Vehicle • Cars, trucks, boats, bicycles, … – subclass: Car • Smaller, more-specific subset of vehicles 2005 Pearson Education, Inc. All rights reserved.
5 single inheritance or one level inheritance class A { int x; int y; int get(int p, int q){ x=p; y=q; return(0); } void Show(){ System. out. println(x); } } class B extends A{ public static void main(String args[]){ A a = new A(); a. get(5, 6); a. Show(); } void display(){ System. out. println("B"); } } 2005 Pearson Education, Inc. All rights reserved.
6 Multilevel Inheritance class A { int x; int y; int get(int p, int q){ x=p; y=q; return(0); } void Show(){ System. out. println(x); } } class B extends A{ void Showb(){ System. out. println("B"); } } 2005 Pearson Education, Inc. All rights reserved.
7 class C extends B{ void display(){ System. out. println("C"); } public static void main(String args[]){ A a = new A(); a. get(5, 6); a. Show(); } } 2005 Pearson Education, Inc. All rights reserved.
8 The super keyword The super is java keyword is used to access the members of the super class. It is used for two purposes in java. 1. keyword super used to access the hidden data variables of the super class hidden by the sub class. super. member; Here member can either be an instance variable or a method 2005 Pearson Education, Inc. All rights reserved.
class A{ int a; float b; void Show(){ System. out. println("b in super class: " + b); } } 9 2005 Pearson Education, Inc. All rights reserved.
class B extends A{ int a; float b; B( int p, float q){ a = p; super. b = q; } void Show(){ super. Show(); System. out. println("b in super class: " + super. b); System. out. println("a in sub class: " + a); } 10 public static void main(String[] args){ B subobj = new B(1, 5); subobj. Show(); } } 2005 Pearson Education, Inc. All rights reserved.
11 Output: b in super class: 5. 0 a in sub class: 1 2005 Pearson Education, Inc. All rights reserved.
2. The keyword super used to call super class constructor in the subclass. This functionality can be achieved just by using the following command. super(param-list); Here parameter list is the list of the parameter requires by the constructor in the super class. super must be the first statement executed inside a super class constructor. If we want to call the default constructor then we pass the empty parameter list. The following program illustrates the use of the super keyword to call a super class constructor. super() 12 2005 Pearson Education, Inc. All rights reserved.
13 class A{ int a; int b; int c; A(int p, int q, int r) { a=p; b=q; c=r; } } 2005 Pearson Education, Inc. All rights reserved.
class B extends A{ int d; B(int l, int m, int n, int o){ super(l, m, n); d=o; } void Show(){ System. out. println("a = " + a); System. out. println("b = " + b); System. out. println("c = " + c); System. out. println("d = " + d); } 14 public static void main(String args[]){ B b = new B(4, 3, 8, 7); b. Show(); } } 2005 Pearson Education, Inc. All rights reserved.
15 Output: a=4 b=3 c=8 d=7 2005 Pearson Education, Inc. All rights reserved.
- Slides: 15