Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
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

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