You are on page 1of 13

Department of CSE

ADVANCED OOP
21CS2116RA
Topic:

INHERITANCE

Session - 1

CREATED BY K. VICTOR BABU


AIM OF THE SESSION

To familiarize students with the basic concept of


Object oriented programming with Java Language

INSTRUCTIONAL OBJECTIVES

This Session is designed to:


1. Demonstrate Inheritance

LEARNING OUTCOMES

At the end of this session, you should be able to:


Summarize all concepts of inheritance

CREATED BY K. VICTOR BABU


SESSION INTRODUCTION

Inheritance can be defined as the


process where one object acquires
the properties of another. With the
use of inheritance, the information
is made manageable in a
hierarchical order.
CREATED BY K. VICTOR BABU
SESSION DESCRIPTION 

The idea behind inheritance in java is


that you can create new classes that are
built upon existing classes. When you
inherit from an existing class, you can
reuse methods and fields of parent
class, and you can add new methods
and fields also.
CREATED BY K. VICTOR BABU
SESSION DESCRIPTION (Cont..)

IS-A is a way of saying : This object is a


type of that object. Let us see how the
extends keyword is used to achieve
inheritance.
Inheritance represents the IS-A
relationship, also known as parent-
child relationship.
CREATED BY K. VICTOR BABU
SESSION DESCRIPTION (Cont..)

Why use inheritance in java


 For Method Overriding (so runtime
polymorphism can be achieved).

 For Code Reusability.

CREATED BY K. VICTOR BABU


ACTIVITIES/ CASE STUDIES/ IMPORTANT FACTS RELATED TO THE
SESSION

class Employee{  
float salary=40000;  
}  
class Programmer extends Employee{  
 int bonus=10000;  
 public static void main(String args[]){  
   Programmer p=new Programmer();  
   System.out.println("Programmer salary is:"+p.salary);  
   System.out.println("Bonus of Programmer is:"+p.bonus);  
}  
}  
Output:
Programmer salary is:40000.0Bonus of programmer is:10000

CREATED BY K. VICTOR BABU


EXAMPLES

public class Animal{


}
public class Mammal extends Animal{
}
public class Reptile extends Animal{
}
public class Dog extends Mammal{
}

CREATED BY K. VICTOR BABU


SUMMARY

Animal is the superclass of Mammal class.


Animal is the superclass of Reptile class.
Mammal and Reptile are subclasses of
Animal class.
Dog is the subclass of both Mammal and
Animal classes.

CREATED BY K. VICTOR BABU


SELF-ASSESSMENT QUESTIONS

1. What is subclass in java?

(a) A subclass is a class that extends another class


(b) A subclass is a class declared inside a class
( c ) Both above.
(d) None of the above

2. Which class cannot be sub classed?

(a) final class


(b) object class
(c) abstract class
(d) child class

CREATED BY K. VICTOR BABU


TERMINAL QUESTIONS

•Develop Animal, Monkey and Human classes in the hierarchy with three
level multilevel inheritance. Write atleast one method in each class and
Methods of the Monkey class must be overridden in Human class.
Declare objects fro all three classes, all respective methods, write outputs?

•Write a java program Employee as super class with salary as instance


variable and a derived class Programmer with bonus as instance variable.
Create an object for Programmer and print salary and bonus,
give all necessary data.

CREATED BY K. VICTOR BABU


REFERENCES FOR FURTHER LEARNING OF THE SESSION

Reference Books:
1. Java for beginners
2. Complete reference Java
3. Mastering java

Sites and Web links:


1. www.javatpoint.com
2. www.oracle.java
3. www. Java.com

CREATED BY K. VICTOR BABU


THANK YOU

Team – Course Name

CREATED BY K. VICTOR BABU

You might also like