SlideShare a Scribd company logo
1
D.SEETHALAKSHMI
Assistant Professor
Department of Computer Applications
Bon Secours College for Women
 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
 Rectangle
 Triangle
 Circle
8
Each could be a kind of shape
(could be specializations of the
shape class).
Each knows how to draw itself,
etc.
Could write code to have all
shapes draw themselves, or save
the whole collection to a file.
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
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)
 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

More Related Content

Similar to Inheritance in java.ppt (20)

PDF
Unit 2
Amar Jukuntla
 
PDF
L5
lksoo
 
PPTX
Object Oriented Principles
Emprovise
 
PPT
Polymorphism
Kumar
 
PPTX
Java session2
Rajeev Kumar
 
PPTX
java part 1 computer science.pptx
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
PPTX
4-OOPS.pptx
SatyamMishra237306
 
DOC
116824015 java-j2 ee
homeworkping9
 
PPT
06 abstract-classes
Anup Burange
 
PDF
JAVA-PPT'S.pdf
AnmolVerma363503
 
PPT
Chapter 5 (OOP Principles).ppt
henokmetaferia1
 
PPT
Java inheritance
Arati Gadgil
 
PPT
Java Programming - Inheritance
Oum Saokosal
 
PPTX
OOP Presentation.pptx
DurgaPrasadVasantati
 
PPTX
OOP Presentation.pptx
DurgaPrasadVasantati
 
PPT
06 abstract-classes
Abhishek Khune
 
PPT
Chapter 8 Inheritance
OUM SAOKOSAL
 
PDF
Java OOP Programming language (Part 5) - Inheritance
OUM SAOKOSAL
 
PPT
Object and class
mohit tripathi
 
L5
lksoo
 
Object Oriented Principles
Emprovise
 
Polymorphism
Kumar
 
Java session2
Rajeev Kumar
 
java part 1 computer science.pptx
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
4-OOPS.pptx
SatyamMishra237306
 
116824015 java-j2 ee
homeworkping9
 
06 abstract-classes
Anup Burange
 
JAVA-PPT'S.pdf
AnmolVerma363503
 
Chapter 5 (OOP Principles).ppt
henokmetaferia1
 
Java inheritance
Arati Gadgil
 
Java Programming - Inheritance
Oum Saokosal
 
OOP Presentation.pptx
DurgaPrasadVasantati
 
OOP Presentation.pptx
DurgaPrasadVasantati
 
06 abstract-classes
Abhishek Khune
 
Chapter 8 Inheritance
OUM SAOKOSAL
 
Java OOP Programming language (Part 5) - Inheritance
OUM SAOKOSAL
 
Object and class
mohit tripathi
 

More from SeethaDinesh (20)

PPTX
Input Devices.pptx
SeethaDinesh
 
PPT
Generations of Computers.ppt
SeethaDinesh
 
PPT
Arrays in JAVA.ppt
SeethaDinesh
 
PPTX
PROGRAMMING IN JAVA unit 1.pptx
SeethaDinesh
 
PPTX
Cloud Computing Basics.pptx
SeethaDinesh
 
PPT
unit 5 stack & queue.ppt
SeethaDinesh
 
PPT
Greedy_Backtracking graph coloring.ppt
SeethaDinesh
 
PPT
Structure and Union.ppt
SeethaDinesh
 
DOCX
Shortest Path Problem.docx
SeethaDinesh
 
PPT
BINARY TREE REPRESENTATION.ppt
SeethaDinesh
 
PPT
NME UNIT II.ppt
SeethaDinesh
 
PDF
DS UNIT 1.pdf
SeethaDinesh
 
PPT
Basics of C.ppt
SeethaDinesh
 
PPTX
chapter 1.pptx
SeethaDinesh
 
PPTX
DW unit 3.pptx
SeethaDinesh
 
PDF
DW unit 2.pdf
SeethaDinesh
 
PDF
DW Unit 1.pdf
SeethaDinesh
 
PPTX
NME WPI UNIt 3.pptx
SeethaDinesh
 
PDF
NME UNIT I & II MATERIAL.pdf
SeethaDinesh
 
PDF
graphtraversals.pdf
SeethaDinesh
 
Input Devices.pptx
SeethaDinesh
 
Generations of Computers.ppt
SeethaDinesh
 
Arrays in JAVA.ppt
SeethaDinesh
 
PROGRAMMING IN JAVA unit 1.pptx
SeethaDinesh
 
Cloud Computing Basics.pptx
SeethaDinesh
 
unit 5 stack & queue.ppt
SeethaDinesh
 
Greedy_Backtracking graph coloring.ppt
SeethaDinesh
 
Structure and Union.ppt
SeethaDinesh
 
Shortest Path Problem.docx
SeethaDinesh
 
BINARY TREE REPRESENTATION.ppt
SeethaDinesh
 
NME UNIT II.ppt
SeethaDinesh
 
DS UNIT 1.pdf
SeethaDinesh
 
Basics of C.ppt
SeethaDinesh
 
chapter 1.pptx
SeethaDinesh
 
DW unit 3.pptx
SeethaDinesh
 
DW unit 2.pdf
SeethaDinesh
 
DW Unit 1.pdf
SeethaDinesh
 
NME WPI UNIt 3.pptx
SeethaDinesh
 
NME UNIT I & II MATERIAL.pdf
SeethaDinesh
 
graphtraversals.pdf
SeethaDinesh
 
Ad

Recently uploaded (20)

PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PDF
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
Dimensions of Societal Planning in Commonism
StefanMz
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
Ad

Inheritance in java.ppt

  • 1. 1 D.SEETHALAKSHMI Assistant Professor Department of Computer Applications Bon Secours College for Women
  • 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.  Rectangle  Triangle  Circle 8 Each could be a kind of shape (could be specializations of the shape class). Each knows how to draw itself, etc. Could write code to have all shapes draw themselves, or save the whole collection to a file.
  • 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. 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)
  • 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