JAVA

Inheritance

Pothan Prasad Pabbuleti

Topics
Introduction of Inheritance.  Example program. Types of the Inheritance.

Introduction
• Definition • Daily life Example • Features of Inheritance • Example program

Features of Inheritance
• Reusability – Inheritance allows code reusability. • Refinement – Adding the new functionality to the existing component without modifying the component

Example Program
class figure { int l,b,area; void showarea(){ Area = l*b; System.out.println(“The area = “+area); }} class rectangle extends figure{ rectangle(int len, int wid){ l = len; b = wid; } } class square extends figure { Square(int len) l=len; b=len; class inheritance { Public static void main(String args[]) { rectangle rect = new recatangle(3,4); Rect.showarea(); square sqr = new square(4); sqr.showarea(); }} OUTPUT: The area = 12 The area = 16

Types of Inheritance
1. Single Inheritance 3. Multilevel Inheritance 5. Hierarchical Inheritance 7. Multiple Inheritance

Single Inheritance
A Super class

B

Sub class

Multilevel Inheritance
A

B

C

Hierarchical Inheritance
A

B

C

C

Multiple Inheritance
B A

C

Click here for another example program.

Sign up to vote on this title
UsefulNot useful