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

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

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
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
inheritance c++
inheritance c++inheritance c++
inheritance c++
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Abstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaAbstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core Java
 
Java interface
Java interfaceJava interface
Java interface
 
Javapolymorphism
JavapolymorphismJavapolymorphism
Javapolymorphism
 
Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
 
Interfaces and abstract classes
Interfaces and abstract classesInterfaces and abstract classes
Interfaces and abstract classes
 
inheritance
inheritanceinheritance
inheritance
 
Polymorphism in Java
Polymorphism in JavaPolymorphism in Java
Polymorphism in Java
 
Java class,object,method introduction
Java class,object,method introductionJava class,object,method introduction
Java class,object,method introduction
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Generics in java
Generics in javaGenerics in java
Generics in java
 

Destacado (9)

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

Unit3 java
Unit3 javaUnit3 java
Unit3 java
mrecedu
 
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
RudranilDas11
 

Similar a Inheritance (20)

Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7
 
Ppt on this and super keyword
Ppt on this and super keywordPpt on this and super keyword
Ppt on this and super keyword
 
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 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
 

Último

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
AnaAcapella
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Último (20)

How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
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.
 
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
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
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
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
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
 
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
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.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