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

Similar to A457405934_21789_26_2018_Inheritance.ppt

Inheritance in java
Inheritance in java Inheritance in java
Inheritance in java
yash jain
 
Chap-3 Inheritance.pptx
Chap-3 Inheritance.pptxChap-3 Inheritance.pptx
Chap-3 Inheritance.pptx
chetanpatilcp783
 
11slide.ppt
11slide.ppt11slide.ppt
11slide.ppt
MohammedNouh7
 
‫Chapter3 inheritance
‫Chapter3 inheritance‫Chapter3 inheritance
‫Chapter3 inheritance
Mahmoud Alfarra
 
OOPs Concepts - Android Programming
OOPs Concepts - Android ProgrammingOOPs Concepts - Android Programming
OOPs Concepts - Android Programming
Purvik Rana
 
11slide
11slide11slide
11slide
IIUM
 
Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes
Uzair Salman
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
mha4
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
mha4
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
CPD INDIA
 
inheritance.pptx
inheritance.pptxinheritance.pptx
inheritance.pptx
sonukumarjha12
 
11.Object Oriented Programming.pdf
11.Object Oriented Programming.pdf11.Object Oriented Programming.pdf
11.Object Oriented Programming.pdf
Export Promotion Bureau
 
L03 Software Design
L03 Software DesignL03 Software Design
L03 Software Design
Ólafur Andri Ragnarsson
 
Inheritance
InheritanceInheritance
C# Summer course - Lecture 4
C# Summer course - Lecture 4C# Summer course - Lecture 4
C# Summer course - Lecture 4
mohamedsamyali
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry Level
Ramrao Desai
 
Object oriented concepts
Object oriented conceptsObject oriented concepts
Object oriented concepts
Gousalya Ramachandran
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
madhavi patil
 

Similar to A457405934_21789_26_2018_Inheritance.ppt (20)

Inheritance in java
Inheritance in java Inheritance in java
Inheritance in java
 
Chap-3 Inheritance.pptx
Chap-3 Inheritance.pptxChap-3 Inheritance.pptx
Chap-3 Inheritance.pptx
 
Oop
OopOop
Oop
 
11slide.ppt
11slide.ppt11slide.ppt
11slide.ppt
 
‫Chapter3 inheritance
‫Chapter3 inheritance‫Chapter3 inheritance
‫Chapter3 inheritance
 
OOPs Concepts - Android Programming
OOPs Concepts - Android ProgrammingOOPs Concepts - Android Programming
OOPs Concepts - Android Programming
 
11slide
11slide11slide
11slide
 
Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
 
JavaScript OOP
JavaScript OOPJavaScript OOP
JavaScript OOP
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
inheritance.pptx
inheritance.pptxinheritance.pptx
inheritance.pptx
 
11.Object Oriented Programming.pdf
11.Object Oriented Programming.pdf11.Object Oriented Programming.pdf
11.Object Oriented Programming.pdf
 
L03 Software Design
L03 Software DesignL03 Software Design
L03 Software Design
 
Inheritance
InheritanceInheritance
Inheritance
 
C# Summer course - Lecture 4
C# Summer course - Lecture 4C# Summer course - Lecture 4
C# Summer course - Lecture 4
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry Level
 
Object oriented concepts
Object oriented conceptsObject oriented concepts
Object oriented concepts
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
 

More from RithwikRanjan

A1771937735_21789_14_2018__16_ Nested Classes.ppt
A1771937735_21789_14_2018__16_ Nested Classes.pptA1771937735_21789_14_2018__16_ Nested Classes.ppt
A1771937735_21789_14_2018__16_ Nested Classes.ppt
RithwikRanjan
 
INTERNSHIP PRESENTATION.pptx
INTERNSHIP PRESENTATION.pptxINTERNSHIP PRESENTATION.pptx
INTERNSHIP PRESENTATION.pptx
RithwikRanjan
 
internship.pptx
internship.pptxinternship.pptx
internship.pptx
RithwikRanjan
 
AM.pptx
AM.pptxAM.pptx
AM.pptx
RithwikRanjan
 
4_A1208223655_21789_2_2018_04. Operators.ppt
4_A1208223655_21789_2_2018_04. Operators.ppt4_A1208223655_21789_2_2018_04. Operators.ppt
4_A1208223655_21789_2_2018_04. Operators.ppt
RithwikRanjan
 
sam_technical_seminar.pptx
sam_technical_seminar.pptxsam_technical_seminar.pptx
sam_technical_seminar.pptx
RithwikRanjan
 
sam_report.pptx
sam_report.pptxsam_report.pptx
sam_report.pptx
RithwikRanjan
 
TQM-Module 5.pptx
TQM-Module 5.pptxTQM-Module 5.pptx
TQM-Module 5.pptx
RithwikRanjan
 
CIM MODULE 2.pptx
CIM MODULE 2.pptxCIM MODULE 2.pptx
CIM MODULE 2.pptx
RithwikRanjan
 
A2003822018_21789_17_2018_09. ArrayList.ppt
A2003822018_21789_17_2018_09. ArrayList.pptA2003822018_21789_17_2018_09. ArrayList.ppt
A2003822018_21789_17_2018_09. ArrayList.ppt
RithwikRanjan
 
0_A1590026209_21789_20_2018_0 Lecture.ppt
0_A1590026209_21789_20_2018_0 Lecture.ppt0_A1590026209_21789_20_2018_0 Lecture.ppt
0_A1590026209_21789_20_2018_0 Lecture.ppt
RithwikRanjan
 
6_A1944859510_21789_2_2018_06. Branching Statements.ppt
6_A1944859510_21789_2_2018_06. Branching Statements.ppt6_A1944859510_21789_2_2018_06. Branching Statements.ppt
6_A1944859510_21789_2_2018_06. Branching Statements.ppt
RithwikRanjan
 
A1869984431_21789_28_2018_Abstract Class.ppt
A1869984431_21789_28_2018_Abstract Class.pptA1869984431_21789_28_2018_Abstract Class.ppt
A1869984431_21789_28_2018_Abstract Class.ppt
RithwikRanjan
 

More from RithwikRanjan (13)

A1771937735_21789_14_2018__16_ Nested Classes.ppt
A1771937735_21789_14_2018__16_ Nested Classes.pptA1771937735_21789_14_2018__16_ Nested Classes.ppt
A1771937735_21789_14_2018__16_ Nested Classes.ppt
 
INTERNSHIP PRESENTATION.pptx
INTERNSHIP PRESENTATION.pptxINTERNSHIP PRESENTATION.pptx
INTERNSHIP PRESENTATION.pptx
 
internship.pptx
internship.pptxinternship.pptx
internship.pptx
 
AM.pptx
AM.pptxAM.pptx
AM.pptx
 
4_A1208223655_21789_2_2018_04. Operators.ppt
4_A1208223655_21789_2_2018_04. Operators.ppt4_A1208223655_21789_2_2018_04. Operators.ppt
4_A1208223655_21789_2_2018_04. Operators.ppt
 
sam_technical_seminar.pptx
sam_technical_seminar.pptxsam_technical_seminar.pptx
sam_technical_seminar.pptx
 
sam_report.pptx
sam_report.pptxsam_report.pptx
sam_report.pptx
 
TQM-Module 5.pptx
TQM-Module 5.pptxTQM-Module 5.pptx
TQM-Module 5.pptx
 
CIM MODULE 2.pptx
CIM MODULE 2.pptxCIM MODULE 2.pptx
CIM MODULE 2.pptx
 
A2003822018_21789_17_2018_09. ArrayList.ppt
A2003822018_21789_17_2018_09. ArrayList.pptA2003822018_21789_17_2018_09. ArrayList.ppt
A2003822018_21789_17_2018_09. ArrayList.ppt
 
0_A1590026209_21789_20_2018_0 Lecture.ppt
0_A1590026209_21789_20_2018_0 Lecture.ppt0_A1590026209_21789_20_2018_0 Lecture.ppt
0_A1590026209_21789_20_2018_0 Lecture.ppt
 
6_A1944859510_21789_2_2018_06. Branching Statements.ppt
6_A1944859510_21789_2_2018_06. Branching Statements.ppt6_A1944859510_21789_2_2018_06. Branching Statements.ppt
6_A1944859510_21789_2_2018_06. Branching Statements.ppt
 
A1869984431_21789_28_2018_Abstract Class.ppt
A1869984431_21789_28_2018_Abstract Class.pptA1869984431_21789_28_2018_Abstract Class.ppt
A1869984431_21789_28_2018_Abstract Class.ppt
 

Recently uploaded

Empowering Limpopo Entrepreneurs Consulting SMEs.pptx
Empowering Limpopo Entrepreneurs  Consulting SMEs.pptxEmpowering Limpopo Entrepreneurs  Consulting SMEs.pptx
Empowering Limpopo Entrepreneurs Consulting SMEs.pptx
Precious Mvulane CA (SA),RA
 
What Are The Immediate Steps To Take When The VW Temperature Light Starts Fla...
What Are The Immediate Steps To Take When The VW Temperature Light Starts Fla...What Are The Immediate Steps To Take When The VW Temperature Light Starts Fla...
What Are The Immediate Steps To Take When The VW Temperature Light Starts Fla...
Import Motorworks
 
欧洲杯比赛投注官网-欧洲杯比赛投注官网网站-欧洲杯比赛投注官网|【​网址​🎉ac123.net🎉​】
欧洲杯比赛投注官网-欧洲杯比赛投注官网网站-欧洲杯比赛投注官网|【​网址​🎉ac123.net🎉​】欧洲杯比赛投注官网-欧洲杯比赛投注官网网站-欧洲杯比赛投注官网|【​网址​🎉ac123.net🎉​】
欧洲杯比赛投注官网-欧洲杯比赛投注官网网站-欧洲杯比赛投注官网|【​网址​🎉ac123.net🎉​】
ahmedendrise81
 
Bài tập - Tiếng anh 11 Global Success UNIT 1 - Bản HS.doc
Bài tập - Tiếng anh 11 Global Success UNIT 1 - Bản HS.docBài tập - Tiếng anh 11 Global Success UNIT 1 - Bản HS.doc
Bài tập - Tiếng anh 11 Global Success UNIT 1 - Bản HS.doc
daothibichhang1
 
What Does the PARKTRONIC Inoperative, See Owner's Manual Message Mean for You...
What Does the PARKTRONIC Inoperative, See Owner's Manual Message Mean for You...What Does the PARKTRONIC Inoperative, See Owner's Manual Message Mean for You...
What Does the PARKTRONIC Inoperative, See Owner's Manual Message Mean for You...
Autohaus Service and Sales
 
5 Warning Signs Your BMW's Intelligent Battery Sensor Needs Attention
5 Warning Signs Your BMW's Intelligent Battery Sensor Needs Attention5 Warning Signs Your BMW's Intelligent Battery Sensor Needs Attention
5 Warning Signs Your BMW's Intelligent Battery Sensor Needs Attention
Bertini's German Motors
 
一比一原版(AIS毕业证)奥克兰商学院毕业证成绩单如何办理
一比一原版(AIS毕业证)奥克兰商学院毕业证成绩单如何办理一比一原版(AIS毕业证)奥克兰商学院毕业证成绩单如何办理
一比一原版(AIS毕业证)奥克兰商学院毕业证成绩单如何办理
eygkup
 
Antique Plastic Traders Company Profile
Antique Plastic Traders Company ProfileAntique Plastic Traders Company Profile
Antique Plastic Traders Company Profile
Antique Plastic Traders
 
What Causes 'Trans Failsafe Prog' to Trigger in BMW X5
What Causes 'Trans Failsafe Prog' to Trigger in BMW X5What Causes 'Trans Failsafe Prog' to Trigger in BMW X5
What Causes 'Trans Failsafe Prog' to Trigger in BMW X5
European Service Center
 
Tyre Industrymarket overview with examples of CEAT
Tyre Industrymarket overview with examples of CEATTyre Industrymarket overview with examples of CEAT
Tyre Industrymarket overview with examples of CEAT
kshamashah95
 
Wondering if Your Mercedes EIS is at Fault Here’s How to Tell
Wondering if Your Mercedes EIS is at Fault Here’s How to TellWondering if Your Mercedes EIS is at Fault Here’s How to Tell
Wondering if Your Mercedes EIS is at Fault Here’s How to Tell
Vic Auto Collision & Repair
 
Things to remember while upgrading the brakes of your car
Things to remember while upgrading the brakes of your carThings to remember while upgrading the brakes of your car
Things to remember while upgrading the brakes of your car
jennifermiller8137
 
Digital Fleet Management - Why Your Business Need It?
Digital Fleet Management - Why Your Business Need It?Digital Fleet Management - Why Your Business Need It?
Digital Fleet Management - Why Your Business Need It?
jennifermiller8137
 
Why Isn't Your BMW X5's Comfort Access Functioning Properly Find Out Here
Why Isn't Your BMW X5's Comfort Access Functioning Properly Find Out HereWhy Isn't Your BMW X5's Comfort Access Functioning Properly Find Out Here
Why Isn't Your BMW X5's Comfort Access Functioning Properly Find Out Here
Masters European & Gapanese Auto Repair
 
One compartment Model Deliverdddddded.pdf
One compartment Model Deliverdddddded.pdfOne compartment Model Deliverdddddded.pdf
One compartment Model Deliverdddddded.pdf
RehanRustam2
 
Strategic Management - Strategies of Rolls Royce
Strategic Management - Strategies of Rolls RoyceStrategic Management - Strategies of Rolls Royce
Strategic Management - Strategies of Rolls Royce
SadmanFuad1
 
一比一原版(AUT毕业证)奥克兰理工大学毕业证成绩单如何办理
一比一原版(AUT毕业证)奥克兰理工大学毕业证成绩单如何办理一比一原版(AUT毕业证)奥克兰理工大学毕业证成绩单如何办理
一比一原版(AUT毕业证)奥克兰理工大学毕业证成绩单如何办理
mymwpc
 
Regeneration of Diesel Particulate Filter in Automobile
Regeneration of Diesel Particulate Filter in AutomobileRegeneration of Diesel Particulate Filter in Automobile
Regeneration of Diesel Particulate Filter in Automobile
AtanuGhosh62
 
What Could Cause The Headlights On Your Porsche 911 To Stop Working
What Could Cause The Headlights On Your Porsche 911 To Stop WorkingWhat Could Cause The Headlights On Your Porsche 911 To Stop Working
What Could Cause The Headlights On Your Porsche 911 To Stop Working
Lancer Service
 
Statistics5,c.xz,c.;c.;d.c;d;ssssss.pptx
Statistics5,c.xz,c.;c.;d.c;d;ssssss.pptxStatistics5,c.xz,c.;c.;d.c;d;ssssss.pptx
Statistics5,c.xz,c.;c.;d.c;d;ssssss.pptx
coc7987515756
 

Recently uploaded (20)

Empowering Limpopo Entrepreneurs Consulting SMEs.pptx
Empowering Limpopo Entrepreneurs  Consulting SMEs.pptxEmpowering Limpopo Entrepreneurs  Consulting SMEs.pptx
Empowering Limpopo Entrepreneurs Consulting SMEs.pptx
 
What Are The Immediate Steps To Take When The VW Temperature Light Starts Fla...
What Are The Immediate Steps To Take When The VW Temperature Light Starts Fla...What Are The Immediate Steps To Take When The VW Temperature Light Starts Fla...
What Are The Immediate Steps To Take When The VW Temperature Light Starts Fla...
 
欧洲杯比赛投注官网-欧洲杯比赛投注官网网站-欧洲杯比赛投注官网|【​网址​🎉ac123.net🎉​】
欧洲杯比赛投注官网-欧洲杯比赛投注官网网站-欧洲杯比赛投注官网|【​网址​🎉ac123.net🎉​】欧洲杯比赛投注官网-欧洲杯比赛投注官网网站-欧洲杯比赛投注官网|【​网址​🎉ac123.net🎉​】
欧洲杯比赛投注官网-欧洲杯比赛投注官网网站-欧洲杯比赛投注官网|【​网址​🎉ac123.net🎉​】
 
Bài tập - Tiếng anh 11 Global Success UNIT 1 - Bản HS.doc
Bài tập - Tiếng anh 11 Global Success UNIT 1 - Bản HS.docBài tập - Tiếng anh 11 Global Success UNIT 1 - Bản HS.doc
Bài tập - Tiếng anh 11 Global Success UNIT 1 - Bản HS.doc
 
What Does the PARKTRONIC Inoperative, See Owner's Manual Message Mean for You...
What Does the PARKTRONIC Inoperative, See Owner's Manual Message Mean for You...What Does the PARKTRONIC Inoperative, See Owner's Manual Message Mean for You...
What Does the PARKTRONIC Inoperative, See Owner's Manual Message Mean for You...
 
5 Warning Signs Your BMW's Intelligent Battery Sensor Needs Attention
5 Warning Signs Your BMW's Intelligent Battery Sensor Needs Attention5 Warning Signs Your BMW's Intelligent Battery Sensor Needs Attention
5 Warning Signs Your BMW's Intelligent Battery Sensor Needs Attention
 
一比一原版(AIS毕业证)奥克兰商学院毕业证成绩单如何办理
一比一原版(AIS毕业证)奥克兰商学院毕业证成绩单如何办理一比一原版(AIS毕业证)奥克兰商学院毕业证成绩单如何办理
一比一原版(AIS毕业证)奥克兰商学院毕业证成绩单如何办理
 
Antique Plastic Traders Company Profile
Antique Plastic Traders Company ProfileAntique Plastic Traders Company Profile
Antique Plastic Traders Company Profile
 
What Causes 'Trans Failsafe Prog' to Trigger in BMW X5
What Causes 'Trans Failsafe Prog' to Trigger in BMW X5What Causes 'Trans Failsafe Prog' to Trigger in BMW X5
What Causes 'Trans Failsafe Prog' to Trigger in BMW X5
 
Tyre Industrymarket overview with examples of CEAT
Tyre Industrymarket overview with examples of CEATTyre Industrymarket overview with examples of CEAT
Tyre Industrymarket overview with examples of CEAT
 
Wondering if Your Mercedes EIS is at Fault Here’s How to Tell
Wondering if Your Mercedes EIS is at Fault Here’s How to TellWondering if Your Mercedes EIS is at Fault Here’s How to Tell
Wondering if Your Mercedes EIS is at Fault Here’s How to Tell
 
Things to remember while upgrading the brakes of your car
Things to remember while upgrading the brakes of your carThings to remember while upgrading the brakes of your car
Things to remember while upgrading the brakes of your car
 
Digital Fleet Management - Why Your Business Need It?
Digital Fleet Management - Why Your Business Need It?Digital Fleet Management - Why Your Business Need It?
Digital Fleet Management - Why Your Business Need It?
 
Why Isn't Your BMW X5's Comfort Access Functioning Properly Find Out Here
Why Isn't Your BMW X5's Comfort Access Functioning Properly Find Out HereWhy Isn't Your BMW X5's Comfort Access Functioning Properly Find Out Here
Why Isn't Your BMW X5's Comfort Access Functioning Properly Find Out Here
 
One compartment Model Deliverdddddded.pdf
One compartment Model Deliverdddddded.pdfOne compartment Model Deliverdddddded.pdf
One compartment Model Deliverdddddded.pdf
 
Strategic Management - Strategies of Rolls Royce
Strategic Management - Strategies of Rolls RoyceStrategic Management - Strategies of Rolls Royce
Strategic Management - Strategies of Rolls Royce
 
一比一原版(AUT毕业证)奥克兰理工大学毕业证成绩单如何办理
一比一原版(AUT毕业证)奥克兰理工大学毕业证成绩单如何办理一比一原版(AUT毕业证)奥克兰理工大学毕业证成绩单如何办理
一比一原版(AUT毕业证)奥克兰理工大学毕业证成绩单如何办理
 
Regeneration of Diesel Particulate Filter in Automobile
Regeneration of Diesel Particulate Filter in AutomobileRegeneration of Diesel Particulate Filter in Automobile
Regeneration of Diesel Particulate Filter in Automobile
 
What Could Cause The Headlights On Your Porsche 911 To Stop Working
What Could Cause The Headlights On Your Porsche 911 To Stop WorkingWhat Could Cause The Headlights On Your Porsche 911 To Stop Working
What Could Cause The Headlights On Your Porsche 911 To Stop Working
 
Statistics5,c.xz,c.;c.;d.c;d;ssssss.pptx
Statistics5,c.xz,c.;c.;d.c;d;ssssss.pptxStatistics5,c.xz,c.;c.;d.c;d;ssssss.pptx
Statistics5,c.xz,c.;c.;d.c;d;ssssss.pptx
 

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.