SlideShare a Scribd company logo
1 of 14
MANPREET SINGH
INHERITANCE
Inheritance in Java is a mechanism in which one object acquires all the
properties and behaviors of a parent object.
The idea behind inheritance in Java is that you can create new classes that are
built upon existing classes. When we inherit from an existing class, we can
reuse methods of the parent class. Moreover, you can add new methods in
your current class also.
For Code Reusability.
THE SYNTAX OF JAVA INHERITANCE
Class Superclass-name
{
//methods
}
class Subclass-name extends Superclass-name
{
//methods
}
//Keyword
//extends
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.
TYPES OF INHERITANCE
Note: Multiple inheritance is not supported in Java
through class.
OTHER ARE INHERITANCE
SINGLE INHERITANCE
class Parent //Parent Class...
{
void show() // Function declaration
{ System.out.println("Show() of class Parent"); }
}
class child extends Parent // Child is inheriting the properties of Parent class
{
public static void main(String arg[])
{ System.out.println("Hello from class child");
child obj = new child(); // Object creation
obj.show();
}
}
OUTPUT
MULTILEVEL INHERITANCE
class version{
void version(){System.out.println("version");}
}
class version1 extends version{
void version1(){System.out.println("version1");}
}
class version2 extends version1
{ public static void main(String args[]){
version2 d= new version2();
d.version(); d.version1();d.version2();
}
void version2(){System.out.println("version2");}
}
OUTPUT
HIERARCHICAL INHERITANCE EXAMPLE
class version{ void version(){System.out.println("version");} }
class version1 extends version{ void version1(){System.out.println("version1");} }
class version2 extends version{void version2(){System.out.println("version2");}}
class all
{
public static void main(String args[])
{ version1 d1= new version1();
version2 d2= new version2();
d1.version(); d1.version1();
d2.version(); d2.version2();
}
}
OUTPUT
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 the same method and you call it from
child class object, there will be ambiguity to call the method of A or B class.
Since compile-time errors are better than runtime errors, Java renders compile-
time error if you inherit 2 classes. So whether you have same method or
different, there will be compile time error.
PROGRAM:
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 invoke
d?
}
}
OUTPUT Compile Time Error
Which mean Hybrid Inheritance is also not possible.
?That how is this possible?
If we need multiple or hybrid
inheritance
INTERFACE
interface one
{ void show(); int a=10; }
interface one1
{ void show(); int b=10; }
class inth implements one,one1
{ public static void main(String arg[])
{ System.out.println("hlo from main");
inth ob= new inth();
ob.show(); }
public void show() // should be public other vice error
{System.out.println("sum is >>>>"+(a+b));}
}
OUTPUT
NARROW AND WIDENING
WIDENING
class parent
{ void show1() {System.out.println("show 1");} }
class child extends parent
{ void show2() {System.out.println("show 2");}
public static void main(String arg[])
{ child obj= new child();
obj.show2();
obj.show1(); }
}
OUTPUT
NARROW
class parent
{ void show1() {System.out.println("show 1");} }
class child extends parent
{ void show2() {System.out.println("show 2");}
public static void main(String arg[])
{ parent obj= new child();
obj.show1();
obj.show2(); }
}
OUTPUT
THANK YOU

More Related Content

What's hot

Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingNithyaN19
ย 
Inheritance and Polymorphism Java
Inheritance and Polymorphism JavaInheritance and Polymorphism Java
Inheritance and Polymorphism JavaM. Raihan
ย 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oopsHirra Sultan
ย 
Inheritance in c++ part1
Inheritance in c++ part1Inheritance in c++ part1
Inheritance in c++ part1Mirza Hussain
ย 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Vineeta Garg
ย 
Dacj 2-1 a
Dacj 2-1 aDacj 2-1 a
Dacj 2-1 aNiit Care
ย 
Overloading and overriding in vb.net
Overloading and overriding in vb.netOverloading and overriding in vb.net
Overloading and overriding in vb.netsuraj pandey
ย 
inheritance
inheritanceinheritance
inheritanceMohit Patodia
ย 
Inheritance in Object Oriented Programming
Inheritance in Object Oriented ProgrammingInheritance in Object Oriented Programming
Inheritance in Object Oriented ProgrammingAshita Agrawal
ย 
Java Inheritance
Java InheritanceJava Inheritance
Java InheritanceManish Tiwari
ย 
Lecture 14 (inheritance basics)
Lecture 14 (inheritance basics)Lecture 14 (inheritance basics)
Lecture 14 (inheritance basics)Abhishek Khune
ย 
JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)Prof. Erwin Globio
ย 
Inheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingInheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingArslan Waseem
ย 
Interface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationInterface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationHoneyChintal
ย 
Java interface
Java interfaceJava interface
Java interfaceArati Gadgil
ย 
Inheritance
InheritanceInheritance
InheritancePadma Kannan
ย 

What's hot (20)

Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
ย 
Inheritance and Polymorphism Java
Inheritance and Polymorphism JavaInheritance and Polymorphism Java
Inheritance and Polymorphism Java
ย 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oops
ย 
Inheritance in c++ part1
Inheritance in c++ part1Inheritance in c++ part1
Inheritance in c++ part1
ย 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
ย 
Dacj 2-1 a
Dacj 2-1 aDacj 2-1 a
Dacj 2-1 a
ย 
Overloading and overriding in vb.net
Overloading and overriding in vb.netOverloading and overriding in vb.net
Overloading and overriding in vb.net
ย 
inheritance
inheritanceinheritance
inheritance
ย 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
ย 
Inheritance in Object Oriented Programming
Inheritance in Object Oriented ProgrammingInheritance in Object Oriented Programming
Inheritance in Object Oriented Programming
ย 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
ย 
Inheritance
InheritanceInheritance
Inheritance
ย 
Lecture 14 (inheritance basics)
Lecture 14 (inheritance basics)Lecture 14 (inheritance basics)
Lecture 14 (inheritance basics)
ย 
JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)
ย 
Inheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingInheritance, Object Oriented Programming
Inheritance, Object Oriented Programming
ย 
Interface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationInterface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementation
ย 
Java interface
Java interfaceJava interface
Java interface
ย 
Inheritance
InheritanceInheritance
Inheritance
ย 
inheritance c++
inheritance c++inheritance c++
inheritance c++
ย 
Inheritance C#
Inheritance C#Inheritance C#
Inheritance C#
ย 

Similar to INHERTANCE , NARROW AND WIDENING

Java inheritance
Java inheritanceJava inheritance
Java inheritanceBHUVIJAYAVELU
ย 
Inheritance used in java
Inheritance used in javaInheritance used in java
Inheritance used in javaTharuniDiddekunta
ย 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance pptNivegeetha
ย 
Inheritance and interface
Inheritance and interfaceInheritance and interface
Inheritance and interfaceShubham Sharma
ย 
Inheritance in Java.pdf
Inheritance in Java.pdfInheritance in Java.pdf
Inheritance in Java.pdfkumari36
ย 
Inheritance in Java beginner to advance with examples.pptx
Inheritance in Java beginner to advance with examples.pptxInheritance in Java beginner to advance with examples.pptx
Inheritance in Java beginner to advance with examples.pptxnaeemcse
ย 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxSodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxRudranilDas11
ย 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritanceArjun Shanka
ย 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaRahulAnanda1
ย 
inheritance.pptx
inheritance.pptxinheritance.pptx
inheritance.pptxsonukumarjha12
ย 
Chap-3 Inheritance.pptx
Chap-3 Inheritance.pptxChap-3 Inheritance.pptx
Chap-3 Inheritance.pptxchetanpatilcp783
ย 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javachauhankapil
ย 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaTech_MX
ย 
OCA Java SE 8 Exam Chapter 5 Class Design
OCA Java SE 8 Exam Chapter 5 Class DesignOCA Java SE 8 Exam Chapter 5 Class Design
OCA Java SE 8 Exam Chapter 5 Class Designฤฐbrahim Kรผrce
ย 
INHERITANCE.pptx
INHERITANCE.pptxINHERITANCE.pptx
INHERITANCE.pptxHARIPRIYA M P
ย 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaAriful Islam
ย 
INHERITANCE IN JAVA.pptx
INHERITANCE IN JAVA.pptxINHERITANCE IN JAVA.pptx
INHERITANCE IN JAVA.pptxNITHISG1
ย 

Similar to INHERTANCE , NARROW AND WIDENING (20)

Java inheritance
Java inheritanceJava inheritance
Java inheritance
ย 
Inheritance used in java
Inheritance used in javaInheritance used in java
Inheritance used in java
ย 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance ppt
ย 
Inheritance and interface
Inheritance and interfaceInheritance and interface
Inheritance and interface
ย 
Inheritance in Java.pdf
Inheritance in Java.pdfInheritance in Java.pdf
Inheritance in Java.pdf
ย 
Inheritance in Java beginner to advance with examples.pptx
Inheritance in Java beginner to advance with examples.pptxInheritance in Java beginner to advance with examples.pptx
Inheritance in Java beginner to advance with examples.pptx
ย 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxSodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
ย 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
ย 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
ย 
inheritance.pptx
inheritance.pptxinheritance.pptx
inheritance.pptx
ย 
Chap-3 Inheritance.pptx
Chap-3 Inheritance.pptxChap-3 Inheritance.pptx
Chap-3 Inheritance.pptx
ย 
Java
JavaJava
Java
ย 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
ย 
Inheritance
InheritanceInheritance
Inheritance
ย 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
ย 
OCA Java SE 8 Exam Chapter 5 Class Design
OCA Java SE 8 Exam Chapter 5 Class DesignOCA Java SE 8 Exam Chapter 5 Class Design
OCA Java SE 8 Exam Chapter 5 Class Design
ย 
INHERITANCE.pptx
INHERITANCE.pptxINHERITANCE.pptx
INHERITANCE.pptx
ย 
Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
ย 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
ย 
INHERITANCE IN JAVA.pptx
INHERITANCE IN JAVA.pptxINHERITANCE IN JAVA.pptx
INHERITANCE IN JAVA.pptx
ย 

Recently uploaded

ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
ย 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
ย 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
ย 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
ย 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
ย 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
ย 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
ย 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
ย 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
ย 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
ย 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
ย 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
ย 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
ย 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
ย 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
ย 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
ย 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
ย 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
ย 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
ย 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
ย 

Recently uploaded (20)

ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
ย 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
ย 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
ย 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
ย 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
ย 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
ย 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
ย 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
ย 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
ย 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
ย 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
ย 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
ย 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
ย 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
ย 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
ย 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
ย 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
ย 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
ย 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
ย 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
ย 

INHERTANCE , NARROW AND WIDENING

  • 2. INHERITANCE Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When we inherit from an existing class, we can reuse methods of the parent class. Moreover, you can add new methods in your current class also. For Code Reusability.
  • 3. THE SYNTAX OF JAVA INHERITANCE Class Superclass-name { //methods } class Subclass-name extends Superclass-name { //methods } //Keyword //extends 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.
  • 5. Note: Multiple inheritance is not supported in Java through class. OTHER ARE INHERITANCE
  • 6. SINGLE INHERITANCE class Parent //Parent Class... { void show() // Function declaration { System.out.println("Show() of class Parent"); } } class child extends Parent // Child is inheriting the properties of Parent class { public static void main(String arg[]) { System.out.println("Hello from class child"); child obj = new child(); // Object creation obj.show(); } } OUTPUT
  • 7. MULTILEVEL INHERITANCE class version{ void version(){System.out.println("version");} } class version1 extends version{ void version1(){System.out.println("version1");} } class version2 extends version1 { public static void main(String args[]){ version2 d= new version2(); d.version(); d.version1();d.version2(); } void version2(){System.out.println("version2");} } OUTPUT
  • 8. HIERARCHICAL INHERITANCE EXAMPLE class version{ void version(){System.out.println("version");} } class version1 extends version{ void version1(){System.out.println("version1");} } class version2 extends version{void version2(){System.out.println("version2");}} class all { public static void main(String args[]) { version1 d1= new version1(); version2 d2= new version2(); d1.version(); d1.version1(); d2.version(); d2.version2(); } } OUTPUT
  • 9. 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 the same method and you call it from child class object, there will be ambiguity to call the method of A or B class. Since compile-time errors are better than runtime errors, Java renders compile- time error if you inherit 2 classes. So whether you have same method or different, there will be compile time error. PROGRAM: 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 invoke d? } } OUTPUT Compile Time Error Which mean Hybrid Inheritance is also not possible. ?That how is this possible? If we need multiple or hybrid inheritance
  • 10. INTERFACE interface one { void show(); int a=10; } interface one1 { void show(); int b=10; } class inth implements one,one1 { public static void main(String arg[]) { System.out.println("hlo from main"); inth ob= new inth(); ob.show(); } public void show() // should be public other vice error {System.out.println("sum is >>>>"+(a+b));} } OUTPUT
  • 12. WIDENING class parent { void show1() {System.out.println("show 1");} } class child extends parent { void show2() {System.out.println("show 2");} public static void main(String arg[]) { child obj= new child(); obj.show2(); obj.show1(); } } OUTPUT
  • 13. NARROW class parent { void show1() {System.out.println("show 1");} } class child extends parent { void show2() {System.out.println("show 2");} public static void main(String arg[]) { parent obj= new child(); obj.show1(); obj.show2(); } } OUTPUT