SlideShare a Scribd company logo
1 of 25
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

What's hot

Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
backdoor
 

What's hot (20)

Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in Java
 
Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
 
itft-Inheritance in java
itft-Inheritance in javaitft-Inheritance in java
itft-Inheritance in java
 
Unit 3 Java
Unit 3 JavaUnit 3 Java
Unit 3 Java
 
Seminar on java
Seminar on javaSeminar on java
Seminar on java
 
javainheritance
javainheritancejavainheritance
javainheritance
 
Inheritance and Polymorphism Java
Inheritance and Polymorphism JavaInheritance and Polymorphism Java
Inheritance and Polymorphism Java
 
Java Programming - Inheritance
Java Programming - InheritanceJava Programming - Inheritance
Java Programming - Inheritance
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oops
 
Object Oriented Programming with C#
Object Oriented Programming with C#Object Oriented Programming with C#
Object Oriented Programming with C#
 
JAVA PROGRAMMING – Packages - Stream based I/O
JAVA PROGRAMMING – Packages - Stream based I/O JAVA PROGRAMMING – Packages - Stream based I/O
JAVA PROGRAMMING – Packages - Stream based I/O
 
Classes, objects, methods, constructors, this keyword in java
Classes, objects, methods, constructors, this keyword  in javaClasses, objects, methods, constructors, this keyword  in java
Classes, objects, methods, constructors, this keyword in java
 
Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiers
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
 

Viewers also liked

Actividad de laboratorio #2
Actividad de laboratorio #2Actividad de laboratorio #2
Actividad de laboratorio #2
Axel554
 
stephen motau Referee form 2016
stephen motau Referee form 2016stephen motau Referee form 2016
stephen motau Referee form 2016
Stephen M
 
Sunitha resume Apr 2016
Sunitha resume Apr 2016Sunitha resume Apr 2016
Sunitha resume Apr 2016
Sunitha Suni
 
COMMUNICATION PROCESS
COMMUNICATION PROCESSCOMMUNICATION PROCESS
COMMUNICATION PROCESS
harshalii
 

Viewers also liked (20)

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

Similar to inheritance

Java cçccfftshrssfuutrfuuggiuffus201-java.ppt
Java cçccfftshrssfuutrfuuggiuffus201-java.pptJava cçccfftshrssfuutrfuuggiuffus201-java.ppt
Java cçccfftshrssfuutrfuuggiuffus201-java.ppt
scsankalp03
 
cs213Lecture_1 java programming oopsss.pptx
cs213Lecture_1 java programming oopsss.pptxcs213Lecture_1 java programming oopsss.pptx
cs213Lecture_1 java programming oopsss.pptx
mshanajoel6
 

Similar to inheritance (20)

04 inheritance
04 inheritance04 inheritance
04 inheritance
 
Inheritance in java.ppt
Inheritance in java.pptInheritance in java.ppt
Inheritance in java.ppt
 
Java cçccfftshrssfuutrfuuggiuffus201-java.ppt
Java cçccfftshrssfuutrfuuggiuffus201-java.pptJava cçccfftshrssfuutrfuuggiuffus201-java.ppt
Java cçccfftshrssfuutrfuuggiuffus201-java.ppt
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference Card
 
Object Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptxObject Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptx
 
core_java.ppt
core_java.pptcore_java.ppt
core_java.ppt
 
Java and the JVM
Java and the JVMJava and the JVM
Java and the JVM
 
Core java
Core javaCore java
Core java
 
Metaprogramming ruby
Metaprogramming rubyMetaprogramming ruby
Metaprogramming ruby
 
Introduction to OOP concepts
Introduction to OOP conceptsIntroduction to OOP concepts
Introduction to OOP concepts
 
Java
JavaJava
Java
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Introducing object oriented programming (oop)
Introducing object oriented programming (oop)Introducing object oriented programming (oop)
Introducing object oriented programming (oop)
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
 
Cse java
Cse javaCse java
Cse java
 
DSL's with Groovy
DSL's with GroovyDSL's with Groovy
DSL's with Groovy
 
SKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPTSKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPT
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
cs213Lecture_1 java programming oopsss.pptx
cs213Lecture_1 java programming oopsss.pptxcs213Lecture_1 java programming oopsss.pptx
cs213Lecture_1 java programming oopsss.pptx
 

More from Mohit Patodia (13)

Services marketing mohit
Services marketing mohit Services marketing mohit
Services marketing mohit
 
C intro
C introC intro
C intro
 
Bluetooth
BluetoothBluetooth
Bluetooth
 
Basic computer maintenance
Basic computer maintenanceBasic computer maintenance
Basic computer maintenance
 
Android
AndroidAndroid
Android
 
Internet
InternetInternet
Internet
 
A quick introduction to c programming
A quick introduction to c programmingA quick introduction to c programming
A quick introduction to c programming
 
cloud computing
cloud computingcloud computing
cloud computing
 
the computer generations
the computer generationsthe computer generations
the computer generations
 
first-applet
first-appletfirst-applet
first-applet
 
intel ignite refresh_rmd
intel ignite refresh_rmdintel ignite refresh_rmd
intel ignite refresh_rmd
 
self motivate
self motivateself motivate
self motivate
 
role of it in supply chain mangment system
role of it in supply chain mangment systemrole of it in supply chain mangment system
role of it in supply chain mangment system
 

Recently uploaded

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Recently uploaded (20)

Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 

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