Top Banner
21

Inheritance in Java

Feb 16, 2016

Download

Documents

Anjali Anju

powerpoint presentation on inheritance in java
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Inheritance in Java
Page 2: Inheritance in Java

INHERITANCE

Page 3: Inheritance in Java

The word inheritance refers to the act

of inheriting material or immaterial possessions by succession .

materialimmaterial

Page 4: Inheritance in Java

Inheritance in java

• Inheritance is a basic concept in Object Oriented Programming .• Inheritance in java is a mechanism in which one class of objects acquires all the properties and behaviors of a parent class.• represents the IS-A / HAS-A relationship .

Page 5: Inheritance in Java

terms

•Superclass/base class/parent class : The class from which another class is derived .

•Subclass/derived class/child class : class that is derived from another class .

Page 6: Inheritance in Java

syntaxInheritance can be implemented

using the keyword extend .

class subclass_name extends superclass_name  {     //methods and fields  }  

Page 7: Inheritance in Java

superclass : employeesubclass : programmerrelation : IS A

programmer IS A employee .

example

Page 8: Inheritance in Java

source codeclass 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(“ponus of Programmer is:"+p.bonus);   }  }  

Page 9: Inheritance in Java

outputprogrammer salary is : 40000.0 bonus of programmer is : 10000

Page 10: Inheritance in Java

types1.Single

inheritance .2.Multiple

inheritance .3.Multilevel

inheritance .4.Hierarchical inheritance

.5.Hybrid inheritance

.

Page 11: Inheritance in Java

Single inheritance

A single class is inherited from a single baseclass

Page 12: Inheritance in Java

example

Page 13: Inheritance in Java

Multiple inheritanceA single class is

inherited from a more than one baseclasses.

Java does not support multiple inheritance .

That is why java is said to be simple .

Page 14: Inheritance in Java

Multilevel

inheritance

A derived class acts as baseclass for another class .

Page 15: Inheritance in Java

example

Page 16: Inheritance in Java

Hierarchical inheritance

More than one class is inherited from a single baseclass

Page 17: Inheritance in Java

example

Page 18: Inheritance in Java
Page 19: Inheritance in Java

hybrid inheritance

a combination of single and multiple

 inheritance

Page 20: Inheritance in Java

advantages•Code reusability – object of a derived class can also access the field of its base class .•Method overriding – we can define a same method in a derived class without altering the function in base class .

Page 21: Inheritance in Java