Java Foundations, Chapter 9, Polymorphism

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Chapter Objectives:

1) Define polymorphism and explore its benefits. 2) Discuss the concept of dynamic binding. 3) Use inheritance to create polymorphic references. 4) Explore the purpose of the syntax of Java interfaces. 5) Use interfaces to create polymorphic references. 6) Discuss object-oriented design in the context of polymorphism.

What is the difference between a class and an interface?

A class can be instantiated; an interface cannot. An interface can contain only abstract methods and constants. A class provides the implementation of r an interface.

interface

A collection of abstract methods, used to define a set of operations that can be used to interact with an object.

polymorphic reference

A reference variable that can refer to different types of objects at different points in time.

How can polymorphism be accomplished using interfaces?

An interface name can be used as the type of a reference. Such as reference variable can refer to any object of any class that implements that interface. Because all classes implement the same interface, they have methods with common signatures, which can be dynamically bound.

How do class hierarchies and interface hierarchies intersect?

Class hierarchies and interface hierarchies do not intersect. A class can be used to derive a new class, and an interface can be used to derive a new interface, but these two types of hierarchies do not overlap.

How does inheritance support polymorphism?

In Java, a reference variable declared using a parent class can be used to refer to an object of the child class. If both classes contain a method with the same signature, the parent reference can be polymorphic.

____ can be applied to interfaces so that one interface can be derived from another.

Inheritance

What is polymorphism?

Polymorphism is the ability of a reference variable to refer to objects of various types at different times. A method invoked through such a reference is bound to different method definitions at different times, depending on the type of the object referenced.

Describe the Comparable interface?

The Comparable interface contains a single method called compareTo, which should return an integer that is less than zero, equal to zero, or greater than zero if the executing object is less than, equal to, or greater than the object which it is being compared, respectively.

Why is the StaffMember class in the firm example declared as abstract?

The StaffMember class is abstract because it is not intended to be instantiated. It serves as a placeholder in the inheritance hierarchy to help organize and manage the object polymorphically.

polymorphism

The ability to define an operation that has more than one meaning by having the operation dynamically bound to methods of various objects.

dynamic binding

The binding of a method invocation to its definition at run-time. Also called late binding.

Interface hierarchy

The hierarchy formed when interfaces are derived from other interfaces. Interface hierarchies are distinct from class hierarchies.

Why is the pay method declared in the StaffMember class, given that it is abstract and has no body at that level?

The pay method has no meaning at the StaffMember level, so it is declared as abstract. But by declaring it there, we guarantee that every object of its children will have a pay method. This allows us to create an array of StaffMember objects, which is actually filled with various types of staff members, and to pay each one. The details of being paid are determined by each class, as appropriate.

How is overriding related to polymorphism?

When a child class overrides the definition of a parent's method, two versions of that method exist. If a polymorphic reference is used to invoke the method, the version of the method that is invoked is determined by the type of the object being referred to, not by the type of the reference variable.

An ____ is a collection of abstract methods and therefore cannot be instantiated.

interface

An _____ name can be used to declare an objects reference variable.

interface

An _____ reference can refer to any object of any class that implements that interface.

interface

The type of the _____, not the type of the reference, determines which version of a ____ is invoked.

object, method

A ____ to a method can be polymorphic which gives the method flexible control of its arguments.

parameter

A ____ reference can refer to different types of ____ over time.

polymorphic , objects

The relationship between a listener and the component it listens to is established using _____.

polymorphism

A _____ ____ can refer to any object created from any class related to it by inheritance.

reference variable

The binding of a method invocation to its definition is performed at ____ for a ____ reference.

run-time, polymorphic

binding

the process of determining which method definition is used to fulfill a given method invocation.


Kaugnay na mga set ng pag-aaral

US History (A) Chapter 1 Test Questions

View Set

Qualys Web Application Scanning (EXAM)

View Set

ECCouncil Computer Hacking Forensic Investigator (V9)

View Set

Media Psychology Exam 3 Study Guide

View Set

supply/demand and economic welfare

View Set