SlideShare a Scribd company logo
Polymorphism, Abstract
Classes & Interfaces
Gaddis
Chapter 11
11-2
The Object Class
Because every class is directly or indirectly
derived from the Object class:
every class inherits the Object class’s members.
example: toString and equals.
In the Object class, the toString method
returns a string containing the object’s class
name and a hash of its memory address.
The equals method accepts the address of an
object as its argument and returns true if it is the
same as the calling object’s address.
Example: ObjectMethods.java
11-3
Polymorphism (pp. 685-689)
A reference variable can reference objects of
classes that are derived from the variable’s class.
GradedActivity exam;
We can use the exam variable to reference a
GradedActivity object.
exam = new GradedActivity();
The GradedActivity class is also used as the
superclass for the FinalExam class.
An object of the FinalExam class is a
GradedActivity object.
11-4
Polymorphism
A GradedActivity variable can be used to
reference a FinalExam object.
GradedActivity exam = new FinalExam(50, 7);
This statement creates a FinalExam object and
stores the object’s address in the exam variable.
This is an example of polymorphism.
The term polymorphism means the ability to take
many forms.
In Java, a reference variable is polymorphic because
it can reference objects of types different from its own,
as long as those types are subclasses of its type.
11-5
Polymorphism
Other legal polymorphic references:
GradedActivity exam1 = new FinalExam(50, 7);
GradedActivity exam2 = new PassFailActivity(70);
GradedActivity exam3 = new PassFailExam(100, 10, 70);
The GradedActivity class has three methods:
setScore, getScore, and getGrade.
A GradedActivity variable can be used to call
only those three methods.
GradedActivity exam = new PassFailExam(100, 10, 70);
System.out.println(exam.getScore()); // This works.
System.out.println(exam.getGrade()); // This works.
System.out.println(exam.getPointsEach()); // ERROR!
11-6
Polymorphism and Dynamic
Binding
If the object of the subclass has overridden
a method in the superclass:
If the variable makes a call to that method the
subclass’s version of the method will be run.
GradedActivity exam = new PassFailActivity(60);
exam.setScore(70);
System.out.println(exam.getGrade());
Java performs dynamic binding or late binding
when a variable contains a polymorphic reference.
The Java Virtual Machine determines at runtime
which method to call, depending on the type of
object that the variable references.
11-7
Polymorphism
It is the object’s type, rather than the
reference type, that determines which
method is called.
Example:
Polymorphic.java
You cannot assign a superclass object
to a subclass reference variable.
11-8
Abstract Classes
An abstract class cannot be instantiated, but
other classes are derived from it.
An Abstract class serves as a superclass for
other classes.
The abstract class represents the generic or
abstract form of all the classes that are
derived from it.
A class becomes abstract when you place
the abstract key word in the class definition.
public abstract class ClassName
11-9
Abstract Methods
An abstract method has no body and must be
overridden in a subclass.
An abstract method is a method that appears in a
superclass, but expects to be overridden in a
subclass.
An abstract method has only a header and no
body.
AccessSpecifier abstract ReturnType MethodName(ParameterList);
Example:
Student.java, CompSciStudent.java, CompSciStudentDemo.java
11-10
Abstract Methods
Notice that the key word abstract appears
in the header, and that the header ends with
a semicolon.
public abstract void setValue(int value);
Any class that contains an abstract method
is automatically abstract.
If a subclass fails to override an abstract
method, a compiler error will result.
Abstract methods are used to ensure that a
subclass implements the method.
11-11
Interfaces
An interface is similar to an abstract class that has
all abstract methods.
It cannot be instantiated, and
all of the methods listed in an interface must be written
elsewhere.
The purpose of an interface is to specify behavior
for other classes.
An interface looks similar to a class, except:
the keyword interface is used instead of the keyword
class, and
the methods that are specified in an interface have no
bodies, only headers that are terminated by semicolons.
11-12
Interfaces
The general format of an interface
definition:
public interface InterfaceName
{
(Method headers...)
}
All methods specified by an interface are
public by default.
A class can implement one or more
interfaces.
11-13
Interfaces
If a class implements an interface, it
uses the implements keyword in the
class header.
public class FinalExam3 extends
GradedActivity implements Relatable
Example:
GradedActivity.java
Relatable.java
FinalExam3.java
InterfaceDemo.java
11-14
Fields in Interfaces
An interface can contain field declarations:
all fields in an interface are treated as final and static.
Because they automatically become final, you must
provide an initialization value.
public interface Doable
{
int FIELD1 = 1, FIELD2 = 2;
(Method headers...)
}
In this interface, FIELD1 and FIELD2 are final static
int variables.
Any class that implements this interface has access to
these variables.
11-15
Implementing Multiple
Interfaces
A class can be derived from only one superclass.
Java allows a class to implement multiple
interfaces.
When a class implements multiple interfaces, it
must provide the methods specified by all of them.
To specify multiple interfaces in a class definition,
simply list the names of the interfaces, separated
by commas, after the implements key word.
public class MyClass implements Interface1,
Interface2,
Interface3
11-16
Interfaces in UML
GradedActivity
Relatable
FinalExam3
A dashed line with an arrow
indicates implementation of an
interface.
11-17
Polymorphism with Interfaces
Java allows you to create reference variables
of an interface type.
An interface reference variable can reference
any object that implements that interface,
regardless of its class type.
This is another example of polymorphism.
Example:
RetailItem.java
CompactDisc.java
DvdMovie.java
PolymorphicInterfaceDemo.java
11-18
Polymorphism with Interfaces
In the example code, two RetailItem
reference variables, item1 and item2, are
declared.
The item1 variable references a CompactDisc
object and the item2 variable references a
DvdMovie object.
When a class implements an interface, an
inheritance relationship known as interface
inheritance is established.
a CompactDisc object is a RetailItem, and
a DvdMovie object is a RetailItem.
11-19
Polymorphism with Interfaces
A reference to an interface can point to any
class that implements that interface.
You cannot create an instance of an interface.
RetailItem item = new RetailItem(); // ERROR!
When an interface variable references an object:
only the methods declared in the interface are available,
explicit type casting is required to access the other
methods of an object referenced by an interface reference.

More Related Content

PDF
Java 06
PPT
Java interfaces
PPT
Interfaces
PPTX
Object oriented programming in php 5
PPTX
Object oriented programming in php 5
PDF
java-06inheritance
DOC
Java interview questions
PPT
9781439035665 ppt ch10
Java 06
Java interfaces
Interfaces
Object oriented programming in php 5
Object oriented programming in php 5
java-06inheritance
Java interview questions
9781439035665 ppt ch10

Similar to PolyInterfaces.pptnowand tomoroooooooooo (20)

PPT
Chap11
PPT
Introduction to OOP with PHP
PPTX
LISP: Object Sytstem Lisp
PPTX
LISP:Object System Lisp
PPT
Classes2
PPTX
FINAL_DAY10_INTERFACES_roles and benefits.pptx
PPTX
Chap3 inheritance
PPTX
java_inheritance_oop_20250730110153.pptx
PDF
Generics and collections in Java
PPT
inner-classes-abstract-classevgdddfs.ppt
PPTX
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
PPTX
Java Basics
PPTX
Java basics
PPTX
Hemajava
PDF
Object Oriented Principles
PDF
L5
DOC
116824015 java-j2 ee
PPTX
C# interview
PDF
1669617800196.pdf
PPT
Unit 3 Java
Chap11
Introduction to OOP with PHP
LISP: Object Sytstem Lisp
LISP:Object System Lisp
Classes2
FINAL_DAY10_INTERFACES_roles and benefits.pptx
Chap3 inheritance
java_inheritance_oop_20250730110153.pptx
Generics and collections in Java
inner-classes-abstract-classevgdddfs.ppt
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
Java Basics
Java basics
Hemajava
Object Oriented Principles
L5
116824015 java-j2 ee
C# interview
1669617800196.pdf
Unit 3 Java
Ad

Recently uploaded (20)

PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
Information Storage and Retrieval Techniques Unit III
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
Fundamentals of safety and accident prevention -final (1).pptx
PDF
Analyzing Impact of Pakistan Economic Corridor on Import and Export in Pakist...
PDF
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
PPTX
CURRICULAM DESIGN engineering FOR CSE 2025.pptx
PPTX
Fundamentals of Mechanical Engineering.pptx
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
PPT on Performance Review to get promotions
PDF
Soil Improvement Techniques Note - Rabbi
PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PPTX
communication and presentation skills 01
PPTX
Artificial Intelligence
PPTX
UNIT 4 Total Quality Management .pptx
PDF
BIO-INSPIRED ARCHITECTURE FOR PARSIMONIOUS CONVERSATIONAL INTELLIGENCE : THE ...
PPTX
Safety Seminar civil to be ensured for safe working.
PDF
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
PDF
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
PDF
Abrasive, erosive and cavitation wear.pdf
R24 SURVEYING LAB MANUAL for civil enggi
Information Storage and Retrieval Techniques Unit III
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Fundamentals of safety and accident prevention -final (1).pptx
Analyzing Impact of Pakistan Economic Corridor on Import and Export in Pakist...
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
CURRICULAM DESIGN engineering FOR CSE 2025.pptx
Fundamentals of Mechanical Engineering.pptx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPT on Performance Review to get promotions
Soil Improvement Techniques Note - Rabbi
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
communication and presentation skills 01
Artificial Intelligence
UNIT 4 Total Quality Management .pptx
BIO-INSPIRED ARCHITECTURE FOR PARSIMONIOUS CONVERSATIONAL INTELLIGENCE : THE ...
Safety Seminar civil to be ensured for safe working.
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
Abrasive, erosive and cavitation wear.pdf
Ad

PolyInterfaces.pptnowand tomoroooooooooo

  • 1. Polymorphism, Abstract Classes & Interfaces Gaddis Chapter 11
  • 2. 11-2 The Object Class Because every class is directly or indirectly derived from the Object class: every class inherits the Object class’s members. example: toString and equals. In the Object class, the toString method returns a string containing the object’s class name and a hash of its memory address. The equals method accepts the address of an object as its argument and returns true if it is the same as the calling object’s address. Example: ObjectMethods.java
  • 3. 11-3 Polymorphism (pp. 685-689) A reference variable can reference objects of classes that are derived from the variable’s class. GradedActivity exam; We can use the exam variable to reference a GradedActivity object. exam = new GradedActivity(); The GradedActivity class is also used as the superclass for the FinalExam class. An object of the FinalExam class is a GradedActivity object.
  • 4. 11-4 Polymorphism A GradedActivity variable can be used to reference a FinalExam object. GradedActivity exam = new FinalExam(50, 7); This statement creates a FinalExam object and stores the object’s address in the exam variable. This is an example of polymorphism. The term polymorphism means the ability to take many forms. In Java, a reference variable is polymorphic because it can reference objects of types different from its own, as long as those types are subclasses of its type.
  • 5. 11-5 Polymorphism Other legal polymorphic references: GradedActivity exam1 = new FinalExam(50, 7); GradedActivity exam2 = new PassFailActivity(70); GradedActivity exam3 = new PassFailExam(100, 10, 70); The GradedActivity class has three methods: setScore, getScore, and getGrade. A GradedActivity variable can be used to call only those three methods. GradedActivity exam = new PassFailExam(100, 10, 70); System.out.println(exam.getScore()); // This works. System.out.println(exam.getGrade()); // This works. System.out.println(exam.getPointsEach()); // ERROR!
  • 6. 11-6 Polymorphism and Dynamic Binding If the object of the subclass has overridden a method in the superclass: If the variable makes a call to that method the subclass’s version of the method will be run. GradedActivity exam = new PassFailActivity(60); exam.setScore(70); System.out.println(exam.getGrade()); Java performs dynamic binding or late binding when a variable contains a polymorphic reference. The Java Virtual Machine determines at runtime which method to call, depending on the type of object that the variable references.
  • 7. 11-7 Polymorphism It is the object’s type, rather than the reference type, that determines which method is called. Example: Polymorphic.java You cannot assign a superclass object to a subclass reference variable.
  • 8. 11-8 Abstract Classes An abstract class cannot be instantiated, but other classes are derived from it. An Abstract class serves as a superclass for other classes. The abstract class represents the generic or abstract form of all the classes that are derived from it. A class becomes abstract when you place the abstract key word in the class definition. public abstract class ClassName
  • 9. 11-9 Abstract Methods An abstract method has no body and must be overridden in a subclass. An abstract method is a method that appears in a superclass, but expects to be overridden in a subclass. An abstract method has only a header and no body. AccessSpecifier abstract ReturnType MethodName(ParameterList); Example: Student.java, CompSciStudent.java, CompSciStudentDemo.java
  • 10. 11-10 Abstract Methods Notice that the key word abstract appears in the header, and that the header ends with a semicolon. public abstract void setValue(int value); Any class that contains an abstract method is automatically abstract. If a subclass fails to override an abstract method, a compiler error will result. Abstract methods are used to ensure that a subclass implements the method.
  • 11. 11-11 Interfaces An interface is similar to an abstract class that has all abstract methods. It cannot be instantiated, and all of the methods listed in an interface must be written elsewhere. The purpose of an interface is to specify behavior for other classes. An interface looks similar to a class, except: the keyword interface is used instead of the keyword class, and the methods that are specified in an interface have no bodies, only headers that are terminated by semicolons.
  • 12. 11-12 Interfaces The general format of an interface definition: public interface InterfaceName { (Method headers...) } All methods specified by an interface are public by default. A class can implement one or more interfaces.
  • 13. 11-13 Interfaces If a class implements an interface, it uses the implements keyword in the class header. public class FinalExam3 extends GradedActivity implements Relatable Example: GradedActivity.java Relatable.java FinalExam3.java InterfaceDemo.java
  • 14. 11-14 Fields in Interfaces An interface can contain field declarations: all fields in an interface are treated as final and static. Because they automatically become final, you must provide an initialization value. public interface Doable { int FIELD1 = 1, FIELD2 = 2; (Method headers...) } In this interface, FIELD1 and FIELD2 are final static int variables. Any class that implements this interface has access to these variables.
  • 15. 11-15 Implementing Multiple Interfaces A class can be derived from only one superclass. Java allows a class to implement multiple interfaces. When a class implements multiple interfaces, it must provide the methods specified by all of them. To specify multiple interfaces in a class definition, simply list the names of the interfaces, separated by commas, after the implements key word. public class MyClass implements Interface1, Interface2, Interface3
  • 16. 11-16 Interfaces in UML GradedActivity Relatable FinalExam3 A dashed line with an arrow indicates implementation of an interface.
  • 17. 11-17 Polymorphism with Interfaces Java allows you to create reference variables of an interface type. An interface reference variable can reference any object that implements that interface, regardless of its class type. This is another example of polymorphism. Example: RetailItem.java CompactDisc.java DvdMovie.java PolymorphicInterfaceDemo.java
  • 18. 11-18 Polymorphism with Interfaces In the example code, two RetailItem reference variables, item1 and item2, are declared. The item1 variable references a CompactDisc object and the item2 variable references a DvdMovie object. When a class implements an interface, an inheritance relationship known as interface inheritance is established. a CompactDisc object is a RetailItem, and a DvdMovie object is a RetailItem.
  • 19. 11-19 Polymorphism with Interfaces A reference to an interface can point to any class that implements that interface. You cannot create an instance of an interface. RetailItem item = new RetailItem(); // ERROR! When an interface variable references an object: only the methods declared in the interface are available, explicit type casting is required to access the other methods of an object referenced by an interface reference.