SlideShare a Scribd company logo
1
Java Inheritance
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

Similar to Java inheritance concept, interface, objects, extends (20)

PPT
core_java.ppt
YashikaDave
 
PPTX
Quick Interview Preparation for C# All Concepts
Karmanjay Verma
 
PPTX
Java
nirbhayverma8
 
PPT
Core java
Savita Rawat
 
PPT
Introducing object oriented programming (oop)
Hemlathadhevi Annadhurai
 
PPTX
Introduction to OOP concepts
Ahmed Farag
 
PPTX
Introduction to Scala
Rahul Jain
 
PDF
Metaprogramming ruby
GeekNightHyderabad
 
PPTX
ppt_on_java.pptx
MAYANKKUMAR492040
 
PPTX
Object oriented programming
MH Abid
 
PPTX
Introduction to oop and java fundamentals
AnsgarMary
 
PPTX
cs213Lecture_1 java programming oopsss.pptx
mshanajoel6
 
PPTX
Pi j3.2 polymorphism
mcollison
 
PPTX
Java
Raghu nath
 
PDF
JAVA Class Presentation.pdf Vsjsjsnheheh
AnushreeP4
 
PDF
First fare 2011 frc-java-introduction
Oregon FIRST Robotics
 
PPTX
Java 102 intro to object-oriented programming in java
agorolabs
 
PPTX
SKILLWISE - OOPS CONCEPT
Skillwise Group
 
PPTX
PPT Lecture-1.4.pptx
HimanshuPandey957216
 
core_java.ppt
YashikaDave
 
Quick Interview Preparation for C# All Concepts
Karmanjay Verma
 
Core java
Savita Rawat
 
Introducing object oriented programming (oop)
Hemlathadhevi Annadhurai
 
Introduction to OOP concepts
Ahmed Farag
 
Introduction to Scala
Rahul Jain
 
Metaprogramming ruby
GeekNightHyderabad
 
ppt_on_java.pptx
MAYANKKUMAR492040
 
Object oriented programming
MH Abid
 
Introduction to oop and java fundamentals
AnsgarMary
 
cs213Lecture_1 java programming oopsss.pptx
mshanajoel6
 
Pi j3.2 polymorphism
mcollison
 
JAVA Class Presentation.pdf Vsjsjsnheheh
AnushreeP4
 
First fare 2011 frc-java-introduction
Oregon FIRST Robotics
 
Java 102 intro to object-oriented programming in java
agorolabs
 
SKILLWISE - OOPS CONCEPT
Skillwise Group
 
PPT Lecture-1.4.pptx
HimanshuPandey957216
 

Recently uploaded (20)

PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
Ad

Java inheritance concept, interface, objects, extends

  • 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