SlideShare a Scribd company logo
1 of 62
Object-Oriented
Programming:
Polymorphism
Chapter 10
What You Will Learn
 What

is polymorphism?

 How

to declare and use virtual
functions for abstract classes

2
Problem with Subclasses
Given the class hierarchy below
 Consider the existence of a draw function for
each subclass
 Consider also an array of references to the
superclass (which can also point to various
objects of the subclasses)
 How do you specify which draw statement to
be called when using the references


Shape class hierarchy

Shape
Circle

Triangle
Right Triangle

Isosceles Triangle

Rectangle
Square

3
Introduction
Polymorphism

– Enables “programming in the general”
– The same invocation can produce “many
forms” of results
Interfaces

– Implemented by classes to assign common
functionality to possibly unrelated classes

4
Polymorphism
When

a program invokes a method
through a superclass variable,
– the correct subclass version of the method
is called,
– based on the type of the reference stored
in the superclass variable

The

same method name and signature
can cause different actions to occur,
– depending on the type of object on which
the method is invoked
5
Polymorphism
 Polymorphism

enables programmers to
deal in generalities and
– let the execution-time environment handle
the specifics.

 Programmers

can command objects to
behave in manners appropriate to
those objects,
– without knowing the types of the objects
– (as long as the objects belong to the same
inheritance hierarchy).
6
Polymorphism Promotes
Extensibility
 Software

behavior

that invokes polymorphic

– independent of the object types to which
messages are sent.
 New

object types that can respond to
existing method calls can be
– incorporated into a system without
requiring modification of the base system.
– Only client code that instantiates new
objects must be modified to accommodate
new types.
7
Demonstrating Polymorphic
Behavior
A

superclass reference can be aimed at
a subclass object
– a subclass object “is-a” superclass object
– the type of the actual referenced object,
not the type of the reference, determines
which method is called

A

subclass reference can be aimed at a
superclass object only if the object is
downcasted
View example, Figure 10.1
8
Polymorphism
 Promotes

extensibility
 New objects types can respond to
existing method calls
– Can be incorporated into a system without
modifying base system
 Only

client code that instantiates the
new objects must be modified
– To accommodate new types
9
Abstract Classes and Methods
 Abstract

classes

– Are superclasses (called abstract
superclasses)
– Cannot be instantiated
– Incomplete
 subclasses

 Concrete

fill in "missing pieces"

classes

– Can be instantiated
– Implement every method they declare
– Provide specifics

10
Abstract Classes and Methods
 Purpose

of an abstract class

– Declare common
– Declare common
class hierarchy
 Contains

attributes
behaviors

…
of classes in a

one or more abstract methods

– Subclasses must override
 Instance

variables, concrete methods
of abstract class
– subject to normal rules of inheritance
11
 Abstract Classes
Classes

that are too general to
create real objects
Used only as abstract superclasses
for concrete subclasses and to
declare reference variables
Many inheritance hierarchies have
abstract superclasses occupying the
top few levels
12
Keyword abstract
Use

to declare a class abstract
Also use to declare a method
abstract
Abstract classes normally contain
one or more abstract methods
All concrete subclasses must
override all inherited abstract
methods
13
 Abstract Classes and Methods
Iterator

class

– Traverses all the objects in a
collection, such as an array
– Often used in polymorphic
programming to traverse a collection
that contains references to objects
from various levels of a hierarchy

14
 Abstract Classes
 Declares

common attributes and
behaviors of the various classes in a
class hierarchy.
 Typically contains one or more abstract
methods
– Subclasses must override if the subclasses
are to be concrete.
 Instance

variables and concrete
methods of an abstract class subject to
the normal rules of inheritance.
15
Beware! Compile Time Errors

 Attempting

to instantiate an object of
an abstract class
 Failure to implement a superclass’s
abstract methods in a subclass
– unless the subclass is also declared
abstract.
16
Creating Abstract Superclass
Employee
superclass Employee,
Figure 10.4

abstract

– earnings is declared abstract
 No

implementation can be given for earnings
in the Employee abstract class

– An array of Employee variables will store
references to subclass objects
method calls from these variables will
call the appropriate version of the earnings
method

 earnings

17
Example Based on Employee
Abstract Class
Abstract Class
Concrete
Concrete
Classes
Classes

Click on Classes to see source code
Click on Classes to see source code

18
Polymorphic interface for the
Employee hierarchy classes.

19
Note in Example Hierarchy
 Dynamic

binding

– Also known as late binding
– Calls to overridden methods are resolved
at execution time, based on the type of
object referenced
 instanceof

operator

– Determines whether an object is an
instance of a certain type

20
How Do They Do That?
 How

does it work?

– Access a derived object via base class
pointer
– Invoke an abstract method
– At run time the correct version of the
method is used
 Design

of the V-Table

– Note description from C++
21
Note in Example Hierarchy
 Downcasting

– Convert a reference to a superclass to a
reference to a subclass
– Allowed only if the object has an is-a
relationship with the subclass
 getClass

method

– Inherited from Object
– Returns an object of type Class
 getName

method of class Class

– Returns the class’s name

22
Superclass And Subclass
Assignment Rules
Assigning a superclass reference to
superclass variable straightforward
 Subclass reference to subclass variable
straightforward
 Subclass reference to superclass variable
safe


–
–


because of is-a relationship
Referring to subclass-only members through
superclass variables a compilation error

Superclass reference to a subclass variable a
compilation error
– Downcasting can get around this error

23
 final Methods and Classes
final

methods

– Cannot be overridden in a subclass
– private and static methods implicitly
final
– final methods are resolved at compile
time, this is known as static binding
 Compilers

final

can optimize by inlining the code

classes

– Cannot be extended by a subclass
– All methods in a final class implicitly
final

24
Why Use Interfaces
 Java

has single inheritance, only
 This means that a child class inherits
from only one parent class
 Sometimes multiple inheritance would
be convenient
 Interfaces give Java some of the
advantages of multiple inheritance
without incurring the disadvantages
25
Why Use Interfaces
 Provide

capability for unrelated classes
to implement a set of common
methods
 Define and standardize ways people
and systems can interact
 Interface specifies what operations
must be permitted
 Does not specify how performed
26
What is an Interface?
 An

interface is a collection of constants
and method declarations
 An interface describes a set of methods
that can be called on an object
 The method declarations do not include
an implementation
– there is no method body
27
What is an Interface?
A

child class that extends a parent
class can also implement an interface
to gain some additional behavior
 Implementing an interface is a
“promise” to include the specified
method(s)
 A method in an interface cannot be
made private
28
When A Class Definition
Implements An Interface:
 It

must implement each method in the
interface
 Each method must be public (even
though the interface might not say so)
 Constants from the interface can be
used as if they had been defined in the
class (They should not be re-defined in
the class)
29
Declaring Constants with Interfaces
Interfaces

can be used to declare
constants used in many class
declarations
– These constants are implicitly public,
static and final
– Using a static import declaration allows
clients to use these constants with just
their names

30
Implementation vs. Interface
Inheritance






Implementation
Inheritance
Functionality high in the
hierarchy
Each new subclass
inherits one or more
methods declared in
superclass
Subclass uses
superclass declarations

Interface Inheritance








Functionality lower in
hierarchy
Superclass specifies one
or more abstract
methods
Must be declared for
each class in hierarchy
Overridden for
subclass-specific
implementations
31
Creating and Using Interfaces
 Declaration

begins with interface

keyword
 Classes implement an interface (and its
methods)
 Contains public abstract methods
– Classes (that implement the interface)
must implement these methods

32
Creating and Using Interfaces
 Consider

the possibility of having a
class which manipulates mathematical
functions
 You want to send a function as a
parameter
– Note that C++ allows this directly
– Java does not
 This

task can be accomplished with
interfaces
33
Creating and Using Interfaces
 Declare

interface Function
 Declare class MyFunction which
implements Function
 Note other functions which are subclass
objects of MyFunction
 View test program which passes
Function subclass objects to function
manipulation methods
34
Case Study: A Payable Hierarchy
 Payable

interface

– Contains method getPaymentAmount
– Is implemented by the Invoice and
Employee classes
Click to view the
test program

35
End of Chapter 10 Lecture

36
Following slides are from previous
edition. Links may not work.

37
Source Code for Shape Superclass
Hierarchy
 Shape

superclass, Figure 10.6

– Note abstract method, getName
 Point

subclass, Figure 10.7

– Note, extends Shape, override of getName
 Circle

subclass, Figure 10.8

– Note extends Point, override of getArea,
getName, and toString
 Cylinder

subclass, Figure 10.9

– Note extends Circle, override of getArea,
38
getName, and toString
Source Code for Shape Superclass
Hierarchy
 Driver

program to demonstrate,
Figure 10.10
– Note array of superclass references

 Output

of
program

39
Polymorphic Payroll System
Employee

SalariedEmployee

CommissionEmployee

HourlyEmployee

BasePlusCommissionEmployee

Class hierarchy for polymorphic payroll application

40
Polymorphic Payroll System
 Abstract

superclass, Employee,
Figure 10.12
– Note abstract class specification, abstract
earnings method

 Abstract

subclass, SalariedEmployee,
Figure 10.13
– Note extends Employee, override of
earnings method

 Look

at other subclasses in text, pg
461 …
– Note here also the override of earnings41
Polymorphic Payroll System
 View

test program, Figure 10.17

– Note array of superclass references which
are pointed at various subclass objects
– Note generic calls of toString method
– Note use of instantceof operator
– Consider use of downcasting (line 37)
– Note use of getClass().getName()
method
 Gives

access to the name of the class
42
Polymorphic Payroll System
 Output

of the
payroll testing
program
 Note results of
getClass().getName()

calls

43
Creating and Using Interfaces
 View

implementation of the interface
Shape, Figure 10.19
Note the following:
– Line 4
public class Point extends Object
implements Shape { …

– Declaration of additional methods
– Declaration of abstract methods getArea,
getVolume, and getName
44
Nested Classes
 Top-level

classes

– Not declared inside a class or a method
 Nested

classes

– Declared inside other classes
– Inner classes
 Non-static

nested classes

 Demonstrated

in Figure 10.22

– Run the program
– Audio
45
Mentioned In The Audio
Object

 Inheritance

Hierarchy

Component
Container

 Panes

of a JFrame

– Background
– Content
– Glass

Window
Frame
JFrame

46
Mentioned In The Audio


Three things required when
performing events
1. Implement the interface
2. Register the event handlers
3. Specifically implement the
actionPerformed methods

47
Nested Classes
 An

inner class is allowed to directly
access its inner class's variables and
methods
 When this used in an inner class
– Refers to current inner-class object
 To

access outer-class using this

– Precede this with outer-class name

48
Nested Classes
 Anonymous

inner class

– Declared inside a method of a class
– Has no name
 Inner

class declared inside a method

– Can access outer class's members
– Limited to local variables of method in
which declared
 Note

use of anonymous inner class,
Figure 10.23
49
Notes on Nested Classes


Compiling class that contains nested class
– Results in separate .class file

– OuterClassName$InnerClassName.class



Inner classes with names can be declared as
– public, protected, private or package access
50
Notes on Nested Classes
 Access

outer class’s this reference

OuterClassName.this
 Outer

class is responsible for creating
inner class objects
OuterClassName.InnerClassName innerRef =
ref.new InnerClassName();

 Nested

classes can be declared static

– Object of outer class need not be created
– Static nested class does not have access
to outer class's non-static members
51
Type-Wrapper Classes for Primitive
Types
 Each

primitive type has one
– Character, Byte, Integer,
Boolean, etc.
 Enable to represent primitive as
Object
– Primitive values can be processed
polymorphically using wrapper classes
 Declared

as final
 Many methods are declared static
52
Invoking Superclass Methods from
Subclass Objects
 Recall

the point and circle hierarchy

– Note :
Assignment of superclass reference to
superclass variable
– Assignment of subclass reference to
subclass variable
 No

surprises … but the "is-a"
relationship makes possible
– Assignment of subclass reference to
superclass variable

 See

Figure 10.1

53
Using Superclass References with
Subclass-Type Variables
 Suppose

we have a superclass object

Point3 point = new Point3 (30, 40);
 Then

a subclass reference

Circle4 circle;
 It

would be illegal to have the subclass
reference aimed at the superclass
object
circle = point;
54
Subclass Method Calls via
Superclass-Type Variables
 Consider

aiming a subclass
reference at a superclass object
– If you use this to access a subclassonly method it is illegal

 Note

Figure 10.3

– Lines 22 - 23

55
Summary of Legal Assignments between
Super- and Subclass Variables
Assigning superclass reference to superclasstype variable OK
 Assigning subclass reference to subclass-type
variable OK
 Assigning subclass object's reference to
superclass type-variable, safe


– A circle "is a" point
– Only possible to invoke superclass methods


Do NOT assign superclass reference to
subclass-type variable
– You can cast the superclass reference to a
subclass type (explicitly)
56
Polymorphism Examples
Suppose designing video game
 Superclass SpaceObject

– Subclasses Martian, SpaceShip, LaserBeam
– Contains method draw



To refresh screen

– Send draw message to each object
– Same message has “many forms” of results



Easy to add class Mercurian

– Extends SpaceObject
– Provides its own implementation of draw



Programmer does not need to change code
– Calls draw regardless of object’s type
– Mercurian objects “plug right in”

57
Polymorphism
 Enables

programmers to deal in
generalities
– Let execution-time environment handle
specifics

 Able

to command objects to behave in
manners appropriate to those objects
– Don't need to know type of the object
– Objects need only to belong to same
inheritance hierarchy
58
Abstract Classes and Methods
 Abstract

classes not required, but
reduce client code dependencies
 To make a class abstract
– Declare with keyword abstract
– Contain one or more abstract methods
public abstract void draw();

– Abstract methods
 No

implementation, must be overridden

59
Inheriting Interface and
Implementation
Make abstract superclass Shape
 Abstract method (must be implemented)
– getName, print
– Default implementation does not make sense


Methods may be overridden
– getArea, getVolume
 Default

implementations return 0.0

– If not overridden, uses superclass default
implementation


Subclasses Point, Circle, Cylinder
60
Polymorphic Interface For The
Shape Hierarchy Class
getArea

Shape

getVolume

getName

toString

0.0

0.0

abstract

default
Object
implement
Shape

Point

0.0

0.0

"Point"

[x,y]
Point

Circle

πr2

0.0

"Circle"

center=[x,y];
radius=r

Circle
Cylinder

2πr +2πrh
2

πr h
2

"Cylinder"

center=[x,y];
radius=r;
height=h

Cylinder

61
Creating and Using Interfaces
 All

the same "is-a" relationships apply
when an interface inheritance is used
 Objects of any class that extend the
interface are also objects of the
interface type
– Circle extends Point, thus it is-a Shape
 Multiple

interfaces are possible

– Comma-separated list used in declaration
62

More Related Content

What's hot

Polymorphism in java, method overloading and method overriding
Polymorphism in java,  method overloading and method overridingPolymorphism in java,  method overloading and method overriding
Polymorphism in java, method overloading and method overridingJavaTportal
 
JAVA Polymorphism
JAVA PolymorphismJAVA Polymorphism
JAVA PolymorphismMahi Mca
 
Object-Oriented Polymorphism Unleashed
Object-Oriented Polymorphism UnleashedObject-Oriented Polymorphism Unleashed
Object-Oriented Polymorphism UnleashedNaresh Chintalcheru
 
Dynamic Polymorphism in C++
Dynamic Polymorphism in C++Dynamic Polymorphism in C++
Dynamic Polymorphism in C++Dharmisha Sharma
 
Polymorphism In Java
Polymorphism In JavaPolymorphism In Java
Polymorphism In JavaSpotle.ai
 
Polymorphism in java
Polymorphism in java Polymorphism in java
Polymorphism in java Janu Jahnavi
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in javasureshraj43
 
Polymorphism in Python
Polymorphism in Python Polymorphism in Python
Polymorphism in Python Home
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in javaAhsan Raja
 
Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++Anil Bapat
 
Polymorphism 140527082302-phpapp01
Polymorphism 140527082302-phpapp01Polymorphism 140527082302-phpapp01
Polymorphism 140527082302-phpapp01Engr.Tazeen Ahmed
 
encapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingencapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingShivam Singhal
 
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 javaCPD INDIA
 

What's hot (20)

Polymorphism in java, method overloading and method overriding
Polymorphism in java,  method overloading and method overridingPolymorphism in java,  method overloading and method overriding
Polymorphism in java, method overloading and method overriding
 
Javapolymorphism
JavapolymorphismJavapolymorphism
Javapolymorphism
 
JAVA Polymorphism
JAVA PolymorphismJAVA Polymorphism
JAVA Polymorphism
 
Object-Oriented Polymorphism Unleashed
Object-Oriented Polymorphism UnleashedObject-Oriented Polymorphism Unleashed
Object-Oriented Polymorphism Unleashed
 
Dynamic Polymorphism in C++
Dynamic Polymorphism in C++Dynamic Polymorphism in C++
Dynamic Polymorphism in C++
 
Polymorphism In Java
Polymorphism In JavaPolymorphism In Java
Polymorphism In Java
 
Polymorphism in java
Polymorphism in java Polymorphism in java
Polymorphism in java
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Polymorphism in Python
Polymorphism in Python Polymorphism in Python
Polymorphism in Python
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Java(Polymorphism)
Java(Polymorphism)Java(Polymorphism)
Java(Polymorphism)
 
polymorphism
polymorphism polymorphism
polymorphism
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in java
 
Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++
 
Polymorphism 140527082302-phpapp01
Polymorphism 140527082302-phpapp01Polymorphism 140527082302-phpapp01
Polymorphism 140527082302-phpapp01
 
Inheritance and polymorphism
Inheritance and polymorphism   Inheritance and polymorphism
Inheritance and polymorphism
 
encapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingencapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloading
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
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
 
OOP java
OOP javaOOP java
OOP java
 

Similar to Polymorphism Chapter: Virtual Functions & Abstract Classes

Core java interview questions
Core java interview questionsCore java interview questions
Core java interview questionsVinay Kumar
 
Corejavainterviewquestions.doc
Corejavainterviewquestions.docCorejavainterviewquestions.doc
Corejavainterviewquestions.docJoyce Thomas
 
Java/J2EE interview Qestions
Java/J2EE interview QestionsJava/J2EE interview Qestions
Java/J2EE interview QestionsArun Vasanth
 
116824015 java-j2 ee
116824015 java-j2 ee116824015 java-j2 ee
116824015 java-j2 eehomeworkping9
 
Complete java&j2ee
Complete java&j2eeComplete java&j2ee
Complete java&j2eeShiva Cse
 
Object Oriented Programming - Polymorphism and Interfaces
Object Oriented Programming - Polymorphism and InterfacesObject Oriented Programming - Polymorphism and Interfaces
Object Oriented Programming - Polymorphism and InterfacesHabtamu Wolde
 
Java Core Parctical
Java Core ParcticalJava Core Parctical
Java Core ParcticalGaurav Mehta
 
Review oop and ood
Review oop and oodReview oop and ood
Review oop and oodthan sare
 
chapter 5 concepts of object oriented programming
chapter 5 concepts of object oriented programmingchapter 5 concepts of object oriented programming
chapter 5 concepts of object oriented programmingWondimuBantihun1
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classesShreyans Pathak
 
Chapter 05 polymorphism extra
Chapter 05 polymorphism extraChapter 05 polymorphism extra
Chapter 05 polymorphism extraNurhanna Aziz
 

Similar to Polymorphism Chapter: Virtual Functions & Abstract Classes (20)

L5
L5L5
L5
 
Core java interview questions
Core java interview questionsCore java interview questions
Core java interview questions
 
Corejavainterviewquestions.doc
Corejavainterviewquestions.docCorejavainterviewquestions.doc
Corejavainterviewquestions.doc
 
Java/J2EE interview Qestions
Java/J2EE interview QestionsJava/J2EE interview Qestions
Java/J2EE interview Qestions
 
Ppt chapter10
Ppt chapter10Ppt chapter10
Ppt chapter10
 
Classes2
Classes2Classes2
Classes2
 
116824015 java-j2 ee
116824015 java-j2 ee116824015 java-j2 ee
116824015 java-j2 ee
 
Core java by amit
Core java by amitCore java by amit
Core java by amit
 
JavaHTP6e_10.ppt
JavaHTP6e_10.pptJavaHTP6e_10.ppt
JavaHTP6e_10.ppt
 
Java interview questions
Java interview questionsJava interview questions
Java interview questions
 
Complete java&j2ee
Complete java&j2eeComplete java&j2ee
Complete java&j2ee
 
Object Oriented Programming - Polymorphism and Interfaces
Object Oriented Programming - Polymorphism and InterfacesObject Oriented Programming - Polymorphism and Interfaces
Object Oriented Programming - Polymorphism and Interfaces
 
Java Core Parctical
Java Core ParcticalJava Core Parctical
Java Core Parctical
 
Core java questions
Core java questionsCore java questions
Core java questions
 
Review oop and ood
Review oop and oodReview oop and ood
Review oop and ood
 
Java session2
Java session2Java session2
Java session2
 
chapter 5 concepts of object oriented programming
chapter 5 concepts of object oriented programmingchapter 5 concepts of object oriented programming
chapter 5 concepts of object oriented programming
 
L05 Frameworks
L05 FrameworksL05 Frameworks
L05 Frameworks
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
 
Chapter 05 polymorphism extra
Chapter 05 polymorphism extraChapter 05 polymorphism extra
Chapter 05 polymorphism extra
 

More from Kumar

Graphics devices
Graphics devicesGraphics devices
Graphics devicesKumar
 
Fill area algorithms
Fill area algorithmsFill area algorithms
Fill area algorithmsKumar
 
region-filling
region-fillingregion-filling
region-fillingKumar
 
Bresenham derivation
Bresenham derivationBresenham derivation
Bresenham derivationKumar
 
Bresenham circles and polygons derication
Bresenham circles and polygons dericationBresenham circles and polygons derication
Bresenham circles and polygons dericationKumar
 
Introductionto xslt
Introductionto xsltIntroductionto xslt
Introductionto xsltKumar
 
Extracting data from xml
Extracting data from xmlExtracting data from xml
Extracting data from xmlKumar
 
Xml basics
Xml basicsXml basics
Xml basicsKumar
 
XML Schema
XML SchemaXML Schema
XML SchemaKumar
 
Publishing xml
Publishing xmlPublishing xml
Publishing xmlKumar
 
Applying xml
Applying xmlApplying xml
Applying xmlKumar
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XMLKumar
 
How to deploy a j2ee application
How to deploy a j2ee applicationHow to deploy a j2ee application
How to deploy a j2ee applicationKumar
 
JNDI, JMS, JPA, XML
JNDI, JMS, JPA, XMLJNDI, JMS, JPA, XML
JNDI, JMS, JPA, XMLKumar
 
EJB Fundmentals
EJB FundmentalsEJB Fundmentals
EJB FundmentalsKumar
 
JSP and struts programming
JSP and struts programmingJSP and struts programming
JSP and struts programmingKumar
 
java servlet and servlet programming
java servlet and servlet programmingjava servlet and servlet programming
java servlet and servlet programmingKumar
 
Introduction to JDBC and JDBC Drivers
Introduction to JDBC and JDBC DriversIntroduction to JDBC and JDBC Drivers
Introduction to JDBC and JDBC DriversKumar
 
Introduction to J2EE
Introduction to J2EEIntroduction to J2EE
Introduction to J2EEKumar
 

More from Kumar (20)

Graphics devices
Graphics devicesGraphics devices
Graphics devices
 
Fill area algorithms
Fill area algorithmsFill area algorithms
Fill area algorithms
 
region-filling
region-fillingregion-filling
region-filling
 
Bresenham derivation
Bresenham derivationBresenham derivation
Bresenham derivation
 
Bresenham circles and polygons derication
Bresenham circles and polygons dericationBresenham circles and polygons derication
Bresenham circles and polygons derication
 
Introductionto xslt
Introductionto xsltIntroductionto xslt
Introductionto xslt
 
Extracting data from xml
Extracting data from xmlExtracting data from xml
Extracting data from xml
 
Xml basics
Xml basicsXml basics
Xml basics
 
XML Schema
XML SchemaXML Schema
XML Schema
 
Publishing xml
Publishing xmlPublishing xml
Publishing xml
 
DTD
DTDDTD
DTD
 
Applying xml
Applying xmlApplying xml
Applying xml
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
 
How to deploy a j2ee application
How to deploy a j2ee applicationHow to deploy a j2ee application
How to deploy a j2ee application
 
JNDI, JMS, JPA, XML
JNDI, JMS, JPA, XMLJNDI, JMS, JPA, XML
JNDI, JMS, JPA, XML
 
EJB Fundmentals
EJB FundmentalsEJB Fundmentals
EJB Fundmentals
 
JSP and struts programming
JSP and struts programmingJSP and struts programming
JSP and struts programming
 
java servlet and servlet programming
java servlet and servlet programmingjava servlet and servlet programming
java servlet and servlet programming
 
Introduction to JDBC and JDBC Drivers
Introduction to JDBC and JDBC DriversIntroduction to JDBC and JDBC Drivers
Introduction to JDBC and JDBC Drivers
 
Introduction to J2EE
Introduction to J2EEIntroduction to J2EE
Introduction to J2EE
 

Recently uploaded

DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????blackmambaettijean
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 

Recently uploaded (20)

DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 

Polymorphism Chapter: Virtual Functions & Abstract Classes

  • 2. What You Will Learn  What is polymorphism?  How to declare and use virtual functions for abstract classes 2
  • 3. Problem with Subclasses Given the class hierarchy below  Consider the existence of a draw function for each subclass  Consider also an array of references to the superclass (which can also point to various objects of the subclasses)  How do you specify which draw statement to be called when using the references  Shape class hierarchy Shape Circle Triangle Right Triangle Isosceles Triangle Rectangle Square 3
  • 4. Introduction Polymorphism – Enables “programming in the general” – The same invocation can produce “many forms” of results Interfaces – Implemented by classes to assign common functionality to possibly unrelated classes 4
  • 5. Polymorphism When a program invokes a method through a superclass variable, – the correct subclass version of the method is called, – based on the type of the reference stored in the superclass variable The same method name and signature can cause different actions to occur, – depending on the type of object on which the method is invoked 5
  • 6. Polymorphism  Polymorphism enables programmers to deal in generalities and – let the execution-time environment handle the specifics.  Programmers can command objects to behave in manners appropriate to those objects, – without knowing the types of the objects – (as long as the objects belong to the same inheritance hierarchy). 6
  • 7. Polymorphism Promotes Extensibility  Software behavior that invokes polymorphic – independent of the object types to which messages are sent.  New object types that can respond to existing method calls can be – incorporated into a system without requiring modification of the base system. – Only client code that instantiates new objects must be modified to accommodate new types. 7
  • 8. Demonstrating Polymorphic Behavior A superclass reference can be aimed at a subclass object – a subclass object “is-a” superclass object – the type of the actual referenced object, not the type of the reference, determines which method is called A subclass reference can be aimed at a superclass object only if the object is downcasted View example, Figure 10.1 8
  • 9. Polymorphism  Promotes extensibility  New objects types can respond to existing method calls – Can be incorporated into a system without modifying base system  Only client code that instantiates the new objects must be modified – To accommodate new types 9
  • 10. Abstract Classes and Methods  Abstract classes – Are superclasses (called abstract superclasses) – Cannot be instantiated – Incomplete  subclasses  Concrete fill in "missing pieces" classes – Can be instantiated – Implement every method they declare – Provide specifics 10
  • 11. Abstract Classes and Methods  Purpose of an abstract class – Declare common – Declare common class hierarchy  Contains attributes behaviors … of classes in a one or more abstract methods – Subclasses must override  Instance variables, concrete methods of abstract class – subject to normal rules of inheritance 11
  • 12.  Abstract Classes Classes that are too general to create real objects Used only as abstract superclasses for concrete subclasses and to declare reference variables Many inheritance hierarchies have abstract superclasses occupying the top few levels 12
  • 13. Keyword abstract Use to declare a class abstract Also use to declare a method abstract Abstract classes normally contain one or more abstract methods All concrete subclasses must override all inherited abstract methods 13
  • 14.  Abstract Classes and Methods Iterator class – Traverses all the objects in a collection, such as an array – Often used in polymorphic programming to traverse a collection that contains references to objects from various levels of a hierarchy 14
  • 15.  Abstract Classes  Declares common attributes and behaviors of the various classes in a class hierarchy.  Typically contains one or more abstract methods – Subclasses must override if the subclasses are to be concrete.  Instance variables and concrete methods of an abstract class subject to the normal rules of inheritance. 15
  • 16. Beware! Compile Time Errors  Attempting to instantiate an object of an abstract class  Failure to implement a superclass’s abstract methods in a subclass – unless the subclass is also declared abstract. 16
  • 17. Creating Abstract Superclass Employee superclass Employee, Figure 10.4 abstract – earnings is declared abstract  No implementation can be given for earnings in the Employee abstract class – An array of Employee variables will store references to subclass objects method calls from these variables will call the appropriate version of the earnings method  earnings 17
  • 18. Example Based on Employee Abstract Class Abstract Class Concrete Concrete Classes Classes Click on Classes to see source code Click on Classes to see source code 18
  • 19. Polymorphic interface for the Employee hierarchy classes. 19
  • 20. Note in Example Hierarchy  Dynamic binding – Also known as late binding – Calls to overridden methods are resolved at execution time, based on the type of object referenced  instanceof operator – Determines whether an object is an instance of a certain type 20
  • 21. How Do They Do That?  How does it work? – Access a derived object via base class pointer – Invoke an abstract method – At run time the correct version of the method is used  Design of the V-Table – Note description from C++ 21
  • 22. Note in Example Hierarchy  Downcasting – Convert a reference to a superclass to a reference to a subclass – Allowed only if the object has an is-a relationship with the subclass  getClass method – Inherited from Object – Returns an object of type Class  getName method of class Class – Returns the class’s name 22
  • 23. Superclass And Subclass Assignment Rules Assigning a superclass reference to superclass variable straightforward  Subclass reference to subclass variable straightforward  Subclass reference to superclass variable safe  – –  because of is-a relationship Referring to subclass-only members through superclass variables a compilation error Superclass reference to a subclass variable a compilation error – Downcasting can get around this error 23
  • 24.  final Methods and Classes final methods – Cannot be overridden in a subclass – private and static methods implicitly final – final methods are resolved at compile time, this is known as static binding  Compilers final can optimize by inlining the code classes – Cannot be extended by a subclass – All methods in a final class implicitly final 24
  • 25. Why Use Interfaces  Java has single inheritance, only  This means that a child class inherits from only one parent class  Sometimes multiple inheritance would be convenient  Interfaces give Java some of the advantages of multiple inheritance without incurring the disadvantages 25
  • 26. Why Use Interfaces  Provide capability for unrelated classes to implement a set of common methods  Define and standardize ways people and systems can interact  Interface specifies what operations must be permitted  Does not specify how performed 26
  • 27. What is an Interface?  An interface is a collection of constants and method declarations  An interface describes a set of methods that can be called on an object  The method declarations do not include an implementation – there is no method body 27
  • 28. What is an Interface? A child class that extends a parent class can also implement an interface to gain some additional behavior  Implementing an interface is a “promise” to include the specified method(s)  A method in an interface cannot be made private 28
  • 29. When A Class Definition Implements An Interface:  It must implement each method in the interface  Each method must be public (even though the interface might not say so)  Constants from the interface can be used as if they had been defined in the class (They should not be re-defined in the class) 29
  • 30. Declaring Constants with Interfaces Interfaces can be used to declare constants used in many class declarations – These constants are implicitly public, static and final – Using a static import declaration allows clients to use these constants with just their names 30
  • 31. Implementation vs. Interface Inheritance    Implementation Inheritance Functionality high in the hierarchy Each new subclass inherits one or more methods declared in superclass Subclass uses superclass declarations Interface Inheritance     Functionality lower in hierarchy Superclass specifies one or more abstract methods Must be declared for each class in hierarchy Overridden for subclass-specific implementations 31
  • 32. Creating and Using Interfaces  Declaration begins with interface keyword  Classes implement an interface (and its methods)  Contains public abstract methods – Classes (that implement the interface) must implement these methods 32
  • 33. Creating and Using Interfaces  Consider the possibility of having a class which manipulates mathematical functions  You want to send a function as a parameter – Note that C++ allows this directly – Java does not  This task can be accomplished with interfaces 33
  • 34. Creating and Using Interfaces  Declare interface Function  Declare class MyFunction which implements Function  Note other functions which are subclass objects of MyFunction  View test program which passes Function subclass objects to function manipulation methods 34
  • 35. Case Study: A Payable Hierarchy  Payable interface – Contains method getPaymentAmount – Is implemented by the Invoice and Employee classes Click to view the test program 35
  • 36. End of Chapter 10 Lecture 36
  • 37. Following slides are from previous edition. Links may not work. 37
  • 38. Source Code for Shape Superclass Hierarchy  Shape superclass, Figure 10.6 – Note abstract method, getName  Point subclass, Figure 10.7 – Note, extends Shape, override of getName  Circle subclass, Figure 10.8 – Note extends Point, override of getArea, getName, and toString  Cylinder subclass, Figure 10.9 – Note extends Circle, override of getArea, 38 getName, and toString
  • 39. Source Code for Shape Superclass Hierarchy  Driver program to demonstrate, Figure 10.10 – Note array of superclass references  Output of program 39
  • 41. Polymorphic Payroll System  Abstract superclass, Employee, Figure 10.12 – Note abstract class specification, abstract earnings method  Abstract subclass, SalariedEmployee, Figure 10.13 – Note extends Employee, override of earnings method  Look at other subclasses in text, pg 461 … – Note here also the override of earnings41
  • 42. Polymorphic Payroll System  View test program, Figure 10.17 – Note array of superclass references which are pointed at various subclass objects – Note generic calls of toString method – Note use of instantceof operator – Consider use of downcasting (line 37) – Note use of getClass().getName() method  Gives access to the name of the class 42
  • 43. Polymorphic Payroll System  Output of the payroll testing program  Note results of getClass().getName() calls 43
  • 44. Creating and Using Interfaces  View implementation of the interface Shape, Figure 10.19 Note the following: – Line 4 public class Point extends Object implements Shape { … – Declaration of additional methods – Declaration of abstract methods getArea, getVolume, and getName 44
  • 45. Nested Classes  Top-level classes – Not declared inside a class or a method  Nested classes – Declared inside other classes – Inner classes  Non-static nested classes  Demonstrated in Figure 10.22 – Run the program – Audio 45
  • 46. Mentioned In The Audio Object  Inheritance Hierarchy Component Container  Panes of a JFrame – Background – Content – Glass Window Frame JFrame 46
  • 47. Mentioned In The Audio  Three things required when performing events 1. Implement the interface 2. Register the event handlers 3. Specifically implement the actionPerformed methods 47
  • 48. Nested Classes  An inner class is allowed to directly access its inner class's variables and methods  When this used in an inner class – Refers to current inner-class object  To access outer-class using this – Precede this with outer-class name 48
  • 49. Nested Classes  Anonymous inner class – Declared inside a method of a class – Has no name  Inner class declared inside a method – Can access outer class's members – Limited to local variables of method in which declared  Note use of anonymous inner class, Figure 10.23 49
  • 50. Notes on Nested Classes  Compiling class that contains nested class – Results in separate .class file – OuterClassName$InnerClassName.class  Inner classes with names can be declared as – public, protected, private or package access 50
  • 51. Notes on Nested Classes  Access outer class’s this reference OuterClassName.this  Outer class is responsible for creating inner class objects OuterClassName.InnerClassName innerRef = ref.new InnerClassName();  Nested classes can be declared static – Object of outer class need not be created – Static nested class does not have access to outer class's non-static members 51
  • 52. Type-Wrapper Classes for Primitive Types  Each primitive type has one – Character, Byte, Integer, Boolean, etc.  Enable to represent primitive as Object – Primitive values can be processed polymorphically using wrapper classes  Declared as final  Many methods are declared static 52
  • 53. Invoking Superclass Methods from Subclass Objects  Recall the point and circle hierarchy – Note : Assignment of superclass reference to superclass variable – Assignment of subclass reference to subclass variable  No surprises … but the "is-a" relationship makes possible – Assignment of subclass reference to superclass variable  See Figure 10.1 53
  • 54. Using Superclass References with Subclass-Type Variables  Suppose we have a superclass object Point3 point = new Point3 (30, 40);  Then a subclass reference Circle4 circle;  It would be illegal to have the subclass reference aimed at the superclass object circle = point; 54
  • 55. Subclass Method Calls via Superclass-Type Variables  Consider aiming a subclass reference at a superclass object – If you use this to access a subclassonly method it is illegal  Note Figure 10.3 – Lines 22 - 23 55
  • 56. Summary of Legal Assignments between Super- and Subclass Variables Assigning superclass reference to superclasstype variable OK  Assigning subclass reference to subclass-type variable OK  Assigning subclass object's reference to superclass type-variable, safe  – A circle "is a" point – Only possible to invoke superclass methods  Do NOT assign superclass reference to subclass-type variable – You can cast the superclass reference to a subclass type (explicitly) 56
  • 57. Polymorphism Examples Suppose designing video game  Superclass SpaceObject – Subclasses Martian, SpaceShip, LaserBeam – Contains method draw  To refresh screen – Send draw message to each object – Same message has “many forms” of results  Easy to add class Mercurian – Extends SpaceObject – Provides its own implementation of draw  Programmer does not need to change code – Calls draw regardless of object’s type – Mercurian objects “plug right in” 57
  • 58. Polymorphism  Enables programmers to deal in generalities – Let execution-time environment handle specifics  Able to command objects to behave in manners appropriate to those objects – Don't need to know type of the object – Objects need only to belong to same inheritance hierarchy 58
  • 59. Abstract Classes and Methods  Abstract classes not required, but reduce client code dependencies  To make a class abstract – Declare with keyword abstract – Contain one or more abstract methods public abstract void draw(); – Abstract methods  No implementation, must be overridden 59
  • 60. Inheriting Interface and Implementation Make abstract superclass Shape  Abstract method (must be implemented) – getName, print – Default implementation does not make sense  Methods may be overridden – getArea, getVolume  Default implementations return 0.0 – If not overridden, uses superclass default implementation  Subclasses Point, Circle, Cylinder 60
  • 61. Polymorphic Interface For The Shape Hierarchy Class getArea Shape getVolume getName toString 0.0 0.0 abstract default Object implement Shape Point 0.0 0.0 "Point" [x,y] Point Circle πr2 0.0 "Circle" center=[x,y]; radius=r Circle Cylinder 2πr +2πrh 2 πr h 2 "Cylinder" center=[x,y]; radius=r; height=h Cylinder 61
  • 62. Creating and Using Interfaces  All the same "is-a" relationships apply when an interface inheritance is used  Objects of any class that extend the interface are also objects of the interface type – Circle extends Point, thus it is-a Shape  Multiple interfaces are possible – Comma-separated list used in declaration 62