Download presentation
Presentation is loading. Please wait.
Published byZoe Harmon Modified over 9 years ago
1
Lecture Objectives Learn what is an ADT, why is it useful Understand algorithm efficiency Use Eclipse for coding and testing Understand and use method overloading, overriding Find out why polymorphism is useful in software design CS 340 1
2
ADTs or… Abstract Data Types CS 340 2
3
ADTs Abstract Date Type (ADT) An encapsulation of data and methods Allows for reusable code The user need not know about the implementation of the ADT A user interacts with the ADT using only public methods CS 340 3
4
ADTs (cont.) ADTs facilitate storage, organization, and processing of information Such ADTs often are called data structures The Java Collections Framework provides implementations of common data structures Other reasons for the ADTs to exist? CS 340 4
5
Interfaces An interface specifies or describes an ADT to the applications programmer: methods and actions ADTs must perform what arguments, if any, must be passed to each method what result the method will return The interface can be viewed as a contract which guarantees how the ADT will function CS 340 5
6
Interfaces (cont.) A class that implements the interface provides code for the ADT If implementation satisfies the ADT contract, then Programmer may implement it as he or she chooses The programmer may add: data fields not in the implementation methods not in the implementation constructors CS 340 6
7
Example: smartPhone Interface An smartPhone is... It must provide operations to: wake up/sleep allow the user to open a particular application add contact display messages adjust volume A class that implements an smartPhone must provide a method for each operation CS 340 7
8
Example: smartPhone Interface (cont.) It must provide operations to: wake up/sleep allow the user to open a particular application add contact display messages adjust volume public interface smartPhone { } Interface ___Code CS 340 8
9
Example: smartPhone Interface (cont.) It must provide operations to: wake up/sleep allow the user to open a particular application add contact display messages adjust volume public interface smartPhone { } Interface ___Code The keyword interface in the header indicates that an interface is being declared CS 340 9
10
Example: smartPhone Interface (cont.) It must provide operations to: wake up/sleep allow the user to open a particular application add contact display messages adjust volume public interface smartPhone { /** Wakes up after it verifies a user's PIN. @param pin The user's PIN @return success or failure */ boolean wakeUp(String pin); boolean sleep(); } Interface ___Code CS 340 10
11
Example: smartPhone Interface (cont.) It must provide operations to: wake up/sleep allow the user to open a particular application add contact display messages adjust volume public interface smartPhone { /** Allows the user to open an application. @param app The selected application @param success Whether or not the application opened*/ void openApplication(String app, boolean success); } Interface ___Code CS 340 11
12
Example: smartPhone Interface (cont.) It must provide operations to: wake up/sleep allow the user to open a particular application add contact display messages adjust volume public interface smartPhone { /** Allows the user to add a contact @param name @param phone numbers*/ void addContact(String name, int mobile, int home, int office, int other); } Interface ___Code CS 340 12
13
Example: smartPhone Interface (cont.) It must provide operations to: wake up/sleep allow the user to open a particular application add contact display messages adjust volume public interface smartPhone { /** Allows message to be displayed @param message to be printed*/ void printMessage(String message); } Interface ___Code CS 340 13
14
Example: smartPhone Interface (cont.) It must provide operations to: wake up/sleep allow the user to open a particular application add contact display messages adjust volume public interface smartPhone { /** Allows the user to change volume @param message to be printed @return success or fail*/ boolean changeVolume(int change); } Interface ___Code CS 340 14
15
Interfaces (cont.) Shows only headings for its methods These are considered abstract methods Abstract methods must be defined in a class that implements the interface CS 340 15
16
Interface Definition FORM: public interface interfaceName { abstract method headings constant declarations } EXAMPLE: public interface Payable { public abstract double calcSalary(); public abstract boolean salaried(); public static final double DEDUCTIONS = 25.5; } CS 340 16
17
The implements Clause Here is a class that implement an interface: public class smartPhoneiPhone implements smartPhone public class smartPhoneDroid implements smartPhone A class may implement more than one interface— their names are separated by commas CS 340 17
18
The implements Clause: Pitfalls Syntax error: Class smartPhoneDroid should be declared abstract; it does not define method printMessage(String) in interface smartPhone If a class contains an undefined abstract method, then It needs to be declared as an abstract class CS 340 18
19
The implements Clause: Pitfalls (cont.) You cannot instantiate an interface: smartPhone asmartPhone = new smartPhone(); // invalid statement Doing so will cause a syntax error: interface smartPhone is abstract; cannot be instantiated CS 340 19
20
Declaring a Variable of an Interface Type You can declare a variable that has an interface type /* expected type */ smartPhoneDroid ATM0 = new smartPhoneDroid() ; /* interface type */ smartPhone ATM1 = new smartPhoneDroid(); smartPhone ATM2 = new smartPhoneiPhone(); The reason for wanting to do this will become clear when we discuss polymorphism CS 340 20
21
Create a music library for myTunes store Include ratings and prices Eclipse 21 CS 340
22
22 myStore (we sell songs online) CS 340
23
Method Overriding, Method Overloading, Polymorphism 23 CS 340
24
Method Overriding Returning to the Robot example, if we declare and then run: Robot myRobot = new Computer("Asimo", "Intel", 5, 16000, 200.4); Cylon yourRobot = new Cylon(“Toaster", "AMD", 5, 24000, 1000.8, 5000.0, 7.5); System.out.println("My computer is:\n" + myRobot.toString()); System.out.println("Your computer is:\n" + yourRobot.toString()); What is the output? Cylon double pixels double battleSpeed Robot String manufacturer String processor int diskSize int numberOfParts double processorSpeed int getDiskSize() double getProcessorSpeed() int getParts() String toString() CS 340 24
25
Method Overriding (cont.) The output would be: My Robot is: Manufacturer: Asimo CPU: Intel Body parts: 5 Disk: 16000 gigabytes Speed: 200.4 gigahertz Your Robot is: Manufacturer: Toaster CPU: AMD Body parts: 5 Disk: 24000 gigabytes Speed: 1000.8 gigahertz The pixels and battleSpeed variables are not printed because Cylon has not defined a toString() method Cylon double pixels double battleSpeed Robot String manufacturer String processor int diskSize int numberOfParts double processorSpeed int getDiskSize() double getProcessorSpeed() int getParts() String toString() 25 CS 340
26
Method Overriding (cont.) To define a toString() for Cylon : public String toString() { String = result = super.toString() + "\nPixels: " + pixels + "\nSpeed in battle: " + battleSpeed + " shots/sec"; return result; } Now Cylon 's toString() method will override Robot 's inherited toString() method and will be called for all Cylon objects Cylon double pixels double battleSpeed Robot String manufacturer String processor int diskSize int numberOfParts double processorSpeed int getDiskSize() double getProcessorSpeed() int getParts() String toString() CS 340 26
27
To define a toString() for Cylon : public String toString() { String = result = super.toString() + "\nPixels: " + pixels + "\nSpeed in battle: " + battleSpeed + " shots/sec"; return result; } Now Cylon 's toString() method will override Robot 's inherited toString() method and will be called for all Cylon objects Method Overriding (cont.) super.methodName() Using the prefix super in a call to a method methodName calls the method with that name in the superclass of the current class CS 340 27 Cylon double pixels double battleSpeed Robot String manufacturer String processor int diskSize int numberOfParts double processorSpeed int getDiskSize() double getProcessorSpeed() int getParts() String toString()
28
Method Overloading Methods with the same name, return type, and parameters override corresponding inherited methods Methods with the same name but different parameters are overloaded CS 340 27
29
Method Overloading (cont.) Take, for example, our Cylon constructor: public Cylon(String man, String processor, int parts, int disk, double procSpeed, int pixels, int battleSpeed) {... } If we want to have a default manufacturer for a Cylon, we can create a constructor with six parameters instead of seven public Cylon(String processor, double ram, int disk, double procSpeed, double screen, double wei) { this(DEFAULT_NB_MAN, double ram, int disk, double procSpeed, int pixels, int battleSpeed); } 28 This call invokes the seven-parameter constructor passing on the six parameters in this constructor, plus the default manufacturer constant DEFAULT_NB_MAN
30
Method Overloading: Pitfall When overriding a method, the method must have: same name the same number and types of parameters in the same order If not, the method will overload This error is common SOLUTION: the annotation @Override @Override public String toString() {... } CS 340 29
31
Polymorphism Polymorphism means having many shapes Polymorphism is a central feature of OOP CS 340 30
32
Polymorphism (cont.) For example: You write a program to reference robots, You need a variable to reference a Robot or a Cylon Easy! Robot theRobot; can reference either a Robot or a Cylon —because a Cylon is-a Robot! CS 340 31
33
Polymorphism (cont.) Example: theRobot = new Cylon(“Centurion", "Intel", 4, 240, 6, 15000, 5000); System.out.println(theRobot.toStr ing()); Which toString() method will be called, Robot 's or Cylon 's? CS 340 32 Cylon double pixels double battleSpeed Robot String manufacturer String processor int diskSize int numberOfParts double processorSpeed int getDiskSize() double getProcessorSpeed() int getParts() String toString()
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.