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

chapter_one_Introduction_to_Object_Oriented_Programming_OOP.pptx
chapter_one_Introduction_to_Object_Oriented_Programming_OOP.pptxchapter_one_Introduction_to_Object_Oriented_Programming_OOP.pptx
chapter_one_Introduction_to_Object_Oriented_Programming_OOP.pptx
FiromsaDine
 
Java
JavaJava
Core java
Core javaCore java
Core java
Savita Rawat
 
Introducing object oriented programming (oop)
Introducing object oriented programming (oop)Introducing object oriented programming (oop)
Introducing object oriented programming (oop)
Hemlathadhevi Annadhurai
 
Introduction to OOP concepts
Introduction to OOP conceptsIntroduction to OOP concepts
Introduction to OOP concepts
Ahmed Farag
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
Rahul Jain
 
Metaprogramming ruby
Metaprogramming rubyMetaprogramming ruby
Metaprogramming ruby
GeekNightHyderabad
 
ppt_on_java.pptx
ppt_on_java.pptxppt_on_java.pptx
ppt_on_java.pptx
MAYANKKUMAR492040
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
MH Abid
 
Introduction to oop and java fundamentals
Introduction to oop and java fundamentalsIntroduction to oop and java fundamentals
Introduction to oop and java fundamentals
AnsgarMary
 
Cse java
Cse javaCse java
cs213Lecture_1 java programming oopsss.pptx
cs213Lecture_1 java programming oopsss.pptxcs213Lecture_1 java programming oopsss.pptx
cs213Lecture_1 java programming oopsss.pptx
mshanajoel6
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
mcollison
 
Java
JavaJava
First fare 2011 frc-java-introduction
First fare 2011 frc-java-introductionFirst fare 2011 frc-java-introduction
First fare 2011 frc-java-introduction
Oregon FIRST Robotics
 
Java 102 intro to object-oriented programming in java
Java 102   intro to object-oriented programming in javaJava 102   intro to object-oriented programming in java
Java 102 intro to object-oriented programming in java
agorolabs
 
SKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPTSKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPT
Skillwise Group
 
PPT Lecture-1.4.pptx
PPT Lecture-1.4.pptxPPT Lecture-1.4.pptx
PPT Lecture-1.4.pptx
HimanshuPandey957216
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming CLASSES-AND-OBJECTS.pptx
Object oriented programming CLASSES-AND-OBJECTS.pptxObject oriented programming CLASSES-AND-OBJECTS.pptx
Object oriented programming CLASSES-AND-OBJECTS.pptx
DaveEstonilo
 

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

chapter_one_Introduction_to_Object_Oriented_Programming_OOP.pptx
chapter_one_Introduction_to_Object_Oriented_Programming_OOP.pptxchapter_one_Introduction_to_Object_Oriented_Programming_OOP.pptx
chapter_one_Introduction_to_Object_Oriented_Programming_OOP.pptx
 
Java
JavaJava
Java
 
Core java
Core javaCore java
Core java
 
Introducing object oriented programming (oop)
Introducing object oriented programming (oop)Introducing object oriented programming (oop)
Introducing object oriented programming (oop)
 
Introduction to OOP concepts
Introduction to OOP conceptsIntroduction to OOP concepts
Introduction to OOP concepts
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Metaprogramming ruby
Metaprogramming rubyMetaprogramming ruby
Metaprogramming ruby
 
ppt_on_java.pptx
ppt_on_java.pptxppt_on_java.pptx
ppt_on_java.pptx
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Introduction to oop and java fundamentals
Introduction to oop and java fundamentalsIntroduction to oop and java fundamentals
Introduction to oop and java fundamentals
 
Cse java
Cse javaCse java
Cse java
 
cs213Lecture_1 java programming oopsss.pptx
cs213Lecture_1 java programming oopsss.pptxcs213Lecture_1 java programming oopsss.pptx
cs213Lecture_1 java programming oopsss.pptx
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
 
Java
JavaJava
Java
 
First fare 2011 frc-java-introduction
First fare 2011 frc-java-introductionFirst fare 2011 frc-java-introduction
First fare 2011 frc-java-introduction
 
Java 102 intro to object-oriented programming in java
Java 102   intro to object-oriented programming in javaJava 102   intro to object-oriented programming in java
Java 102 intro to object-oriented programming in java
 
SKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPTSKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPT
 
PPT Lecture-1.4.pptx
PPT Lecture-1.4.pptxPPT Lecture-1.4.pptx
PPT Lecture-1.4.pptx
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Object oriented programming CLASSES-AND-OBJECTS.pptx
Object oriented programming CLASSES-AND-OBJECTS.pptxObject oriented programming CLASSES-AND-OBJECTS.pptx
Object oriented programming CLASSES-AND-OBJECTS.pptx
 

Recently uploaded

GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
Tomaz Bratanic
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
SOFTTECHHUB
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
Kumud Singh
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Safe Software
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
Claudio Di Ciccio
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
kumardaparthi1024
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
Neo4j
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 

Recently uploaded (20)

GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 

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