You are on page 1of 19

UNIT- I

CHAPTER IN BOOK- 8
PART- I
-K. Indhu

SYLLABUS COVERED
HERE

INHERITANCE

K. INDHU

GOALS
1. Inheritance Definition
2. Inheritance Syntax
3. Inheritance Diagrammatic Example
4. Inheritance Sample Program
5. Public, Private in Inheritance
6. Types of Inheritance in Java
7. No Multiple Inheritance in Java- Why ?
8. Using super
9. Note- super in constructor during inheritance
10. THREE USAGES of super
11. Multi-Level Hierarchy
K. INDHU

INHERITANCE
DEFINITION->DEFINITION
Inheritance in java is a mechanism in which one object
acquires all the properties and behaviors of parent object.

Inheritance represents the IS-A relationship, also


known as parent-child relationship.

WHY USE INHERITANCE IN JAVA?


(1) For Method Overriding (runtime polymorphism can be
achieved).
(2) For Code Reusability.

In the terminology of Java

a class that is inherited is called a super class.


The new class is called a subclass.
K. INDHU

INHERITANCE SYNTAX

The general form of a class declaration


that inherits a superclass is shown here:

class Subclass-name extends Superclassname


{
//methods and fields
}

The extends keyword indicates that you are


making a new class that derives from an
existing class.
K. INDHU

DIAGRAMMATIC
EXAMPLE

K. INDHU

SAMPLE PROGRAM

K. INDHU

SAMPLE PROGRAM

K. INDHU

PUBLIC, PRIVATE

K. INDHU

TYPES IN JAVA

K. INDHU

NO MULTIPLE
INHERITANCE IN JAVA
THRU CLASS- WHY ?

K. INDHU

USING SUPER
I. Whenever a subclass needs to refer to its immediate
super-class, it can do so by use of the keyword "super".
II. Whenever the instance of subclass is created, an
instance of parent class is created implicitly i.e. referred
by "super" reference variable.
III.USAGE OF JAVA "super" KEYWORD->
. (i) super is used to refer immediate parent class
instance variable.
. (ii) super() is used to invoke immediate parent
class constructor.
. (iii) super is used to invoke immediate parent
class method.
K. INDHU

USING SUPER

K. INDHU

USING SUPER

K. INDHU

SUPER IN
CONSTRUCTOR

K. INDHU

USING SUPER

K. INDHU

MULTI-LEVEL
HIERARCHY

E:\JavaPrograms\
Exercises\Exercise2\Pe

K. INDHU

SO FAR WE STUDIED
Inheritance

K. INDHU

HAPPY
LEARNING!!!

You might also like