[PDF] [PDF] Polymorphism through the Java Interface

To understand polymorphism, take an example of a workday at Franklin, Beedle, and Associates Kim brought in pastries while everyone was chatting When



Previous PDF Next PDF





[PDF] Java - Polymorphism - Tutorialspoint

Java objects are polymorphic since any object will pass the IS-A test for their Example: Let us look at an example public interface Vegetarian{} public class 



[PDF] Chapter 13 Inheritance and Polymorphism

For example, in the vehicle domain we could implement a Vehicle superclass as follows Vehicle java 1 2 3 4 5 6 7



[PDF] An Introduction to Polymorphism in Java - College Board

In the example below polymorphism is demonstrated by the use of multiple add methods The computer differentiates among them by the method signatures (the  



[PDF] Object- Oriented Programming: Polymorphism - Pearsoncmgcom

We then provide an example demonstrating polymorphic behavior performed due to Java's polymorphic capabilities about any manual transmission car



[PDF] Polymorphism

morphism occurs in Java through the difference between the declared static class A good example of a polymorphic variable is the array allPiles in the Solitare 



[PDF] Chapter 10 Object-Oriented Programming: Polymorphism

Object-Oriented Programming: Polymorphism Java™ How to Program, 8/e (C) 2010 Pearson Education, Inc All rights reserved 10 4 Abstract Classes and 



[PDF] Polymorphism through the Java Interface

To understand polymorphism, take an example of a workday at Franklin, Beedle, and Associates Kim brought in pastries while everyone was chatting When



[PDF] Polymorphism

Polymorphism A deeper look into Lets have a look at some examples java lang Object contains methods that all classes inherit ○ These include: ○ clone 



[PDF] Inheritance and Polymorphism - Fas Harvard - Harvard University

Example: our Taxi class can define its own toString method: public String toString () { The name for this feature of Java is polymorphism • from the Greek for 

[PDF] java polymorphism example source code

[PDF] java practice exercises online

[PDF] java printf format

[PDF] java printf left justify string

[PDF] java production support interview questions

[PDF] java program list

[PDF] java program to get data from excel sheet

[PDF] java program to sort string array

[PDF] java program using conditional operator

[PDF] java programing book in hindi pdf

[PDF] java programming by sagayaraj pdf

[PDF] java programming exercises online

[PDF] java programming for beginners pdf free download

[PDF] java programming model answer paper summer 2017

[PDF] java programming model answer paper summer 2018

1

Polymorphism through the

Java Interface

Rick Mercer

2

Outline

ŠDescribe Polymorphism

ŠShow a few ways that interfaces are used

Compare objects with Comparator

Create our own icons with Icon

Play audio files with AudioClip

The role of interfaces in Javas Collection Framework 3

Polymorphism

ŠIn general, polymorphism is the ability to appear in many forms

ŠIn object-oriented programming, polymorphism

refers to a programming language's ability to process objects differently depending on their data type (class) ŠPolymorphism is a requirement of any true object- oriented programming language 4

Polymorphism from mercer

To understand polymorphism, take an example of a

workday at Franklin, Beedle, and Associates. Kim brought in pastries while everyone was chatting. When the food was mostly devoured, Jim, the president of the company, invited everyone to 䇾Get back to work.䇿 Sue went back to read a new section of a book she was editing. Tom continued laying out a book. Stephanie went back to figure out some setting in her word- processing program. Ian finished the company catalog. 5

Polymorphism

Jeni met with Jim to discuss a new project. Chris began contacting professors to review a new manuscript. And Krista continued her Web search to find on whether colleges are using C++, Python, or Java. Marty went back to work on the index of his new book. Kim cleaned up the pastries. Rick was just visiting so he went to work on the remaining raspberries. 6

Polymorphic Messages

Š10 different behaviors with the same message!

ŠThe message 䇾Get back to work䇿is a

polymorphic message a message that is understood by many different types of object (or employees in this case) but responded to with different behaviors based on the type of the employee: Editor, Production, 7

Polymorphism

ŠPolymorphism allows the same message to be

sent to different types to get different behavior

ŠIn Java, polymorphism is possible through

inheritance ‡Example: Override toString()to return different values that are textual representations of that type. interfaces

‡Example: Collections.sort(List)sends

compareTomessages to objects that must have implemented Comparable 8

Polymorphism

ŠThe runtime message finds the correct method

same message can invoke different methods the reference variable knows the type aString.compareTo(anotherString) anInteger.compareTo(anotherInteger) aButton.actionPerformed(anEvent) aTextField.actionPerformed(anEvent) aList.add(anObject) aHashSet.add(anObject) 9

The Java Interface

ŠAn interface describes a set of methods

class variables are allowed (need static)

NOT allowed: constructors, instance variables

publicinterfaceIBowlingLine{ publicstaticfinalintLAST_FRAME= 10; publicabstractintscoreAtFrame(intframe); public intscoreSoFar(); // Interface methods are implicitly abstract, so the // abstract modifier is not used with interface methods // (it could be²it's just not necessary). // Also public by default, so this would work voidpinsDowned(int pins); 10

The Java Interface

ŠInterfaces are to be implemented by a class

~ 33% of classes (about 1,000) in Javas API implement one or more interfaces

ŠTypically, two or more classes implement the

same interface

Type guaranteed to have the same methods

Objects can be treated as the same type

May use different algorithms / instance variables

/ data structures 11

The Comparable interface

remember? ŠCan assign an instance of a class that implements and interface to a variable of the interface type

Comparable str = newString("abc");

Comparable acct = newBankAccount("B", 1);

Comparable day = newDate();

ŠSome classes implement Comparable

find out how many with Javas API Šinterface Comparable defines the "natural ordering" for collections 12 interface comparator * Compares its two arguments for order. Returns a * negative integer, zero, or a positive integer as the * first argument is less than, equal to, or greater * than the second argument. Equals not shown here publicinterfaceComparator { intcompare(T one, T other); ŠCan specify sort order by objects. In the code below

What class needs to be written?

What interface must that new class implement?

Comparator idComparator= newByID();

Collections.sort(accounts, idComparator);

Sort using different Comparators

13 class OurIcon implements Icon

Icon myIcon= new

JOptionPane.showMessageDialog(

null, "View from\nthe UofA\nComputer Science\nDepartment", "Message",

JOptionPane.INFORMATION_MESSAGE,

myIcon); ŠNotice the 5thparameter type, class or interface? public static void showMessageDialog(ComponentparentComponent, Objectmessage, Stringtitle, intmessageType,Iconicon) throws HeadlessException 14

LiveCamImage

publicclassLiveCamImage implementsIcon { privateBufferedImage myImage; publicLiveCamImage(String imageFileName) { try{ myImage= } catch(IOException e) { System.err.println("Could not load"+ imageFileName); // Control the upper left corner of the image publicvoidpaintIcon(Component c, Graphics g, intx, inty) { g.drawImage(myImage, 2, 2, null); // Icon also specifies getIconWidth and getIconHeight // See file in InterfaceExamples.zip 15

An interface you need now

ŠAn interface, a reference type, can have

staticvariables and method headings with ; publicintsize(); // no { }

ŠMany classes may implement an interface

Use interface ActionListenerto register any

number of objects to respond to button clicks, menu selections, and input into a text field public interfaceActionListener{ public void actionPerformed(ActionEventtheEvent); 16

Multiple classes implement

the same interface

ŠTo implement an interface, classes must have

all methods specified as given in the interface privateclassTheButtonListener implements ActionListener { publicvoidactionPerformed(ActionEvent theEvent) { // Do this method when a user clicks TheButton private classTheTextFieldListener implements ActionListener {

public void actionPerformed(ActionEvent theEvent) {// Do this code when user presses the enter // key in TheTextField when it has focus}}

quotesdbs_dbs17.pdfusesText_23