SlideShare a Scribd company logo
INHERITANCE
Introduction
 Inheritance in java is a mechanism in which one
object acquires all the properties and behaviors of
parent object.
 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.
 Inheritance represents the IS-A relationship, also
known as parent-child relationship.
Why use Inheritance in java
 For Method Overriding (so runtime
polymorphism can be achieved): If subclass (child
class) has the same method as declared in the
parent class, it is known as method overriding
in java.
 For Code Reusability: Use the existing class to
build the new class
Syntax of Java Inheritance
 The extends keyword indicates that you are
making a new class that derives from an existing
class. The meaning of "extends" is to increase the
functionality.
 In the terminology of Java, a class which is
inherited is called parent or super class and the
new class is called child or subclass.
class Subclass-name extends Superclass-name
{
//methods and fields
}
Java Inheritance Example
 In the figure, Programmer
is the subclass and
Employee is the
superclass. Relationship
between two classes is
Programmer IS-A
Employee.
 It means that Programmer
is a type of Employee.
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);
}
}
Types of inheritance
 On the basis of class, there can be three types of
inheritance in java: single, multilevel and
hierarchical.
Single Inheritance Example
class Principal
{
void Calling()
{
System.out.println(“Calling...");
}
}
Class Teacher extends Principal
{
void Teaching()
{
System.out.println(“Teaching...");
}
}
class TestInheritance
{
public static void main(String args[])
{
Teacher t=new Teacher();
t.Teaching();
t.Calling();
}
}
Multilevel Inheritance Example
class Principal
{
Void Calling()
{System.out.println(“Calling...");}
}
class Teacher extends Principal
{
void Teaching(){System.out.println(“Teaching...");}
}
class Student extends Teacher
{
void Studying(){System.out.println(“Studying..");}
}
class TestInheritance2{
public static void main(String args[]){
Student s=new Student();
s. Studying();
s. Teaching();
s. Calling();
Example
 class Principal
 {
 void Calling(){System.out.println(“calling..");}
 }
 class Teacher1 extends Principal
 {
 void Teaching(){System.out.println(“teaching….");}
 }
 class Teacher2 extends Principal
 {
 void Counselling(){System.out.println(“counselling..");}
 }
 class TestInheritance3
 {
 public static void main(String args[]){
 Teacher2 t=new Teacher2();
 t.Counselling();
 t.Calling();
 //t.Teaching();//C.T.Error
 }}
Why multiple inheritance is not supported in java?
 To reduce the complexity and simplify the
language, multiple inheritance is not supported in
java.
 Consider a scenario where A, B and C are three
classes. The C class inherits A and B classes. If A
and B classes have same method and you call it
from child class object, there will be ambiguity to
call method of A or B class.
 class A{
 void msg(){System.out.println("Hello");}
 }
 class B{
 void msg(){System.out.println("Welcome");}
 }
 class C extends A,B{//suppose if it were

 Public Static void main(String args[]){
 C obj=new C();
 obj.msg();//Now which msg() method would be invoked?
 }
 }
Lets Do Something
1. Create a class named Year that contains a data
field that holds the number of days in a year.
Include a get method that displays the number of
days and a constructor that sets the number of
days to 365. Create a subclass named LeapYear.
LeapYear’s constructor overrides Year’s
constructor and sets the number of days to 366.
Write an application named UseYear that
instantiates one object of each class and displays
their data.
2. (The Person, Student, Employee, Faculty, and
Staff classes) Design a class named Person and its
two subclasses named Student and Employee.
Make Faculty and Staff subclasses of Employee. A
person has a name, address, phone number, and
email address. A student has a class status
(freshman, sophomore, junior, or senior). Define the
status as a constant. An employee has an office,
salary, and date hired. A faculty member has office
hours and a rank. A staff member has a title. Override
the toString method in each class to display the
class name and the person’s name. Write a test
program that creates a Person, Student, Employee,
Faculty, and Staff, and invokes their toString()
methods.

More Related Content

PPTX
Inheritance in Java is a mechanism in which one object acquires all the prope...
Kavitha S
 
PPTX
Inheritance in Java,Type of Inheritance,Method overriding
marysj3
 
PPTX
Inheritance in java
chauhankapil
 
PPTX
Inheritance in Java beginner to advance with examples.pptx
naeemcse
 
PPTX
Inheritance and Interfaces
NAGASURESH MANOHARAN
 
PPTX
Inheritance & interface ppt Inheritance
narikamalliy
 
PPTX
Inheritance in OOPs with java
AAKANKSHA JAIN
 
PPTX
UNIT 5.pptx
CurativeServiceDivis
 
Inheritance in Java is a mechanism in which one object acquires all the prope...
Kavitha S
 
Inheritance in Java,Type of Inheritance,Method overriding
marysj3
 
Inheritance in java
chauhankapil
 
Inheritance in Java beginner to advance with examples.pptx
naeemcse
 
Inheritance and Interfaces
NAGASURESH MANOHARAN
 
Inheritance & interface ppt Inheritance
narikamalliy
 
Inheritance in OOPs with java
AAKANKSHA JAIN
 

Similar to A457405934_21789_26_2018_Inheritance.ppt (20)

PPTX
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
RudranilDas11
 
PPTX
Inheritance in oop
MuskanNazeer
 
PPT
web program-Inheritance,pack&except in Java.ppt
mcjaya2024
 
PPTX
Inheritance Slides
Ahsan Raja
 
PPTX
Chapter5.pptxfghwryhYETHYETH67IOIKUTJJUILOUI
berihun18
 
PPTX
INHERITANCE.pptx
HARIPRIYA M P
 
PPT
Ap Power Point Chpt7
dplunkett
 
PPTX
Inheritance in java.pptx_20241025_101324_0000.pptx.pptx
saurabhthege
 
PPTX
Inheritance in java computer programming app
figeman282
 
PDF
‏‏‏‏‏‏oop lecture objectives will come.pdf
nabeehmohammedtaher
 
PPT
Inheritance in java.ppt FOR TYPES OF INHERITANCE
DrNeetuSharma5
 
PDF
java_vyshali.pdf
Vyshali6
 
PDF
Inheritance_abstractclass_interface.pdf
kshitijsaini9
 
PPT
Java_notes.ppt
tuyambazejeanclaude
 
PPTX
Lecture 6 inheritance
manish kumar
 
PPT
Chapter 5 (OOP Principles).ppt
henokmetaferia1
 
PPTX
inheritance.pptx
sonukumarjha12
 
PPTX
OBJECT ORIENTED PROGRAMMING STRUCU2.pptx
JeevaR43
 
PPTX
Inheritance In Java
Manish Sahu
 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
RudranilDas11
 
Inheritance in oop
MuskanNazeer
 
web program-Inheritance,pack&except in Java.ppt
mcjaya2024
 
Inheritance Slides
Ahsan Raja
 
Chapter5.pptxfghwryhYETHYETH67IOIKUTJJUILOUI
berihun18
 
INHERITANCE.pptx
HARIPRIYA M P
 
Ap Power Point Chpt7
dplunkett
 
Inheritance in java.pptx_20241025_101324_0000.pptx.pptx
saurabhthege
 
Inheritance in java computer programming app
figeman282
 
‏‏‏‏‏‏oop lecture objectives will come.pdf
nabeehmohammedtaher
 
Inheritance in java.ppt FOR TYPES OF INHERITANCE
DrNeetuSharma5
 
java_vyshali.pdf
Vyshali6
 
Inheritance_abstractclass_interface.pdf
kshitijsaini9
 
Java_notes.ppt
tuyambazejeanclaude
 
Lecture 6 inheritance
manish kumar
 
Chapter 5 (OOP Principles).ppt
henokmetaferia1
 
inheritance.pptx
sonukumarjha12
 
OBJECT ORIENTED PROGRAMMING STRUCU2.pptx
JeevaR43
 
Inheritance In Java
Manish Sahu
 
Ad

More from RithwikRanjan (13)

PPT
A1771937735_21789_14_2018__16_ Nested Classes.ppt
RithwikRanjan
 
PPTX
INTERNSHIP PRESENTATION.pptx
RithwikRanjan
 
PPTX
internship.pptx
RithwikRanjan
 
PPTX
AM.pptx
RithwikRanjan
 
PPT
4_A1208223655_21789_2_2018_04. Operators.ppt
RithwikRanjan
 
PPTX
sam_technical_seminar.pptx
RithwikRanjan
 
PPTX
sam_report.pptx
RithwikRanjan
 
PPTX
TQM-Module 5.pptx
RithwikRanjan
 
PPTX
CIM MODULE 2.pptx
RithwikRanjan
 
PPT
A2003822018_21789_17_2018_09. ArrayList.ppt
RithwikRanjan
 
PPT
0_A1590026209_21789_20_2018_0 Lecture.ppt
RithwikRanjan
 
PPT
6_A1944859510_21789_2_2018_06. Branching Statements.ppt
RithwikRanjan
 
PPT
A1869984431_21789_28_2018_Abstract Class.ppt
RithwikRanjan
 
A1771937735_21789_14_2018__16_ Nested Classes.ppt
RithwikRanjan
 
INTERNSHIP PRESENTATION.pptx
RithwikRanjan
 
internship.pptx
RithwikRanjan
 
AM.pptx
RithwikRanjan
 
4_A1208223655_21789_2_2018_04. Operators.ppt
RithwikRanjan
 
sam_technical_seminar.pptx
RithwikRanjan
 
sam_report.pptx
RithwikRanjan
 
TQM-Module 5.pptx
RithwikRanjan
 
CIM MODULE 2.pptx
RithwikRanjan
 
A2003822018_21789_17_2018_09. ArrayList.ppt
RithwikRanjan
 
0_A1590026209_21789_20_2018_0 Lecture.ppt
RithwikRanjan
 
6_A1944859510_21789_2_2018_06. Branching Statements.ppt
RithwikRanjan
 
A1869984431_21789_28_2018_Abstract Class.ppt
RithwikRanjan
 
Ad

Recently uploaded (20)

PPTX
RTM_Module1_Summary_tyiuwyPresentation.pptx
DeepakKumar311204
 
PPTX
My analysis on GM_History_Presentation.pptx
savithrir7
 
PDF
deloitte-nl-integrated-annual-report-2018-2019.pdf
dsoham206
 
PPTX
LIGHT-HUMAN EYE AND THE COLOURFUL WORLD.ppt.pptx
atharvawafgaonkar
 
PDF
NCHRP Report 672 Roundabouts: An Informational Guide
Forklift Trucks in Minnesota
 
DOCX
Prelim exam in Elementary Stat. (1).docx
CharmicahOmayan3
 
PPTX
Presentation Homologation Kendaraan Roda 3
delapanpaduprima
 
PPTX
Soffit_Panel_India_Presentation.pptx____
interviewquestion6
 
PDF
PC228USLC-3E0 Komatsu Hydraulic Excavator Parts Manual SN 40001-UP
Heavy Equipment Manual
 
PDF
SAA4D95LE-7A KOMATSU ENGINE PARTS MANUAL SN 800001-UP (For PC138US-11PC138USL...
Heavy Equipment Manual
 
PPT
Operational Risk and its importance an d
icuphamid
 
PDF
PC110R-1 Komatsu Hydraulic Excavator Parts Manual SN 2265010170-Up
Heavy Equipment Manual
 
PPTX
Database management system is manager data
thakormitul730
 
PDF
PC170LC-11 Komatsu Hydraulic Excvator Parts Manual SN 35001-UP (For KAL)
Heavy Equipment Manual
 
PPTX
INTRODUCTION TO HUMAN RESOURCE MANAGEMEN
FahadBinImtiaz
 
PPTX
STRATEGIC HRM.pptxkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
khushigulati2325
 
PPTX
MEASURING-TOOLS FOR GRADE 8 AUTOMOTIVE SERVICING
RosalieUsmanLegaspi
 
PPTX
PROPOSAL RESEARCH METHODOLOGY-1lssskkskes
IsaacAntwi15
 
PPTX
1.02 Ramesh Pinjani - Important Track Design Parameters.pptx
abheeplay
 
PPTX
Selection and Retentionhsiwisjjeuwjfdwws
FahadBinImtiaz
 
RTM_Module1_Summary_tyiuwyPresentation.pptx
DeepakKumar311204
 
My analysis on GM_History_Presentation.pptx
savithrir7
 
deloitte-nl-integrated-annual-report-2018-2019.pdf
dsoham206
 
LIGHT-HUMAN EYE AND THE COLOURFUL WORLD.ppt.pptx
atharvawafgaonkar
 
NCHRP Report 672 Roundabouts: An Informational Guide
Forklift Trucks in Minnesota
 
Prelim exam in Elementary Stat. (1).docx
CharmicahOmayan3
 
Presentation Homologation Kendaraan Roda 3
delapanpaduprima
 
Soffit_Panel_India_Presentation.pptx____
interviewquestion6
 
PC228USLC-3E0 Komatsu Hydraulic Excavator Parts Manual SN 40001-UP
Heavy Equipment Manual
 
SAA4D95LE-7A KOMATSU ENGINE PARTS MANUAL SN 800001-UP (For PC138US-11PC138USL...
Heavy Equipment Manual
 
Operational Risk and its importance an d
icuphamid
 
PC110R-1 Komatsu Hydraulic Excavator Parts Manual SN 2265010170-Up
Heavy Equipment Manual
 
Database management system is manager data
thakormitul730
 
PC170LC-11 Komatsu Hydraulic Excvator Parts Manual SN 35001-UP (For KAL)
Heavy Equipment Manual
 
INTRODUCTION TO HUMAN RESOURCE MANAGEMEN
FahadBinImtiaz
 
STRATEGIC HRM.pptxkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
khushigulati2325
 
MEASURING-TOOLS FOR GRADE 8 AUTOMOTIVE SERVICING
RosalieUsmanLegaspi
 
PROPOSAL RESEARCH METHODOLOGY-1lssskkskes
IsaacAntwi15
 
1.02 Ramesh Pinjani - Important Track Design Parameters.pptx
abheeplay
 
Selection and Retentionhsiwisjjeuwjfdwws
FahadBinImtiaz
 

A457405934_21789_26_2018_Inheritance.ppt

  • 2. Introduction  Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.  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.  Inheritance represents the IS-A relationship, also known as parent-child relationship.
  • 3. Why use Inheritance in java  For Method Overriding (so runtime polymorphism can be achieved): If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in java.  For Code Reusability: Use the existing class to build the new class
  • 4. Syntax of Java Inheritance  The extends keyword indicates that you are making a new class that derives from an existing class. The meaning of "extends" is to increase the functionality.  In the terminology of Java, a class which is inherited is called parent or super class and the new class is called child or subclass. class Subclass-name extends Superclass-name { //methods and fields }
  • 5. Java Inheritance Example  In the figure, Programmer is the subclass and Employee is the superclass. Relationship between two classes is Programmer IS-A Employee.  It means that Programmer is a type of Employee.
  • 6. 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); } }
  • 7. Types of inheritance  On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical.
  • 8. Single Inheritance Example class Principal { void Calling() { System.out.println(“Calling..."); } } Class Teacher extends Principal { void Teaching() { System.out.println(“Teaching..."); } } class TestInheritance { public static void main(String args[]) { Teacher t=new Teacher(); t.Teaching(); t.Calling(); } }
  • 9. Multilevel Inheritance Example class Principal { Void Calling() {System.out.println(“Calling...");} } class Teacher extends Principal { void Teaching(){System.out.println(“Teaching...");} } class Student extends Teacher { void Studying(){System.out.println(“Studying..");} } class TestInheritance2{ public static void main(String args[]){ Student s=new Student(); s. Studying(); s. Teaching(); s. Calling();
  • 10. Example  class Principal  {  void Calling(){System.out.println(“calling..");}  }  class Teacher1 extends Principal  {  void Teaching(){System.out.println(“teaching….");}  }  class Teacher2 extends Principal  {  void Counselling(){System.out.println(“counselling..");}  }  class TestInheritance3  {  public static void main(String args[]){  Teacher2 t=new Teacher2();  t.Counselling();  t.Calling();  //t.Teaching();//C.T.Error  }}
  • 11. Why multiple inheritance is not supported in java?  To reduce the complexity and simplify the language, multiple inheritance is not supported in java.  Consider a scenario where A, B and C are three classes. The C class inherits A and B classes. If A and B classes have same method and you call it from child class object, there will be ambiguity to call method of A or B class.
  • 12.  class A{  void msg(){System.out.println("Hello");}  }  class B{  void msg(){System.out.println("Welcome");}  }  class C extends A,B{//suppose if it were   Public Static void main(String args[]){  C obj=new C();  obj.msg();//Now which msg() method would be invoked?  }  }
  • 13. Lets Do Something 1. Create a class named Year that contains a data field that holds the number of days in a year. Include a get method that displays the number of days and a constructor that sets the number of days to 365. Create a subclass named LeapYear. LeapYear’s constructor overrides Year’s constructor and sets the number of days to 366. Write an application named UseYear that instantiates one object of each class and displays their data.
  • 14. 2. (The Person, Student, Employee, Faculty, and Staff classes) Design a class named Person and its two subclasses named Student and Employee. Make Faculty and Staff subclasses of Employee. A person has a name, address, phone number, and email address. A student has a class status (freshman, sophomore, junior, or senior). Define the status as a constant. An employee has an office, salary, and date hired. A faculty member has office hours and a rank. A staff member has a title. Override the toString method in each class to display the class name and the person’s name. Write a test program that creates a Person, Student, Employee, Faculty, and Staff, and invokes their toString() methods.