Chapter 10 OOP: Polymorphism

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

The UML expresses the relationship between a class and an interface through a relationship known as realization.

A class is said to "realize," or implement, the methods of an interface.

Serializable

An interface used to identify classes whose objects can be written to (i.e., serialized) or read from (i.e., deserialized) some type of storage (e.g., file on disk, database field) or transmitted across a network.

If class Rectangle is derived from class Quadrilateral, then a Rectangle object is a more specific version of a Quadrilateral.

Any operation (e.g., calculating the perimeter or the area) that can be performed on a Quadrilateral can also be performed on a Rectangle.

This process is known as dynamic binding or late binding. For example, line 46 implicitly invokes method toString of the object to which currentEmployee refers.

As a result of dynamic binding, Java decides which class's toString method to call at execution time rather than at compile time.

SwingConstants

Contains a set of constants used in GUI programming to position GUI elements on the screen. We explore GUI programming in

You can declare classes that implement (i.e., provide concrete implementations for the methods of) one or more interfaces.

Each interface method must be declared in all the classes that explicitly implement the interface. Once a class implements an interface, all objects of that class have an is-a relationship with the interface type, and all objects of the class are guaranteed to provide the functionality described by the interface.

By declaring this method abstract, we indicate that each concrete subclass must provide an appropriate earnings implementation and that a program will be able to use superclass

Employee variables to invoke method earnings polymorphically for any type of Employee.

Polymorphism is particularly effective for implementing so-called layered software systems. In operating systems, for example, each type of physical device could operate quite differently from the others.

Even so, commands to read or write data from and to devices may have a certain uniformity. For each device, the operating system uses a piece of software called a device driver to control all communication between the system and the device.

While processing the objects polymorphically, the program increases the base salary of each BasePlusCommissionEmployee by 10%—this requires determining the object's type at execution time.

Finally, the program polymorphically determines and outputs the type of each object in the Employee array. Lines 9-18 create objects of each of the four concrete Employee subclasses.

Runnable

Implemented by any class for which objects of that class should be able to execute in parallel using a technique called multithreading

Polymorphism enables you to "program in the general" rather than "program in the specific."

In particular, polymorphism enables you to write programs that process objects that share the same superclass (either directly or indirectly) as if they're all objects of the superclass; this can simplify programming.

Comparable

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare objects. Interface Comparable is used to allow objects of a class that implements the interface to be compared to one another. Interface Comparable is commonly used for ordering objects in a collection such as an array.

An interface is often used in place of an abstract class when there's no default implementation to inherit—that is, no fields and no default method implementations.

Like public abstract classes, interfaces are typically public types. Like a public class, a public interface must be declared in a file with the same name as the interface and the .java file-name extension.

A final method in a superclass cannot be overridden in a subclass—this guarantees that the final method implementation will be used by all direct and indirect subclasses in the hierarchy.

Methods that are declared private are implicitly final, because it's not possible to override them in a subclass. Methods that are declared static are also implicitly final. A final method's declaration can never change, so all subclasses use the same method implementation, and calls to final methods are resolved at compile time—this is known as static binding

Occasionally, when performing polymorphic processing, we need to program "in the specific."

Our Employee case study demonstrates that a program can determine the type of an object at execution time and act on that object accordingly.

A final class that's declared final cannot be a superclass (i.e., a class cannot extend a final class). All methods in a final class are implicitly final. Class String is an example of a final class. If you were allowed to create a subclass of String, objects of that subclass could be used wherever strings are expected.

Since class String cannot be extended, programs that use strings can rely on the functionality of String objects as specified in the Java API. Making the class final also prevents programmers from creating subclasses that might bypass security restrictions. For more insights on the use of keyword final, visit

Classes that can be used to instantiate objects are called concrete classes.

Such classes provide implementations of every method they declare (some of the implementations can be inherited)

Sometimes it's useful to declare classes—called abstract classes—for which you never intend to create objects. Because they're used only as superclasses in inheritance hierarchies, we refer to them as abstract superclasses.

These classes cannot be used to instantiate objects, because, as we'll soon see, abstract classes are incomplete. Subclasses must declare the "missing pieces" to become "concrete" classes, from which you can instantiate objects

Interfaces are particularly useful for assigning common functionality to possibly unrelated classes.

This allows objects of unrelated classes to be processed polymorphically—objects of classes that implement the same interface can respond to all of the interface method calls

In the next example, we aim a superclass reference at a subclass object. We then show how invoking a method on a subclass object via a superclass reference invokes the subclass functionality—the type of the referenced object, not the type of the variable, determines which method is called.

This example demonstrates that an object of a subclass can be treated as an object of its superclass,

An interface declaration begins with the keyword interface and contains only constants and abstract methods.

Unlike classes, all interface members must be public, and interfaces may not specify any implementation details, such as concrete method declarations and instance variables

The Java compiler does allow the assignment of a superclass reference to a subclass variable if we explicitly cast the superclass reference to the subclass type

Why would we ever want to perform such an assignment? A super-class reference can be used to invoke only the methods declared in the superclass—attempting to invoke subclass-only methods through a superclass reference results in compilation errors

GUI event-listener interfaces

You work with graphical user interfaces (GUIs) every day. In your web browser, you might type the address of a website to visit, or you might click a button to return to a previous site. The browser responds to your interaction and performs the desired task. Your interaction is known as an event, and the code that the browser uses to respond to an event is known as an event handler.

These assignments are natural and straightforward—super-class variables are intended to refer to superclass objects,

and subclass variables are intended to refer to subclass objects. However, as you'll soon see, other assignments are possible.

When a superclass variable contains a reference to a subclass object

and that reference is used to call a method, the subclass version of the method is called.

As you'll learn later in the chapter, you cannot treat a superclass object as a subclass object,

because a superclass object is not an object of any of its subclasses.

An abstract class normally contains one or more abstract methods. An abstract method is one with keyword abstract in its declaration, as in

public abstract void draw Abstract methods do not provide implementations.

If a program needs to perform a subclass-specific operation on a subclass object referenced by a superclass variable,

the program must first cast the superclass reference to a subclass reference through a technique known as downcasting. This enables the program to invoke subclass methods that are not in the superclass

To enable this mechanism to work with your objects, you simply have to mark them as Serializable by adding implements Serializable

to the end of your class declaration's first line. Then, all the objects of your class have the is-a relationship with Serializable.


Kaugnay na mga set ng pag-aaral

ENT - CH. 7 Financing and Accounting

View Set

Corint - Analizatorul olfactiv + gustativ

View Set

chapter 13 Research and Data Analysis (REVIEW)

View Set

SPAN102 Ch. 11 Práctica Ejercicio 3 Oraciones

View Set

EAQ 2 Maternity & Women's Health Pregnancy, Labor, Children

View Set

Swift interview questions, Swift Interview Prep, iOS INTERVIEW QUESTIONS

View Set

Pre-assesment - Introduction to Cryptography - C839 - WGU

View Set

Unit 29: Vehicle Owner Responsibilities

View Set