COP2800 - Computer Programming Using JAVA

COP2800 - Computer Programming Using JAVA

COP2800 Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 21 Inheritance and Polymorphism (contd) Webpage: www.cise.ufl.edu/~mssz/JavaNM/Top-Level.html COP2800 Programming in JAVA Course Objectives Basic Knowledge of Computers & Programming Specific Knowledge of JAVA Programming Practical Programming Projects Build Skills

Todays Class Review of Inheritance Concept(s) Polymorphism What is Polymorphism, and How is it Used? Polymorphism in Java Review: Java Program Structure HIGH-LEVEL VIEW PICTURE CREDIT: http://www.webbasedprogramming.com/JAVA-Developers-Guide/ch4.htm JAVA Units: - Packages - Classes

(Instances) - Methods - Instructions - Variables Review: Java Package Structure PICTURE CREDIT: http://users.soe.ucsc.edu/~charlie/book/notes/summary1-4/sld016.htm Review: Classes and Objects Example: CLASS NAME: String VARIABLE NAME: greeting String greeting = "Hello world!";

CLASS DESCRIPTION Instance-of OBJECT #1 Instance-of OBJECT #2 Instance-of ... OBJECT #n

Review: Inheritance Example: SUBCLASS NAME: Golfer SUPERCLASS NAME: Person class Golfer extends Person; SUPERCLASS DESCRIPTION extends SUBCLASS #1 DESCRIPTION extends SUBCLASS #2

DESCRIPTION extends ... SUBCLASS #n DESCRIPTION Inheritance Example 1: SUPERCLASS PERSON extends SUBCLASS #1

GOLFER ADD NEW PROPERTIES Subclass Variables: swing stance grip score extends SUBCLASS #2 STUDENT

Subclass Variables: gpa health_status major extracurriculars Inheritance Inheritanceisisthe thecapability capability ofofaaclass to use the

class to use the properties propertiesand andmethods methodsofof another anotherclass classwhile whileadding adding itsitsown functionality. own functionality. extends ...

... SUBCLASS #n ELECTRICIAN Subclass Variables: availability hourly_rate company unionized Inheritance: Highest Level? Object is the highest-level class in Java Object is extended to form all Java

classes (which are Objects subclasses) Image Credit: http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html Java: Class Specification Example 2: A Bicycle class CLASS VARIABLES (see Constructor below) CONSTRUCTOR INITIALIZES Class Variables but wait, theres more WHAT ABOUT THE METHODS?? Code Credit: http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html Java: Class Specification

Example 2: Bicycle class (contd) speed = speed decrement ; speed = speed + increment ; Code Credit: http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html Java: Class Specification Example 2: Bicycle class (Big Picture) CLASS NAME: CLASS VARIABLES CONSTRUCTOR METHOD #1 METHOD #2

METHOD #3 METHOD #4 Code Credit: http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html Java: Subclass Specification Example 2: MountainBike subclass CLASS NAME: NEW CLASS VARIABLE CONSTRUCTOR (MountainBike) NEW METHOD

Code Credit: http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html Java: Subclass Specification Example 2: MountainBike subclass Inherits all variables (fields) and methods of Bicycle class Adds variable (field) seatHeight Adds method SetHeight to set the variable

seatHeight Original Bicycle class has 3 fields & 4 methods. New MountainBike subclass has 4 variables (fields) and 5 methods (Bicycles + new ones). New Concept: Polymorphism Meaning: Poly = many Morph = shape Example: + operator can do many things int + int integer addition

int + float decimal addition string + string concatenation Polymorphism (contd) NEW WORD: Overloading Example: int + int integer addition

int + float decimal addition string + string concatenation We say the + operator is overloaded because one operator performs many functions Polymorphism (contd) Polymorphism in programming: Strategies: 1 Write many functions to do many things

2 Write one polymorphic function to do many things OBJECTORIENTED DESIGN Polymorphism (contd) Write one function to do many (contd) Example: length() method Class #1 length method specification Class #2 length method specification

things Polymorphism (contd) JAVA: One function to act on many objects Example: length() method Class #1 object1 = Class #2 object2 = object.length object.length Polymorphism (contd) Example 3: Animals and their Noises

Lets begin with a public class called Animal public class Animal { public boolean IsQuadruped = true; Now lets make a method called sleep: public void sleep() { System.out.println(Zzzzz); } } Code Credits: http://www.java-made-easy.com/java-inheritance.html http://www.java-made-easy.com/polymorphism-in-java.html Polymorphism (contd) Example 3: Animals and their Noises (contd) Lets create subclass Dog from Animal

public class Dog extends Animal { with a method for making waking noises: public void makeNoise() { public boolean awake = true; System.out.println(Woof!); } } Polymorphism (contd) Example 3: Animals and their Noises (contd) Lets create subclass Cat from Animal public class Cat extends Animal { with a method for making waking noises: public void makeNoise() {

public boolean awake = true; System.out.println(Meow!); } This is Approach #1 Many Functions to do } Many Things makeNoise is Polymorphic Polymorphism (contd) Example 3: Animals and their Noises (contd) Lets create an Abstract Class called Animal public abstract class AbAnimal { with a method for making waking noises: public void makeNoise(); } Note that there are no curly braces for the

method makeNoise just a semicolon Polymorphism (contd) Example 3: Animals and their Noises (contd) As before, lets create subclass Cat from AbAnimal public class Cat extends AbAnimal { public void makeNoise() { public boolean awake = true; System.out.println(Meow!); } } In the next example (Dog), makeNoise will also appear as a specializing method Polymorphism (contd) Example 3: Animals and their Noises (contd)

As before, lets create subclass Dog from AbAnimal public class Dog extends AbAnimal { public void makeNoise() { public boolean awake = true; System.out.println(Woof!); } } Approach #2: Method makeNoise is inherited from the abstract class specification AbAnimal Polymorphism (contd) Example 3: Animals and their Noises (contd) Summary: 1 Create an abstract class C 2 Embed an abstract method M within class C 3 Extend class C to create a subclass S1

4 Make a method M1 within S1 that specializes M 5 Extend class C to create a subclass S2 6 Make a method M2 within S2 that specializes M and so forth (N times) Result: Mi is polymorphic and overloaded N-fold More: Inheritance & Polymorphism READING ASSIGNMENT: D. Liang: Chapter 10 (Thinking in Objects) and Chapter 11 (Inheritance and Polymorphism) Friday 01 Mar 2013: How to do Assignment #3, Part III (LAPTOPS) After Spring Break: More on Inheritance & Polymorphism in JAVA Collections, Sets, Lists, and Queues

Recently Viewed Presentations

  • Inclusive Assessment - Ahead

    Inclusive Assessment - Ahead

    (Disability Act, 2005) Inclusive Assessment Disabled Students All Students Contingency Approach "special arrangements" such as extra time, own room, etc.) which is essentially a form of assimilation into an existing system Alternative Approach (e.g., a viva voce instead of a...
  • Diapositiva 1 - WordPress.com

    Diapositiva 1 - WordPress.com

    Un 15 -20 % de LCFA comparten criterios de Asma y EPOC ... Wingdings Monotype Sorts Arial Narrow 굴림 Times Arial Black Tahoma Times New Roman Symbol WP Greek Century Math B Secuencia 1_Secuencia Diapositiva de Microsoft PowerPoint Microsoft Graph...
  • Proposal to Change Portal Environment

    Proposal to Change Portal Environment

    in order to maintain similar BSB/Programme ratio as in 2004/2005 (***) This shows the gap in percentage points between the two ratios. Essentially this shows that with a ZRG budget the delivery would have to be $630M. in order to...
  • ICPR BISR Colloquium Risk and vulnerability in prison

    ICPR BISR Colloquium Risk and vulnerability in prison

    The project partner was Penal Reform International (led by Baroness Vivienne Stern). A Federal Law on Alternatives to Imprisonment was enacted at the end of December 2004, and the numbers serving Community Service Orders grew from 17,400 in 2005, to...
  • George Leigh Queensland Department of Agriculture and Fisheries

    George Leigh Queensland Department of Agriculture and Fisheries

    Fishing clubs allowed anglers to learn from experts. Personal mobility also increased (personal boats and 4WD vehicles). Also had changes in club regulations. E.g., scoring of weight versus number of fish. Clubs developed self-imposed minimum sizes that were higher than...
  • Morality and Ethics

    Morality and Ethics

    Definitions of Moral Codes. Internet Encyclopedia of Philosophy. Rules or norms within a group for what is proper behavior for the members of that group. Rules or norms provide the measure. Complex definitions of right and wrong. Different cultures have...
  • Individual Differences in Infant Attachment Security

    Individual Differences in Infant Attachment Security

    Control ("Demandingness") * * * * * * * * * * Authoritarian Low acceptance/responsiveness High control Power-assertive discipline Ex: "Do it because I say so" More likely to use physical punishment Authoritative High acceptance/responsiveness Moderate control Set clear standards...
  • Genius Sperm Bank - University of Wisconsin-Platteville

    Genius Sperm Bank - University of Wisconsin-Platteville

    Genius Sperm Bank. By: Grant Ritchie. Robert Graham: Life. Born June 9th, 1906 in Harbor Springs, MIwhich was a resort town for Business CEOs (White only) ... Graham and Muller agreed to start the Hermann J. Muller Repository of Germinal...