SlideShare a Scribd company logo
1 of 17
INHERITANCE IN JAVA




                                   INHERITANCE
                 PRESENTED BY :-
                 SAPNA SHARMA
1
CONTENT
 Introduction to Inheritance
 Types of inheritance




                                    INHERITANCE
 Define sub class

 Single Inheritance

 Super keyword

 Multilevel Inheritance

 Method Overriding

 Abstract class

 Final keyword

                                2
INTRODUCTION TO INHERITANCE
 Reusability is yet another factor of OOP’s. Java
  classes are used in several ways. This is basically




                                                             INHERITANCE
  by creating new classes ,reusing the properties
  existing one.
 The deriving the new class from an old one is called
  inheritance.
 The old class is known as base class or super
  class.
 The class who inherit the properties of the base
  class is called derived class or sub class.
                                                         3
TYPES OF INHERITANCE

 Single inheritance(one super class)




                                                          INHERITANCE
 Multiple inheritance( several super classes)

 Multilevel inheritance(

 Hierarchical inheritance(one super class many sub
  class)




                                                      4
DEFINE SUBCLASS
class sub-classname extends super-classname
{




                                                             INHERITANCE
  variable declaration;// sub class variables
  method declaration;// sub class method
  }
  The keyword extends signifies that the properties of
  the super class are extended to the sub class.




                                                         5
EXAMPLE OF SINGLE INHERITANCE
class sum              // Super class
{
   int a=10;




                                                          INHERITANCE
   int b=20;
   void show1()
   {
           System.out.println("value of a :- " +a);
           System.out.println("value of b :- " +b);
   }
}
class sum2 extends sum             // base class
{
   public static void main(String args[])
   {
           sum2 obj = new sum2();
           obj.show1();
}
                                                      6
}
SUB CLASS CONSTRUCTOR

 A subclass constructor is used to construct the




                                                             INHERITANCE
  instance variable of both the subclass and super
  class.
 The subclass constructor uses the keyword super
  to invoke the constructor method of the super class.




                                                         7
CONDITION FOR SUPER KEYWORD
 Super may only be used within a subclass
  constructor.




                                                              INHERITANCE
 The call to super class constructor must appear as
  the first statement with in the subclass constructor.
 The parameter in the super call must match the
  order and type of the instance variable declared in
  the super class.




                                                          8
USES OF SUPER KEYWORD
   It calls the super class constructor.




                                                INHERITANCE
SYNTAX:- super( parameter list);
 Access the member of the super class.



SYNTAX:- super. member variable;




                                            9
USING SUPER TO CALL SUPER CLASS
                              CONSTRUCTOR
class demo
{    int x,y;
     public demo()
{    x=10;




                                                        INHERITANCE
     y=20;}
public demo(int i,int j)
{    x=i;
     y=j;       }}
class demo1 extends demo{
int j;
public demo1(){
super(10,15);
j=30;}
void show(){
System.out.println(x+ " " +y + " " +j);}
public static void main(String args[]){                10
demo1 d = new demo1();
d.show();}}
MULTILEVEL INHERITANCE

When a subclass derived from a super class and




                                                       INHERITANCE
a subclass is further derived from that subclass or
when a subclass acts as a super class for some
other subclass, it creates a multilevel hierarchy.




                                                      11
METHOD OVERRIDING
 In a class hierarchy , when a method in a sub class
  has the same name and type signature as a




                                                            INHERITANCE
  method in its super class, then the method is said
  to override the method in the super class.
 When an overridden method is called from within a
  sub class, it will always refer to the version of that
  method defined by the subclass. The version of the
  method defined by the super class will be hidden.



                                                           12
EXAMPLE OF METHOD OVERRIDDING
class sum                       // Super class
{
    int a=10;




                                                                                  INHERITANCE
    int b=20;
    void show()
    {
                System.out.println("value of a :- " +a);
                System.out.println("value of b :- " +b);
    }}
class sum2 extends sum          // base class
{   int i=30,j=40;
    void show()
    {           System.out.println(i+ " " +j);// only this value will be print
    }
    public static void main(String args[])
    {
                sum2 obj = new sum2();
                                                                                 13
                obj.show();}}
ABSTRACT CLASS AND METHODS
   An abstract class is a class that is declared abstract—it
    may or may not include abstract methods. Abstract
    classes cannot be instantiated, but they can be sub




                                                                 INHERITANCE
    classed.
    SYNTAX:-
    Abstract class name
    {
         }
   An abstract method is a method that is declared without
    an implementation (without braces, and followed by a
    semicolon), like this:
    SYNTAX:-
    abstract void method(Parameter);                            14
EXAMPLE OF ABSTRACT CLASS
abstract class sum
{abstract void show();
abstract void get();
}
class sum1 extends sum




                                                 INHERITANCE
{
    void show()
    {
                 System.out.println("HELLO");
                 }
    void get()
    {
    System.out.println("WELCOME");
    }
    public static void main(String args[])
    {
                 sum1 obj = new sum1();
                 obj.show();
                 obj.get();                     15
}
}
FINAL KEYWORD
Final keyword is used as follows:-




                                      INHERITANCE
1.   To prevent Overriding .
2.   To prevent inheritance
3.   To create a named constant.




                                     16
INHERITANCE
     THANK YOU
17

More Related Content

What's hot (20)

Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
 
Abstract Class Presentation
Abstract Class PresentationAbstract Class Presentation
Abstract Class Presentation
 
Inner classes in java
Inner classes in javaInner classes in java
Inner classes in java
 
Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Virtual base class
Virtual base classVirtual base class
Virtual base class
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Constructor overloading & method overloading
Constructor overloading & method overloadingConstructor overloading & method overloading
Constructor overloading & method overloading
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Chapter 07 inheritance
Chapter 07 inheritanceChapter 07 inheritance
Chapter 07 inheritance
 
Java package
Java packageJava package
Java package
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Ppt on this and super keyword
Ppt on this and super keywordPpt on this and super keyword
Ppt on this and super keyword
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
OOP java
OOP javaOOP java
OOP java
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
 

Viewers also liked

encapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingencapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingShivam Singhal
 
Principles and advantages of oop ppt
Principles and advantages of oop pptPrinciples and advantages of oop ppt
Principles and advantages of oop pptdaxesh chauhan
 
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...cprogrammings
 

Viewers also liked (10)

Polymorphism
PolymorphismPolymorphism
Polymorphism
 
encapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingencapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloading
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and Polymorphism
 
inheritance c++
inheritance c++inheritance c++
inheritance c++
 
Encapsulation C++
Encapsulation C++Encapsulation C++
Encapsulation C++
 
Principles and advantages of oop ppt
Principles and advantages of oop pptPrinciples and advantages of oop ppt
Principles and advantages of oop ppt
 
encapsulation
encapsulationencapsulation
encapsulation
 
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
 
polymorphism
polymorphism polymorphism
polymorphism
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
 

Similar to Inheritance

Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7kamal kotecha
 
Unit3 java
Unit3 javaUnit3 java
Unit3 javamrecedu
 
Inheritance Slides
Inheritance SlidesInheritance Slides
Inheritance SlidesAhsan Raja
 
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
 
Inheritance and Polymorphism in java simple and clear
Inheritance and Polymorphism in java simple and clear Inheritance and Polymorphism in java simple and clear
Inheritance and Polymorphism in java simple and clear ASHNA nadhm
 
Chap3 inheritance
Chap3 inheritanceChap3 inheritance
Chap3 inheritanceraksharao
 
Java inheritance
Java inheritanceJava inheritance
Java inheritanceSmrutiShah9
 
Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#Abid Kohistani
 
itft-Inheritance in java
itft-Inheritance in javaitft-Inheritance in java
itft-Inheritance in javaAtul Sehdev
 
INHERITANCE IN JAVA.pptx
INHERITANCE IN JAVA.pptxINHERITANCE IN JAVA.pptx
INHERITANCE IN JAVA.pptxNITHISG1
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritanceteach4uin
 

Similar to Inheritance (20)

Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7
 
Unit3 java
Unit3 javaUnit3 java
Unit3 java
 
Inheritance Slides
Inheritance SlidesInheritance Slides
Inheritance Slides
 
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
 
Sdtl assignment 03
Sdtl assignment 03Sdtl assignment 03
Sdtl assignment 03
 
Ppt of c++ vs c#
Ppt of c++ vs c#Ppt of c++ vs c#
Ppt of c++ vs c#
 
Inheritance and Polymorphism in java simple and clear
Inheritance and Polymorphism in java simple and clear Inheritance and Polymorphism in java simple and clear
Inheritance and Polymorphism in java simple and clear
 
Chap3 inheritance
Chap3 inheritanceChap3 inheritance
Chap3 inheritance
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
 
Java unit2
Java unit2Java unit2
Java unit2
 
Inheritance
InheritanceInheritance
Inheritance
 
Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#
 
Presentation 3.pdf
Presentation 3.pdfPresentation 3.pdf
Presentation 3.pdf
 
Inheritance
InheritanceInheritance
Inheritance
 
Interface
InterfaceInterface
Interface
 
itft-Inheritance in java
itft-Inheritance in javaitft-Inheritance in java
itft-Inheritance in java
 
java_inheritance.pdf
java_inheritance.pdfjava_inheritance.pdf
java_inheritance.pdf
 
INHERITANCE IN JAVA.pptx
INHERITANCE IN JAVA.pptxINHERITANCE IN JAVA.pptx
INHERITANCE IN JAVA.pptx
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritance
 

Recently uploaded

Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 

Recently uploaded (20)

Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 

Inheritance

  • 1. INHERITANCE IN JAVA INHERITANCE PRESENTED BY :- SAPNA SHARMA 1
  • 2. CONTENT  Introduction to Inheritance  Types of inheritance INHERITANCE  Define sub class  Single Inheritance  Super keyword  Multilevel Inheritance  Method Overriding  Abstract class  Final keyword 2
  • 3. INTRODUCTION TO INHERITANCE  Reusability is yet another factor of OOP’s. Java classes are used in several ways. This is basically INHERITANCE by creating new classes ,reusing the properties existing one.  The deriving the new class from an old one is called inheritance.  The old class is known as base class or super class.  The class who inherit the properties of the base class is called derived class or sub class. 3
  • 4. TYPES OF INHERITANCE  Single inheritance(one super class) INHERITANCE  Multiple inheritance( several super classes)  Multilevel inheritance(  Hierarchical inheritance(one super class many sub class) 4
  • 5. DEFINE SUBCLASS class sub-classname extends super-classname { INHERITANCE variable declaration;// sub class variables method declaration;// sub class method } The keyword extends signifies that the properties of the super class are extended to the sub class. 5
  • 6. EXAMPLE OF SINGLE INHERITANCE class sum // Super class { int a=10; INHERITANCE int b=20; void show1() { System.out.println("value of a :- " +a); System.out.println("value of b :- " +b); } } class sum2 extends sum // base class { public static void main(String args[]) { sum2 obj = new sum2(); obj.show1(); } 6 }
  • 7. SUB CLASS CONSTRUCTOR  A subclass constructor is used to construct the INHERITANCE instance variable of both the subclass and super class.  The subclass constructor uses the keyword super to invoke the constructor method of the super class. 7
  • 8. CONDITION FOR SUPER KEYWORD  Super may only be used within a subclass constructor. INHERITANCE  The call to super class constructor must appear as the first statement with in the subclass constructor.  The parameter in the super call must match the order and type of the instance variable declared in the super class. 8
  • 9. USES OF SUPER KEYWORD  It calls the super class constructor. INHERITANCE SYNTAX:- super( parameter list);  Access the member of the super class. SYNTAX:- super. member variable; 9
  • 10. USING SUPER TO CALL SUPER CLASS CONSTRUCTOR class demo { int x,y; public demo() { x=10; INHERITANCE y=20;} public demo(int i,int j) { x=i; y=j; }} class demo1 extends demo{ int j; public demo1(){ super(10,15); j=30;} void show(){ System.out.println(x+ " " +y + " " +j);} public static void main(String args[]){ 10 demo1 d = new demo1(); d.show();}}
  • 11. MULTILEVEL INHERITANCE When a subclass derived from a super class and INHERITANCE a subclass is further derived from that subclass or when a subclass acts as a super class for some other subclass, it creates a multilevel hierarchy. 11
  • 12. METHOD OVERRIDING  In a class hierarchy , when a method in a sub class has the same name and type signature as a INHERITANCE method in its super class, then the method is said to override the method in the super class.  When an overridden method is called from within a sub class, it will always refer to the version of that method defined by the subclass. The version of the method defined by the super class will be hidden. 12
  • 13. EXAMPLE OF METHOD OVERRIDDING class sum // Super class { int a=10; INHERITANCE int b=20; void show() { System.out.println("value of a :- " +a); System.out.println("value of b :- " +b); }} class sum2 extends sum // base class { int i=30,j=40; void show() { System.out.println(i+ " " +j);// only this value will be print } public static void main(String args[]) { sum2 obj = new sum2(); 13 obj.show();}}
  • 14. ABSTRACT CLASS AND METHODS  An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be sub INHERITANCE classed. SYNTAX:- Abstract class name { }  An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this: SYNTAX:- abstract void method(Parameter); 14
  • 15. EXAMPLE OF ABSTRACT CLASS abstract class sum {abstract void show(); abstract void get(); } class sum1 extends sum INHERITANCE { void show() { System.out.println("HELLO"); } void get() { System.out.println("WELCOME"); } public static void main(String args[]) { sum1 obj = new sum1(); obj.show(); obj.get(); 15 } }
  • 16. FINAL KEYWORD Final keyword is used as follows:- INHERITANCE 1. To prevent Overriding . 2. To prevent inheritance 3. To create a named constant. 16
  • 17. INHERITANCE THANK YOU 17