1
Java Inheritance
By :- Mohit patodiya
2
Inheritance
• 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
3
Inheritance
• 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.
4
Inheritance Example
• 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.
5
Polymorphism
• 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.
6
Method Polymorphism
• 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!).
7
Shapes
• 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.
8
Kinds of Shapes
• Rectangle
• Triangle
• Circle
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 definition
class classname {
field declarations
{ initialization code }
Constructors
Methods
}
10
Abstract Class modifier
• 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
11
Interesting Method Modifiers
• 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).
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
Inheritance vs. Composition
• 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
14
Composition
• 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).
15
Inheritance
• 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.
16
Inheritance
• 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).
17
Java Inheritance
• 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).
18
Implementation Inheritance
• 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.
19
Accessing superclass methods
from derived class.
• 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
20
Single inheritance only
(implementation inheritance).
• 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.
21
Casting Objects
• 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.
22
The class Object
• 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.
23
Interfaces
• 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.
24
interface implementation
• 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).
25
Interfaces can be extended
• 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

More Related Content

PPS
Dacj 2-1 a
PDF
java-06inheritance
PPT
04 inheritance
PPT
Java inheritance
PPTX
Java Inheritance - sub class constructors - Method overriding
PPT
Java: Inheritance
PPTX
Inheritance and its types In Java
PPT
Inheritance and Polymorphism
Dacj 2-1 a
java-06inheritance
04 inheritance
Java inheritance
Java Inheritance - sub class constructors - Method overriding
Java: Inheritance
Inheritance and its types In Java
Inheritance and Polymorphism

What's hot (20)

PPTX
Inheritance in JAVA PPT
PPTX
Multiple inheritance possible in Java
PPTX
Inheritance in Java
PDF
itft-Inheritance in java
PPT
Unit 3 Java
PPSX
Seminar on java
PDF
javainheritance
PPTX
Inheritance and Polymorphism Java
PPT
Java Programming - Inheritance
PPTX
oops concept in java | object oriented programming in java
PDF
Inheritance In Java
PPTX
Java inheritance
PPTX
Inheritance in oops
PPTX
Object Oriented Programming with C#
PDF
JAVA PROGRAMMING – Packages - Stream based I/O
PDF
Classes, objects, methods, constructors, this keyword in java
PPTX
Inheritance in Java
PPT
Java access modifiers
PPT
Object Oriented Programming with Java
Inheritance in JAVA PPT
Multiple inheritance possible in Java
Inheritance in Java
itft-Inheritance in java
Unit 3 Java
Seminar on java
javainheritance
Inheritance and Polymorphism Java
Java Programming - Inheritance
oops concept in java | object oriented programming in java
Inheritance In Java
Java inheritance
Inheritance in oops
Object Oriented Programming with C#
JAVA PROGRAMMING – Packages - Stream based I/O
Classes, objects, methods, constructors, this keyword in java
Inheritance in Java
Java access modifiers
Object Oriented Programming with Java
Ad

Viewers also liked (20)

PPTX
Six week industrial training presentation BY GUPTA TARUN
PPTX
Procedimiento EXENCION MÓDULO PRÁCTICAS
DOCX
Little red ridding
PPTX
Actividad de laboratorio #2
PDF
Programación Feria del Banano - Apartadó, Antioquia
PPTX
Vygotskys socio cultural theory
PDF
0137_Whisky_vis1
PDF
Screenstretch
PDF
stephen motau Referee form 2016
PDF
Caterware case-study-cutty-sark
PDF
Intro to .NET and Core C#
PPT
Automated code review process
PPT
Principles of Communication
DOCX
Sunitha resume Apr 2016
PPT
COMMUNICATION PROCESS
PPS
Microsoft Access
PDF
akamai's [state of internet] / security
PDF
Cisco Annual Security Report
PDF
C# conventions & good practices
PPTX
تحديد مدى نجاح عملية ومنتج التصميم التعليمي
Six week industrial training presentation BY GUPTA TARUN
Procedimiento EXENCION MÓDULO PRÁCTICAS
Little red ridding
Actividad de laboratorio #2
Programación Feria del Banano - Apartadó, Antioquia
Vygotskys socio cultural theory
0137_Whisky_vis1
Screenstretch
stephen motau Referee form 2016
Caterware case-study-cutty-sark
Intro to .NET and Core C#
Automated code review process
Principles of Communication
Sunitha resume Apr 2016
COMMUNICATION PROCESS
Microsoft Access
akamai's [state of internet] / security
Cisco Annual Security Report
C# conventions & good practices
تحديد مدى نجاح عملية ومنتج التصميم التعليمي
Ad

Similar to inheritance (20)

PPT
04_inheritance power point presentation.
PPT
Java inheritance concept, interface, objects, extends
PPT
04 inheritance
PPT
Inheritance in java.ppt
PDF
Core java complete notes - Contact at +91-814-614-5674
PDF
Core java complete notes - PAID call at +91-814-614-5674
PPT
inheritance of java...basics of java in ppt
PPTX
java part 1 computer science.pptx
PPT
Inheritance and its necessity in java.ppt
PPTX
ITTutor Advanced Java (1).pptx
PPTX
Android Training (Java Review)
PPTX
Object Oriented Programming Tutorial.pptx
PDF
JAVA-PPT'S.pdf
PPTX
Java Programming - UNIT - 1, Basics OOPS, Differences
PPTX
OOPS (Object Oriented Programming System) CONCEPTS
PDF
PPT
java training faridabad
PPT
10 - Encapsulation(object oriented programming)- java . ppt
PPTX
JAVA-PPT'S-complete-chrome.pptx
PPTX
JAVA-PPT'S.pptx
04_inheritance power point presentation.
Java inheritance concept, interface, objects, extends
04 inheritance
Inheritance in java.ppt
Core java complete notes - Contact at +91-814-614-5674
Core java complete notes - PAID call at +91-814-614-5674
inheritance of java...basics of java in ppt
java part 1 computer science.pptx
Inheritance and its necessity in java.ppt
ITTutor Advanced Java (1).pptx
Android Training (Java Review)
Object Oriented Programming Tutorial.pptx
JAVA-PPT'S.pdf
Java Programming - UNIT - 1, Basics OOPS, Differences
OOPS (Object Oriented Programming System) CONCEPTS
java training faridabad
10 - Encapsulation(object oriented programming)- java . ppt
JAVA-PPT'S-complete-chrome.pptx
JAVA-PPT'S.pptx

More from Mohit Patodia (13)

PPTX
Services marketing mohit
PPT
C intro
PPT
Bluetooth
PPT
Basic computer maintenance
PPT
Android
PPTX
Internet
PPT
A quick introduction to c programming
PPT
cloud computing
PPT
the computer generations
PPT
first-applet
PPT
intel ignite refresh_rmd
PPT
self motivate
PPTX
role of it in supply chain mangment system
Services marketing mohit
C intro
Bluetooth
Basic computer maintenance
Android
Internet
A quick introduction to c programming
cloud computing
the computer generations
first-applet
intel ignite refresh_rmd
self motivate
role of it in supply chain mangment system

Recently uploaded (20)

PDF
HVAC Specification 2024 according to central public works department
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
PPTX
Computer Architecture Input Output Memory.pptx
PDF
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
PDF
AI-driven educational solutions for real-life interventions in the Philippine...
PDF
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
PDF
Trump Administration's workforce development strategy
PDF
Hazard Identification & Risk Assessment .pdf
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
What if we spent less time fighting change, and more time building what’s rig...
PPTX
Introduction to pro and eukaryotes and differences.pptx
PPTX
B.Sc. DS Unit 2 Software Engineering.pptx
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PDF
Uderstanding digital marketing and marketing stratergie for engaging the digi...
PPTX
TNA_Presentation-1-Final(SAVE)) (1).pptx
PDF
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
PDF
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
HVAC Specification 2024 according to central public works department
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
Computer Architecture Input Output Memory.pptx
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
AI-driven educational solutions for real-life interventions in the Philippine...
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
Trump Administration's workforce development strategy
Hazard Identification & Risk Assessment .pdf
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
What if we spent less time fighting change, and more time building what’s rig...
Introduction to pro and eukaryotes and differences.pptx
B.Sc. DS Unit 2 Software Engineering.pptx
Chinmaya Tiranga quiz Grand Finale.pdf
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
Paper A Mock Exam 9_ Attempt review.pdf.
Uderstanding digital marketing and marketing stratergie for engaging the digi...
TNA_Presentation-1-Final(SAVE)) (1).pptx
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
MBA _Common_ 2nd year Syllabus _2021-22_.pdf

inheritance

  • 1. 1 Java Inheritance By :- Mohit patodiya
  • 2. 2 Inheritance • 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
  • 3. 3 Inheritance • 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.
  • 4. 4 Inheritance Example • 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.
  • 5. 5 Polymorphism • 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.
  • 6. 6 Method Polymorphism • 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!).
  • 7. 7 Shapes • 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.
  • 8. 8 Kinds of Shapes • Rectangle • Triangle • Circle 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. 9 class definition class classname { field declarations { initialization code } Constructors Methods }
  • 10. 10 Abstract Class modifier • 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
  • 11. 11 Interesting Method Modifiers • 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).
  • 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. 13 Inheritance vs. Composition • 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
  • 14. 14 Composition • 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).
  • 15. 15 Inheritance • 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.
  • 16. 16 Inheritance • 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).
  • 17. 17 Java Inheritance • 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).
  • 18. 18 Implementation Inheritance • 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.
  • 19. 19 Accessing superclass methods from derived class. • 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
  • 20. 20 Single inheritance only (implementation inheritance). • 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.
  • 21. 21 Casting Objects • 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.
  • 22. 22 The class Object • 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.
  • 23. 23 Interfaces • 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.
  • 24. 24 interface implementation • 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).
  • 25. 25 Interfaces can be extended • 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