SlideShare una empresa de Scribd logo
1 de 14
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.

Más contenido relacionado

Similar a A457405934_21789_26_2018_Inheritance.ppt

Similar a 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
 

Más de RithwikRanjan

Más de 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
 

Último

Escorts Service Rajajinagar ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Rajajinagar ☎ 7737669865☎ Book Your One night Stand (Bangalore)Escorts Service Rajajinagar ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Rajajinagar ☎ 7737669865☎ Book Your One night Stand (Bangalore)
amitlee9823
 
Vip Mumbai Call Girls Panvel Call On 9920725232 With Body to body massage wit...
Vip Mumbai Call Girls Panvel Call On 9920725232 With Body to body massage wit...Vip Mumbai Call Girls Panvel Call On 9920725232 With Body to body massage wit...
Vip Mumbai Call Girls Panvel Call On 9920725232 With Body to body massage wit...
amitlee9823
 
Sanjay Nagar Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalor...
Sanjay Nagar Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalor...Sanjay Nagar Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalor...
Sanjay Nagar Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalor...
amitlee9823
 
Top Rated Call Girls South Mumbai : 9920725232 We offer Beautiful and sexy Ca...
Top Rated Call Girls South Mumbai : 9920725232 We offer Beautiful and sexy Ca...Top Rated Call Girls South Mumbai : 9920725232 We offer Beautiful and sexy Ca...
Top Rated Call Girls South Mumbai : 9920725232 We offer Beautiful and sexy Ca...
amitlee9823
 
如何办理(NCL毕业证书)纽卡斯尔大学毕业证毕业证成绩单原版一比一
如何办理(NCL毕业证书)纽卡斯尔大学毕业证毕业证成绩单原版一比一如何办理(NCL毕业证书)纽卡斯尔大学毕业证毕业证成绩单原版一比一
如何办理(NCL毕业证书)纽卡斯尔大学毕业证毕业证成绩单原版一比一
avy6anjnd
 
Call Girls Hongasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Hongasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Hongasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Hongasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
amitlee9823
 
Vip Mumbai Call Girls Navi Mumbai Call On 9920725232 With Body to body massag...
Vip Mumbai Call Girls Navi Mumbai Call On 9920725232 With Body to body massag...Vip Mumbai Call Girls Navi Mumbai Call On 9920725232 With Body to body massag...
Vip Mumbai Call Girls Navi Mumbai Call On 9920725232 With Body to body massag...
amitlee9823
 
Top Rated Call Girls Navi Mumbai : 9920725232 We offer Beautiful and sexy Cal...
Top Rated Call Girls Navi Mumbai : 9920725232 We offer Beautiful and sexy Cal...Top Rated Call Girls Navi Mumbai : 9920725232 We offer Beautiful and sexy Cal...
Top Rated Call Girls Navi Mumbai : 9920725232 We offer Beautiful and sexy Cal...
amitlee9823
 
➥🔝 7737669865 🔝▻ Asansol Call-girls in Women Seeking Men 🔝Asansol🔝 Escorts...
➥🔝 7737669865 🔝▻ Asansol Call-girls in Women Seeking Men  🔝Asansol🔝   Escorts...➥🔝 7737669865 🔝▻ Asansol Call-girls in Women Seeking Men  🔝Asansol🔝   Escorts...
➥🔝 7737669865 🔝▻ Asansol Call-girls in Women Seeking Men 🔝Asansol🔝 Escorts...
amitlee9823
 
Top Rated Call Girls Mumbai Central : 9920725232 We offer Beautiful and sexy ...
Top Rated Call Girls Mumbai Central : 9920725232 We offer Beautiful and sexy ...Top Rated Call Girls Mumbai Central : 9920725232 We offer Beautiful and sexy ...
Top Rated Call Girls Mumbai Central : 9920725232 We offer Beautiful and sexy ...
amitlee9823
 
Vip Mumbai Call Girls Colaba Call On 9920725232 With Body to body massage wit...
Vip Mumbai Call Girls Colaba Call On 9920725232 With Body to body massage wit...Vip Mumbai Call Girls Colaba Call On 9920725232 With Body to body massage wit...
Vip Mumbai Call Girls Colaba Call On 9920725232 With Body to body massage wit...
amitlee9823
 
➥🔝 7737669865 🔝▻ Moradabad Call-girls in Women Seeking Men 🔝Moradabad🔝 Esc...
➥🔝 7737669865 🔝▻ Moradabad Call-girls in Women Seeking Men  🔝Moradabad🔝   Esc...➥🔝 7737669865 🔝▻ Moradabad Call-girls in Women Seeking Men  🔝Moradabad🔝   Esc...
➥🔝 7737669865 🔝▻ Moradabad Call-girls in Women Seeking Men 🔝Moradabad🔝 Esc...
amitlee9823
 
Call Girls in Patel Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Patel Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Patel Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Patel Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Último (20)

Escorts Service Rajajinagar ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Rajajinagar ☎ 7737669865☎ Book Your One night Stand (Bangalore)Escorts Service Rajajinagar ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Rajajinagar ☎ 7737669865☎ Book Your One night Stand (Bangalore)
 
Vip Mumbai Call Girls Panvel Call On 9920725232 With Body to body massage wit...
Vip Mumbai Call Girls Panvel Call On 9920725232 With Body to body massage wit...Vip Mumbai Call Girls Panvel Call On 9920725232 With Body to body massage wit...
Vip Mumbai Call Girls Panvel Call On 9920725232 With Body to body massage wit...
 
Sanjay Nagar Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalor...
Sanjay Nagar Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalor...Sanjay Nagar Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalor...
Sanjay Nagar Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalor...
 
8377087607, Door Step Call Girls In Majnu Ka Tilla (Delhi) 24/7 Available
8377087607, Door Step Call Girls In Majnu Ka Tilla (Delhi) 24/7 Available8377087607, Door Step Call Girls In Majnu Ka Tilla (Delhi) 24/7 Available
8377087607, Door Step Call Girls In Majnu Ka Tilla (Delhi) 24/7 Available
 
Top Rated Call Girls South Mumbai : 9920725232 We offer Beautiful and sexy Ca...
Top Rated Call Girls South Mumbai : 9920725232 We offer Beautiful and sexy Ca...Top Rated Call Girls South Mumbai : 9920725232 We offer Beautiful and sexy Ca...
Top Rated Call Girls South Mumbai : 9920725232 We offer Beautiful and sexy Ca...
 
What Does The Engine Malfunction Reduced Power Message Mean For Your BMW X5
What Does The Engine Malfunction Reduced Power Message Mean For Your BMW X5What Does The Engine Malfunction Reduced Power Message Mean For Your BMW X5
What Does The Engine Malfunction Reduced Power Message Mean For Your BMW X5
 
如何办理(NCL毕业证书)纽卡斯尔大学毕业证毕业证成绩单原版一比一
如何办理(NCL毕业证书)纽卡斯尔大学毕业证毕业证成绩单原版一比一如何办理(NCL毕业证书)纽卡斯尔大学毕业证毕业证成绩单原版一比一
如何办理(NCL毕业证书)纽卡斯尔大学毕业证毕业证成绩单原版一比一
 
John Deere 7430 7530 Tractors Diagnostic Service Manual W.pdf
John Deere 7430 7530 Tractors Diagnostic Service Manual W.pdfJohn Deere 7430 7530 Tractors Diagnostic Service Manual W.pdf
John Deere 7430 7530 Tractors Diagnostic Service Manual W.pdf
 
Call Girls Hongasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Hongasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Hongasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Hongasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
 
Vip Mumbai Call Girls Navi Mumbai Call On 9920725232 With Body to body massag...
Vip Mumbai Call Girls Navi Mumbai Call On 9920725232 With Body to body massag...Vip Mumbai Call Girls Navi Mumbai Call On 9920725232 With Body to body massag...
Vip Mumbai Call Girls Navi Mumbai Call On 9920725232 With Body to body massag...
 
Top Rated Call Girls Navi Mumbai : 9920725232 We offer Beautiful and sexy Cal...
Top Rated Call Girls Navi Mumbai : 9920725232 We offer Beautiful and sexy Cal...Top Rated Call Girls Navi Mumbai : 9920725232 We offer Beautiful and sexy Cal...
Top Rated Call Girls Navi Mumbai : 9920725232 We offer Beautiful and sexy Cal...
 
➥🔝 7737669865 🔝▻ Asansol Call-girls in Women Seeking Men 🔝Asansol🔝 Escorts...
➥🔝 7737669865 🔝▻ Asansol Call-girls in Women Seeking Men  🔝Asansol🔝   Escorts...➥🔝 7737669865 🔝▻ Asansol Call-girls in Women Seeking Men  🔝Asansol🔝   Escorts...
➥🔝 7737669865 🔝▻ Asansol Call-girls in Women Seeking Men 🔝Asansol🔝 Escorts...
 
Call Girls in Malviya Nagar Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts Ser...
Call Girls in Malviya Nagar Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts Ser...Call Girls in Malviya Nagar Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts Ser...
Call Girls in Malviya Nagar Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts Ser...
 
Call Girls in Malviya Nagar Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts Ser...
Call Girls in Malviya Nagar Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts Ser...Call Girls in Malviya Nagar Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts Ser...
Call Girls in Malviya Nagar Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts Ser...
 
Top Rated Call Girls Mumbai Central : 9920725232 We offer Beautiful and sexy ...
Top Rated Call Girls Mumbai Central : 9920725232 We offer Beautiful and sexy ...Top Rated Call Girls Mumbai Central : 9920725232 We offer Beautiful and sexy ...
Top Rated Call Girls Mumbai Central : 9920725232 We offer Beautiful and sexy ...
 
Vip Mumbai Call Girls Colaba Call On 9920725232 With Body to body massage wit...
Vip Mumbai Call Girls Colaba Call On 9920725232 With Body to body massage wit...Vip Mumbai Call Girls Colaba Call On 9920725232 With Body to body massage wit...
Vip Mumbai Call Girls Colaba Call On 9920725232 With Body to body massage wit...
 
(ISHITA) Call Girls Service Jammu Call Now 8617697112 Jammu Escorts 24x7
(ISHITA) Call Girls Service Jammu Call Now 8617697112 Jammu Escorts 24x7(ISHITA) Call Girls Service Jammu Call Now 8617697112 Jammu Escorts 24x7
(ISHITA) Call Girls Service Jammu Call Now 8617697112 Jammu Escorts 24x7
 
Dubai Call Girls R0yalty O525547819 Call Girls Dubai
Dubai Call Girls R0yalty O525547819 Call Girls DubaiDubai Call Girls R0yalty O525547819 Call Girls Dubai
Dubai Call Girls R0yalty O525547819 Call Girls Dubai
 
➥🔝 7737669865 🔝▻ Moradabad Call-girls in Women Seeking Men 🔝Moradabad🔝 Esc...
➥🔝 7737669865 🔝▻ Moradabad Call-girls in Women Seeking Men  🔝Moradabad🔝   Esc...➥🔝 7737669865 🔝▻ Moradabad Call-girls in Women Seeking Men  🔝Moradabad🔝   Esc...
➥🔝 7737669865 🔝▻ Moradabad Call-girls in Women Seeking Men 🔝Moradabad🔝 Esc...
 
Call Girls in Patel Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Patel Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Patel Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Patel Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 

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.