SlideShare a Scribd company logo
Java Inheritance




                   1
 On the surface, inheritance is a code re-use
 issue.
   we can extend code that is already written in a
   manageable manner.
 Inheritance is more
   it supports polymorphism at the language level




                                                      2
 Take an existing object type (collection of
 fields and methods) and extend it.
   create a special version of the code without re-
    writing any of the existing code (or even explicitly
    calling it!).
   End result is a more specific object type, called the
    sub-class / derived class / child class.
   The original code is called the superclass / parent
    class / base class.

                                                            3
   Employee: name, email, phone
     FulltimeEmployee: also has salary, office, benefits, …
      ▪ Manager: CompanyCar, can change salaries, rates contracts,
        offices, etc.
     Contractor: HourlyRate, ContractDuration, …


   A manager is a special kind of FullTimeEmployee,
    which is a special kind of Employee.


                                                                     4
 Create code that deals with general object
  types, without the need to know what
  specific type each object is.
 Generate a list of employee names:
   all objects derived from Employee have a name
    field since Employee has a name field
   no need to treat managers differently from
    anyone else.

                                                    5
 The real power comes with
  methods/behaviors.
 A better example:
  shape object types used by a drawing program.
  we want to be able to handle any kind of shape
   someone wants to code (in the future).
  we want to be able to write code now that can
   deal with shape objects (without knowing what
   they are!).

                                                    6
   Shape:
     color, layer    fields

     draw()          draw itself on the screen

     calcArea()      calculates it's own area.

     serialize()      generate a string that can be saved and
      later used to re-generate the object.


                                                                 7
Each could be a kind of shape
 Rectangle
              (could be specializations of the
              shape class).
 Triangle

              Each knows how to draw itself,
 Circle      etc.


              Could write code to have all
              shapes draw themselves, or save
              the whole collection to a file.

                                                 8
class classname {
   field declarations
   { initialization code }
   Constructors
   Methods
}




                             9
 Abstract modifier means that the class can be
 used as a superclass only.
   no objects of this class can be created.
   can have attributes, even code
    ▪ all are inherited
    ▪ methods can be overridden
 Used in inheritance hierarchies



                                                  10
   private/protected/public:
     protected means private to all but subclasses
     what if none of these specified?
   abstract: no implementation given, must be
    supplied by subclass.
     the class itself must also be declared abstract
   final: the method cannot be changed by a
    subclass (no alternative implementation can be
    provided by a subclass).


                                                        11
Interesting Method Modifiers
       (that have nothing to do with this slide set)

• native: the method is written in some local
  code (C/C++) - the implementation is not
  provided in Java (recall assembler routines
  linked with C)
• synchronized: only one thread at a time
  can call the method (later)



                                                       12
 When one object type depends on another,
 the relationship could be:
   is-a
   has-a
 Sometimes it's hard to define the
 relationship, but in general you use
 composition (aggregation) when the
 relationship is has-a


                                             13
 One class has instance variables that refer to
  object of another.
 Sometimes we have a collection of objects,
  the class just provides the glue.
   establishes the relationship between objects.
 There is nothing special happening here (as
 far as the compiler is concerned).


                                                    14
 One object type is defined as being a special
  version of some other object type.
   a specialization.
 The more general class is called:
   base class, super class, parent class.
 The more specific class is called:
   derived class, subclass, child class.



                                                  15
 A derived class object is an object of the base
 class.
   is-a, not has-a.
   all fields and methods are inherited.
 The derived class object also has some stuff
 that the base class does not provide (usually).



                                                    16
 Two kinds:
  implementation: the code that defines methods.
  interface: the method prototypes only.


 Other OOP languages often provide the
 same capabilities (but not as an explicit
 option).


                                                    17
 Derived class inherits the implementations of
  all methods from base class.
   can replace some with alternatives.
   new methods in derived class can access all non-
   private base class fields and methods.

 This is similar to (simple) C++ inheritance.



                                                       18
 Can use super() to access all (non-private)
 superclass methods.
   even those replaced with new versions in the
   derived class.
 Can use super() to call base class
 constructor.
   use arguments to specify desired constructor



                                                   19
 You can't extend more than one class!
   the derived class can't have more than one base
   class.

 You can do multiple inheritance with interface
 inheritance.



                                                      20
   A object of a derived class can be cast as an object
    of the base class.
     this is much of the power!
   When a method is called, the selection of which
    version of method is run is totally dynamic.
     overridden methods are dynamic.

Note: Selection of overloaded methods is done at compile time. There are
  some situations in which this can cause confusion.



                                                                           21
 Granddaddy of all Java classes.

 All methods defined in the class Object are
 available in every class.

 Any object can be cast as an Object.




                                                22
 An interface is a definition of method
  prototypes and possibly some constants
  (static final fields).
 An interface does not include the
  implementation of any methods, it just
  defines a set of methods that could be
  implemented.


                                           23
 A class can implement an interface, this
 means that it provides implementations for
 all the methods in the interface.

 Java classes can implement any number of
 interfaces (multiple interface inheritance).



                                                24
   Creation (definition) of interfaces can be done using
    inheritance:
     one interface can extend another.

   Sometimes interfaces are used just as labeling
    mechanisms:
     Look in the Java API documentation for interfaces like
      Cloneable.
   Example: BubbleSort w/ SortInterfaceDemo


                                                               25
ou
     k   y
  a n
Th

                26
?

    27

More Related Content

What's hot (20)

PPT
Java: Inheritance
Tareq Hasan
 
PPTX
Inheritance in Java
Tamanna Akter
 
PPS
Dacj 2-1 a
Niit Care
 
PPTX
Java Inheritance - sub class constructors - Method overriding
NithyaN19
 
PDF
itft-Inheritance in java
Atul Sehdev
 
PPT
Unit 3 Java
arnold 7490
 
PDF
Inheritance In Java
Arnab Bhaumik
 
PPTX
Multiple inheritance possible in Java
Kurapati Vishwak
 
PPT
Inheritance and Polymorphism
BG Java EE Course
 
PPTX
Java Inheritance | Java Course
RAKESH P
 
PPTX
Inheritance in Java
Elizabeth alexander
 
PPSX
Seminar on java
shathika
 
PPTX
Inheritance In Java
Darpan Chelani
 
PPT
Inheritance in java
Lovely Professional University
 
PPTX
Java inheritance
BHUVIJAYAVELU
 
PPT
Java Programming - Inheritance
Oum Saokosal
 
PPTX
Inheritance in oops
Hirra Sultan
 
PPTX
Inheritance and Polymorphism Java
M. Raihan
 
PPT
Inheritance C#
Raghuveer Guthikonda
 
Java: Inheritance
Tareq Hasan
 
Inheritance in Java
Tamanna Akter
 
Dacj 2-1 a
Niit Care
 
Java Inheritance - sub class constructors - Method overriding
NithyaN19
 
itft-Inheritance in java
Atul Sehdev
 
Unit 3 Java
arnold 7490
 
Inheritance In Java
Arnab Bhaumik
 
Multiple inheritance possible in Java
Kurapati Vishwak
 
Inheritance and Polymorphism
BG Java EE Course
 
Java Inheritance | Java Course
RAKESH P
 
Inheritance in Java
Elizabeth alexander
 
Seminar on java
shathika
 
Inheritance In Java
Darpan Chelani
 
Inheritance in java
Lovely Professional University
 
Java inheritance
BHUVIJAYAVELU
 
Java Programming - Inheritance
Oum Saokosal
 
Inheritance in oops
Hirra Sultan
 
Inheritance and Polymorphism Java
M. Raihan
 
Inheritance C#
Raghuveer Guthikonda
 

Similar to 04 inheritance (20)

PPT
Inheritance in java.ppt
SeethaDinesh
 
PPT
04 inheritance
Sumit Srivastava
 
PPT
04_inheritance power point presentation.
ravishankarbhaganaga1
 
PPT
Java inheritance concept, interface, objects, extends
HelariaXavier
 
PPTX
01. design pattern
MD Sayem Ahmed
 
PDF
javainterface
Arjun Shanka
 
PDF
JAVA-PPT'S.pdf
AnmolVerma363503
 
PDF
L5
lksoo
 
PPTX
Object Oriented Principles
Emprovise
 
PPTX
Java session2
Rajeev Kumar
 
PPT
06 abstract-classes
Anup Burange
 
PPTX
JAVA-PPT'S-complete-chrome.pptx
KunalYadav65140
 
PPTX
JAVA-PPT'S.pptx
RaazIndia
 
PPT
Polymorphism
Kumar
 
PDF
Core java complete notes - Contact at +91-814-614-5674
Lokesh Kakkar Mobile No. 814-614-5674
 
PDF
Core java complete notes - PAID call at +91-814-614-5674
WebKrit Infocom
 
PPTX
Lecture 12
talha ijaz
 
PPTX
java part 1 computer science.pptx
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
PPT
06 abstract-classes
Abhishek Khune
 
PPTX
Object oriented programming 3 object oriented concepts
Vaibhav Khanna
 
Inheritance in java.ppt
SeethaDinesh
 
04 inheritance
Sumit Srivastava
 
04_inheritance power point presentation.
ravishankarbhaganaga1
 
Java inheritance concept, interface, objects, extends
HelariaXavier
 
01. design pattern
MD Sayem Ahmed
 
javainterface
Arjun Shanka
 
JAVA-PPT'S.pdf
AnmolVerma363503
 
L5
lksoo
 
Object Oriented Principles
Emprovise
 
Java session2
Rajeev Kumar
 
06 abstract-classes
Anup Burange
 
JAVA-PPT'S-complete-chrome.pptx
KunalYadav65140
 
JAVA-PPT'S.pptx
RaazIndia
 
Polymorphism
Kumar
 
Core java complete notes - Contact at +91-814-614-5674
Lokesh Kakkar Mobile No. 814-614-5674
 
Core java complete notes - PAID call at +91-814-614-5674
WebKrit Infocom
 
Lecture 12
talha ijaz
 
java part 1 computer science.pptx
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
06 abstract-classes
Abhishek Khune
 
Object oriented programming 3 object oriented concepts
Vaibhav Khanna
 
Ad

Recently uploaded (20)

PPTX
The Future of Artificial Intelligence Opportunities and Risks Ahead
vaghelajayendra784
 
PPTX
Top 10 AI Tools, Like ChatGPT. You Must Learn In 2025
Digilearnings
 
DOCX
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
PPTX
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
PDF
TOP 10 AI TOOLS YOU MUST LEARN TO SURVIVE IN 2025 AND ABOVE
digilearnings.com
 
PPTX
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
PPTX
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
DOCX
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
PPTX
TOP 10 AI TOOLS YOU MUST LEARN TO SURVIVE IN 2025 AND ABOVE
digilearnings.com
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
PPTX
K-Circle-Weekly-Quiz12121212-May2025.pptx
Pankaj Rodey
 
PPTX
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
PPTX
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
PDF
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
PPTX
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Electrophysiology_of_Heart. Electrophysiology studies in Cardiovascular syste...
Rajshri Ghogare
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
PDF
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
PPTX
Rules and Regulations of Madhya Pradesh Library Part-I
SantoshKumarKori2
 
PPT
DRUGS USED IN THERAPY OF SHOCK, Shock Therapy, Treatment or management of shock
Rajshri Ghogare
 
The Future of Artificial Intelligence Opportunities and Risks Ahead
vaghelajayendra784
 
Top 10 AI Tools, Like ChatGPT. You Must Learn In 2025
Digilearnings
 
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
TOP 10 AI TOOLS YOU MUST LEARN TO SURVIVE IN 2025 AND ABOVE
digilearnings.com
 
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
TOP 10 AI TOOLS YOU MUST LEARN TO SURVIVE IN 2025 AND ABOVE
digilearnings.com
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
K-Circle-Weekly-Quiz12121212-May2025.pptx
Pankaj Rodey
 
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
Electrophysiology_of_Heart. Electrophysiology studies in Cardiovascular syste...
Rajshri Ghogare
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
Rules and Regulations of Madhya Pradesh Library Part-I
SantoshKumarKori2
 
DRUGS USED IN THERAPY OF SHOCK, Shock Therapy, Treatment or management of shock
Rajshri Ghogare
 
Ad

04 inheritance

  • 2.  On the surface, inheritance is a code re-use issue.  we can extend code that is already written in a manageable manner.  Inheritance is more  it supports polymorphism at the language level 2
  • 3.  Take an existing object type (collection of fields and methods) and extend it.  create a special version of the code without re- writing any of the existing code (or even explicitly calling it!).  End result is a more specific object type, called the sub-class / derived class / child class.  The original code is called the superclass / parent class / base class. 3
  • 4. Employee: name, email, phone  FulltimeEmployee: also has salary, office, benefits, … ▪ Manager: CompanyCar, can change salaries, rates contracts, offices, etc.  Contractor: HourlyRate, ContractDuration, …  A manager is a special kind of FullTimeEmployee, which is a special kind of Employee. 4
  • 5.  Create code that deals with general object types, without the need to know what specific type each object is.  Generate a list of employee names:  all objects derived from Employee have a name field since Employee has a name field  no need to treat managers differently from anyone else. 5
  • 6.  The real power comes with methods/behaviors.  A better example:  shape object types used by a drawing program.  we want to be able to handle any kind of shape someone wants to code (in the future).  we want to be able to write code now that can deal with shape objects (without knowing what they are!). 6
  • 7. Shape:  color, layer fields  draw() draw itself on the screen  calcArea() calculates it's own area.  serialize() generate a string that can be saved and later used to re-generate the object. 7
  • 8. Each could be a kind of shape  Rectangle (could be specializations of the shape class).  Triangle Each knows how to draw itself,  Circle etc. Could write code to have all shapes draw themselves, or save the whole collection to a file. 8
  • 9. class classname { field declarations { initialization code } Constructors Methods } 9
  • 10.  Abstract modifier means that the class can be used as a superclass only.  no objects of this class can be created.  can have attributes, even code ▪ all are inherited ▪ methods can be overridden  Used in inheritance hierarchies 10
  • 11. private/protected/public:  protected means private to all but subclasses  what if none of these specified?  abstract: no implementation given, must be supplied by subclass.  the class itself must also be declared abstract  final: the method cannot be changed by a subclass (no alternative implementation can be provided by a subclass). 11
  • 12. Interesting Method Modifiers (that have nothing to do with this slide set) • native: the method is written in some local code (C/C++) - the implementation is not provided in Java (recall assembler routines linked with C) • synchronized: only one thread at a time can call the method (later) 12
  • 13.  When one object type depends on another, the relationship could be:  is-a  has-a  Sometimes it's hard to define the relationship, but in general you use composition (aggregation) when the relationship is has-a 13
  • 14.  One class has instance variables that refer to object of another.  Sometimes we have a collection of objects, the class just provides the glue.  establishes the relationship between objects.  There is nothing special happening here (as far as the compiler is concerned). 14
  • 15.  One object type is defined as being a special version of some other object type.  a specialization.  The more general class is called:  base class, super class, parent class.  The more specific class is called:  derived class, subclass, child class. 15
  • 16.  A derived class object is an object of the base class.  is-a, not has-a.  all fields and methods are inherited.  The derived class object also has some stuff that the base class does not provide (usually). 16
  • 17.  Two kinds:  implementation: the code that defines methods.  interface: the method prototypes only.  Other OOP languages often provide the same capabilities (but not as an explicit option). 17
  • 18.  Derived class inherits the implementations of all methods from base class.  can replace some with alternatives.  new methods in derived class can access all non- private base class fields and methods.  This is similar to (simple) C++ inheritance. 18
  • 19.  Can use super() to access all (non-private) superclass methods.  even those replaced with new versions in the derived class.  Can use super() to call base class constructor.  use arguments to specify desired constructor 19
  • 20.  You can't extend more than one class!  the derived class can't have more than one base class.  You can do multiple inheritance with interface inheritance. 20
  • 21. A object of a derived class can be cast as an object of the base class.  this is much of the power!  When a method is called, the selection of which version of method is run is totally dynamic.  overridden methods are dynamic. Note: Selection of overloaded methods is done at compile time. There are some situations in which this can cause confusion. 21
  • 22.  Granddaddy of all Java classes.  All methods defined in the class Object are available in every class.  Any object can be cast as an Object. 22
  • 23.  An interface is a definition of method prototypes and possibly some constants (static final fields).  An interface does not include the implementation of any methods, it just defines a set of methods that could be implemented. 23
  • 24.  A class can implement an interface, this means that it provides implementations for all the methods in the interface.  Java classes can implement any number of interfaces (multiple interface inheritance). 24
  • 25. Creation (definition) of interfaces can be done using inheritance:  one interface can extend another.  Sometimes interfaces are used just as labeling mechanisms:  Look in the Java API documentation for interfaces like Cloneable.  Example: BubbleSort w/ SortInterfaceDemo 25
  • 26. ou k y a n Th 26
  • 27. ? 27