



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The concept of polymorphism in java, which allows methods to change form based on the context in which they are used. It covers static and dynamic binding, polymorphism via inheritance, and polymorphism via interfaces. Examples of code demonstrating these concepts.
Typology: Papers
1 / 5
This page cannot be seen from the preview
Don't miss anything!




Mammal pet; Horse barbaro = new Horse(); pet = barbaro;
//******************************************************************** // Firm.java Java Foundations // // Demonstrates polymorphism via inheritance. //******************************************************************** public { class Firm //----------------------------------------------------------------- // Creates a staff of employees for a firm and pays them. //----------------------------------------------------------------- public static void main (String[] args) { Staff personnel = new Staff();
//******************************************************************** // Staff.java Java Foundations // //******************************************************************** Represents the personnel staff of a particular business. public class Staff { private // Constructor: StaffMember[] Sets staffList;up the list of staff members. //----------------------------------------------------------------- public Staff () { staffList = new StaffMember[5]; staffList[0] "555-0469", = new"123-45-6789", 2423.07); Executive ("Tony", "123 Main Line", staffList[1] "555-0101", = new"987-65-4321", 1246.15); Employee ("Paulie", "456 Off Line", staffList[2] "555-0000", = new"010-20-3040", 1169.23); Employee ("Vito", "789 Off Rocker", staffList[3] "555-0690", = new"958-47-3625", 10.55); Hourly ("Michael", "678 Fifth Ave.", staffList[4] "555-8374"); = new Volunteer ("Adrianna", "987 Babe Blvd.", ((Executive)staffList[0]).awardBonus (500.00); ((Hourly)staffList[3]).addHours (40); }
//----------------------------------------------------------------- // Pays all staff members.
{ double amount; for { (int count=0; count < staffList.length; count++)
if (amount == System.out.println ("Thanks!"); 0.0) else System.out.println (”To Be Paid: " + amount); }^ } }
abstract public { class StaffMember protected protected String name;String address; protected String phone; // //----------------------------------------------------------------- Constructor: Sets up this staff member using the specified info. public { StaffMember (String eName, String eAddress, String ePhone) name = address = eName; eAddress; }^ phone = ePhone; //----------------------------------------------------------------- // Derived classes must define the pay method for each type of employee.
//******************************************************************** // Volunteer.java Java Foundations // //******************************************************************** Represents a staff member that works as a volunteer. public { class Volunteer extends StaffMember //----------------------------------------------------------------- // Constructor: Sets up this volunteer using the specified // //----------------------------------------------------------------- information. public { Volunteer (String eName, String eAddress, String ePhone) }^ super^ (eName, eAddress,^ ePhone); //----------------------------------------------------------------- // Returns a zero pay value for this volunteer. //-----------------------------------------------------------------
{ return 0.0; }^ }
//******************************************************************** // Employee.java Java Foundations // //******************************************************************** Represents a general paid employee. public { class Employee extends StaffMember protected protected String socialSecurityNumber;double payRate; // //----------------------------------------------------------------- Constructor: Sets up this employee with the specified information. public Employee (String eName, String socSecNumber, double rate) String eAddress, String ePhone, {…} //----------------------------------------------------------------- // Returns the pay rate for this employee. //-----------------------------------------------------------------
}^ return^ payRate; }
//******************************************************************** // Executive.java Java Foundations // //******************************************************************** Represents an executive staff member, who can earn a bonus. public { class Executive extends Employee private double bonus; public Executive (String String eName,socSecNumber, String eAddress,double rate) String { … ePhone,} //----------------------------------------------------------------- // Computes and returns the pay for an executive, which is the // //----------------------------------------------------------------- regular employee payment plus a one-time bonus.
double payment = super.pay() + bonus; bonus = 0; }^ return^ payment; }
//******************************************************************** // SecretTest.java Java Foundations // // Demonstrates the use of a formal interface. //******************************************************************** public class SecretTest { //----------------------------------------------------------------- // Creates a Secret object and exercises its encryption. //----------------------------------------------------------------- public static void main (String[] args) { Secret hush = new Secret ("Wil Wheaton is my hero!"); System.out.println (hush); hush.encrypt(); System.out.println (hush); hush.decrypt(); System.out.println (hush); }^ }
//----------------------------------------------------------------- // Encrypts this secret using a Caesar cipher. Has no effect if // //----------------------------------------------------------------- this secret is already encrypted.
if { (!encrypted) String for (int masked index=0; = ""; index < message.length(); index++) message^ masked =^ =masked;^ masked^ +^ (char)(message.charAt(index)+shift); }^ encrypted^ =^ true; } (more…)
//----------------------------------------------------------------- // Decrypts and returns this secret. Has no effect if this // //----------------------------------------------------------------- secret is not currently encrypted.
if (encrypted) { String for (int unmasked index=0; index < message.length(); = ""; index++) message =^ unmasked unmasked;^ = unmasked +^ (char)(message.charAt(index)-shift); }^ encrypted^ =^ false; }^ return^ message; //----------------------------------------------------------------- // Returns true if this secret is currently encrypted. //----------------------------------------------------------------- public boolean isEncrypted() { return encrypted; } (more…)
A class can implement multiple interfaces The interfaces are listed in the implements clause The class must implement all methods in all interfaces listed in the header
The Java standard class library contains many helpful interfaces The Comparable interface contains one abstract method called compareTo, which is used to compare two objects The String class implements Comparable, giving us the ability to put strings in lexicographic order
You could write a class that implements certain methods (such as compareTo) without formally implementing the interface (Comparable) However, formally establishing the relationship between a class and an interface allows Java to deal with an object in certain ways Interfaces are a key aspect of OO design in Java
An interface name can be used as the type of an object reference variable
The current reference can be used to point to any object of any class that implements the Speaker interface The version of speak that the following line invokes depends on the type of object that current is referencing
Suppose two classes, Philosopher and Dog, both implement the Speaker interface, providing distinct versions of the speak method In the following code, the first call to speak invokes one version and the second invokes another