© Prognoz Technologies Pvt. Ltd Inheritance
© Prognoz Technologies Pvt. Ltd OOP Encapsulation Inheritance Polymorphism Abstraction 4 pillars of OOPS
© Prognoz Technologies Pvt. Ltd Inheritance  Inheritance is a fundamental Object Oriented concept  A class can be defined as a "subclass" of another class.  The subclass inherits all data attributes of its superclass  The subclass inherits all methods of its superclass  The subclass inherits all associations of its superclass
© Prognoz Technologies Pvt. Ltd Inheritance(Continue…)  When we talk about inheritance, the most commonly used keyword would be extends and implements. Class Class extends Interface Class implements Interface Interface extends
© Prognoz Technologies Pvt. Ltd Inheritance(Continue…)  These words would determine whether one object IS-A type of another. IS-A Relationship:  IS-A is a way of saying : This object is a type of that object. Surgeon IS-A Doctor
© Prognoz Technologies Pvt. Ltd Inheritance(Continue…)  Now, if we consider the IS-A relationship: 1. Mammal IS-AAnimal 2. Reptile IS-AAnimal 3. Dog IS-A Mammal Hence : Dog IS-AAnimal as well
© Prognoz Technologies Pvt. Ltd Inheritance(Continue…)  Reusability is achieved by INHERITANCE  Java classes Can be Reused by extending a class. Extending an existing class is nothing but reusing properties of the existing classes.  The class whose properties are extended is known as super or base or parent class.  The class which extends the properties of super class is known as sub or derived or child class  A class can either extends another class or can implement an interface
© Prognoz Technologies Pvt. Ltd Example
© Prognoz Technologies Pvt. Ltd Why use inheritance in java  For Method Overriding (so runtime polymorphism can be achieved).  For Code Reusability.
© Prognoz Technologies Pvt. Ltd Types of inheritance in java  On the basis of class, there can be three types of inheritance in java:  Single, multilevel and hierarchical.  In java programming, multiple and hybrid inheritance is supported through interface only.
© Prognoz Technologies Pvt. Ltd Various Forms of Inheritance A B Single Inheritance Hierarchical Inheritance X A B C Multilevel Inheritance A B C A B C Multiple Inheritance NOT SUPPORTED BY JAVA A B C A C Hybrid Inheritance
© Prognoz Technologies Pvt. Ltd 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.
© Prognoz Technologies Pvt. Ltd Thank you!! 13

Interesting Concept of Object Oriented Programming

  • 1.
    © Prognoz TechnologiesPvt. Ltd Inheritance
  • 2.
    © Prognoz TechnologiesPvt. Ltd OOP Encapsulation Inheritance Polymorphism Abstraction 4 pillars of OOPS
  • 3.
    © Prognoz TechnologiesPvt. Ltd Inheritance  Inheritance is a fundamental Object Oriented concept  A class can be defined as a "subclass" of another class.  The subclass inherits all data attributes of its superclass  The subclass inherits all methods of its superclass  The subclass inherits all associations of its superclass
  • 4.
    © Prognoz TechnologiesPvt. Ltd Inheritance(Continue…)  When we talk about inheritance, the most commonly used keyword would be extends and implements. Class Class extends Interface Class implements Interface Interface extends
  • 5.
    © Prognoz TechnologiesPvt. Ltd Inheritance(Continue…)  These words would determine whether one object IS-A type of another. IS-A Relationship:  IS-A is a way of saying : This object is a type of that object. Surgeon IS-A Doctor
  • 6.
    © Prognoz TechnologiesPvt. Ltd Inheritance(Continue…)  Now, if we consider the IS-A relationship: 1. Mammal IS-AAnimal 2. Reptile IS-AAnimal 3. Dog IS-A Mammal Hence : Dog IS-AAnimal as well
  • 7.
    © Prognoz TechnologiesPvt. Ltd Inheritance(Continue…)  Reusability is achieved by INHERITANCE  Java classes Can be Reused by extending a class. Extending an existing class is nothing but reusing properties of the existing classes.  The class whose properties are extended is known as super or base or parent class.  The class which extends the properties of super class is known as sub or derived or child class  A class can either extends another class or can implement an interface
  • 8.
    © Prognoz TechnologiesPvt. Ltd Example
  • 9.
    © Prognoz TechnologiesPvt. Ltd Why use inheritance in java  For Method Overriding (so runtime polymorphism can be achieved).  For Code Reusability.
  • 10.
    © Prognoz TechnologiesPvt. Ltd Types of inheritance in java  On the basis of class, there can be three types of inheritance in java:  Single, multilevel and hierarchical.  In java programming, multiple and hybrid inheritance is supported through interface only.
  • 11.
    © Prognoz TechnologiesPvt. Ltd Various Forms of Inheritance A B Single Inheritance Hierarchical Inheritance X A B C Multilevel Inheritance A B C A B C Multiple Inheritance NOT SUPPORTED BY JAVA A B C A C Hybrid Inheritance
  • 12.
    © Prognoz TechnologiesPvt. Ltd 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.
  • 13.
    © Prognoz TechnologiesPvt. Ltd Thank you!! 13