/** 1.00 07 Feb 1996 Cay Horstmann */ import java.util.*; import corejava.*; public class EmployeeTest { public static void main(String[]

Slides:



Advertisements
Similar presentations
Classes and Objects. What is Design? The parts of the software including – what information each part holds – what things each part can do – how the various.
Advertisements

Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Object-Oriented Programming: Polymorphism Outline 10.1 Introduction 10.2 Relationships Among.
Chapter 20- Virtual Functions and Polymorphism Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng.
Esempi Ereditarietà1 // Definition of class Point #ifndef POINT_H #define POINT_H #include using std::ostream; class Point { friend ostream &operator
1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
 2002 Prentice Hall. All rights reserved. 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2Base Classes and Derived Classes.
1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses 9.3 protected Members 9.4 Relationship.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 27 - Java Object-Oriented Programming Outline 27.1Introduction 27.2Superclasses and Subclasses.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses.
Object-Oriented Programming Outline Introduction Superclasses and Subclasses protected Members Relationship between Superclass Objects and Subclass Objects.
Object-Oriented Programming with Java Exceptions, Strings and Things Lecture 4.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20- Virtual Functions and Polymorphism Outline 20.1Introduction 20.2Type Fields and switch Statements.
Java boot camp1 Subclasses Concepts: The subclass and inheritance: subclass B of class A inherits fields and methods from A. A is a superclass of B. Keyword.
January 28 Objects Again Inheritance What About Types? Casting.
1 Overloading vs. Overriding b Don't confuse the concepts of overloading and overriding b Overloading deals with multiple methods in the same class with.
Chapter 8 – Sections covered
1 Lecture Note 9_Polymorphism Outline Relationships Among Objects in an Inheritance Hierarchy Invoking Base-Class Functions from Derived-Class object Virtual.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Object-Oriented Programming: Polymorphism Outline 10.1 Introduction 10.2 Relationships Among.
Chapter 8 Objects & Classes. Definition of Object-Oriented Programming (OOP) Object-Oriented Programming (OOP) uses the analogy of real objects as a template.
© Amir Kirsh Object Oriented Programming with Java Written by Amir Kirsh.
Chapter 9 - Object-Oriented Programming
1 Abstract Class There are some situations in which it is useful to define base classes that are never instantiated. Such classes are called abstract classes.
 2002 Prentice Hall. All rights reserved. Page 1 Chapter 9 - Object-Oriented Programming 9.10 Introduction to Polymorphism 9.11 Type Fields and switch.
Interfaces Need arising from software engineering –Disparate groups of programmers need to agree to a “contract” that spells out how their software interacts.
CS 307 Fundamentals of Computer ScienceInterfaces and Abstract Classes 1 Topic 7 Interfaces and Abstract Classes “I prefer Agassiz in the abstract, rather.
Advanced Programming Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra Lecture 1: Course overview.
 2002 Prentice Hall. All rights reserved. Chapter 9 - Object-Oriented Programming Outline 9.1 Introduction 9.2 Superclasses and Subclasses 9.3 protected.
Agenda Scope of variables Comparable interface Location class in GridWorld homework.
10/7/2015IT 1791 Java’s World in UML Object Shape {abstract} This is done implicitly Shape {abstract} - ShapeName: String # setShapeName(newShapeName:
1 Object-Oriented Programming: Polymorphism 10.1 Introduction 10.2 Relationships Among Objects in an Inheritance Hierarchy Invoking Superclass Methods.
 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)
 2002 Prentice Hall. All rights reserved. 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2Base Classes and Derived Classes.
CMSC 2021 Polymorphism. CMSC 2022 Static vs. Dynamic Binding Binding The determination of which method in the class hierarchy is to be used for a particular.
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 8 Inheritance and Polymorphism 一切皆有缘起.
Outline §Review of the last class l class variables and methods l method overloading and overriding §Inheritance and polymorphism l polymorphism l abstract.
1 OOP : main concepts Polymorphism. 2 OOP : main concepts  The main concepts:  In a superclass –public members Accessible anywhere program has a reference.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 20 - C++ Virtual Functions and Polymorphism.
Copyright (c) 1998, 1999 D.L. Bailey * Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.
 2003 Prentice Hall, Inc. All rights reserved Case Study: Three-Level Inheritance Hierarchy Three level point/circle/cylinder hierarchy –Point.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 – Inheritance Part 2 Outline 19.8Direct Base Classes and Indirect Base Classes 19.9Using Constructors.
Inheritance & Polymorphism1. 2 Introduction Besides composition, another form of reuse is inheritance. With inheritance, an object can inherit behavior.
 2002 Prentice Hall. All rights reserved. Modifed by Haytham Allos, NYU CSD V Week 2 - Object-Oriented Programming Outline 2.1 Introduction 2.2.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
1 Lecture 8 Chapter 10 - Object-Oriented Programming: Polymorphism Outline Introduction Relationships Among Objects in an Inheritance Hierarchy Invoking.
 2003 Prentice Hall, Inc. All rights reserved Virtual Functions Polymorphism –Same message, “print”, given to many objects All through a base.
CS100A, 15 Sept Lecture 5 1 CS100A, 5 Sept This lecture continues the discussion of classes. The important new concept of this lecture is.
CS100A, Fall Lecture 5 1 CS100A, Fall 1997 Lecture, Tuesday, 16 September. This lecture continues the discussion of classes. The important new concept.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Polymorphism Outline 20.5Polymorphism 20.6Case Study: A Payroll System Using Polymorphism.
1 Interfaces and Abstract Classes The ability to define the behavior of an object without specifying that behavior is to be implemented Interface class.
Object-Oriented Programming: Polymorphism Chapter 10.
 2002 Prentice Hall. All rights reserved. Page 1 Inheritance: Object-Oriented Programming Outline 9.1 Introduction 9.2 Superclasses and Subclasses 9.3.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Object-Oriented Programming: Polymorphism.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
Web Design & Development Lecture 9
Inheritance ITI1121 Nour El Kadri.
Chapter 9 – Object-Oriented Programming: Inheritance
Chapter 9 - Object-Oriented Programming
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Object Oriented Programming with Java
Week 6 Object-Oriented Programming (2): Polymorphism
Package & Java Access Specifiers
Chapter 9 - Object-Oriented Programming: Inheritance
Chapter 20- Virtual Functions and Polymorphism
Chapter 20 - C++ Virtual Functions and Polymorphism
Object-Oriented Programming: Polymorphism
Virtual Functions Example
Presentation transcript:

/** Feb 1996 Cay Horstmann */ import java.util.*; import corejava.*; public class EmployeeTest { public static void main(String[] args) { Employee[] staff = new Employee[3]; staff[0] = new Employee("Harry Hacker", 35000, new Day(1989,10,1)); staff[1] = new Employee("Carl Cracker", 75000, new Day(1987,12,15)); staff[2] = new Employee("Tony Tester", 38000, new Day(1990,3,15)); int i; for (i = 0; i < 3; i++) staff[i].raiseSalary(5); for (i = 0; i < 3; i++) staff[i].print(); }

class Employee { public Employee(String n, double s, Day d) { name = n; salary = s; hireDay = d; } public void print() { System.out.println(name + " " + salary + " " + hireYear()); } public void raiseSalary(double byPercent) { salary *= 1 + byPercent / 100; } public int hireYear() { return hireDay.getYear(); } private String name; private double salary; private Day hireDay; }

/** Mar 1998 Cay Horstmann */ public class Card { public static final int ACE = 1; public static final int JACK = 11; public static final int QUEEN = 12; public static final int KING = 13; public static final int CLUBS = 1; public static final int DIAMONDS = 2; public static final int HEARTS = 3; public static final int SPADES = 4; public Card(int v, int s) { value = v; suit = s; } public int getValue() { return value; }

public int getSuit() { return suit; } public int rank() { if (value == 1) return 4 * 13 + suit; else return 4 * (value - 1) + suit; } public String toString() { String v; String s; if (value == ACE) v = "Ace"; else if (value == JACK) v = "Jack"; else if (value == QUEEN) v = "Queen"; else if (value == KING) v = "King"; else v = String.valueOf(value); if (suit == DIAMONDS) s = "Diamonds"; else if (suit == HEARTS) s = "Hearts"; else if (suit == SPADES) s = "Spades"; else /* suit == CLUBS */ s = "Clubs"; return v + " of " + s; } private int value; private int suit; }

/** Mar 1998 Cay Horstmann */ import corejava.*; public class CardDeck { public CardDeck() { deck = new Card[52]; fill(); shuffle(); } public void fill() { int i; int j; for (i = 1; i <= 13; i++) for (j = 1; j <= 4; j++) deck[4 * (i - 1) + j - 1] = new Card(i, j); cards = 52; }

public void shuffle() { int next; for (next = 0; next < cards - 1; next++) { int r = new RandomIntGenerator(next, cards - 1).draw(); Card temp = deck[next]; deck[next] = deck[r]; deck[r] = temp; } public Card draw() { if (cards == 0) return null; cards--; return deck[cards]; }

public void play(int rounds) { int i; int wins = 0; for (i = 1; i <= rounds; i++) { Card yours = draw(); System.out.print("Your draw: " + yours + " "); Card mine = draw(); System.out.print("My draw: " + mine + " "); if (yours.rank() > mine.rank()) { System.out.println("You win"); wins++; } else System.out.println("I win"); } System.out.println("Your wins: " + wins + " My wins: " + (rounds - wins)); }

public static void main(String[] args) { CardDeck d = new CardDeck(); d.play(10); // play ten rounds } private Card[] deck; private int cards; }

/** Apr 1998 Cay Horstmann */ import java.util.*; import corejava.*; public class EmployeeSortTest { public static void main(String[] args) { Employee[] staff = new Employee[3]; staff[0] = new Employee("Harry Hacker", 35000, new Day(1989,10,1)); staff[1] = new Employee("Carl Cracker", 75000, new Day(1987,12,15)); staff[2] = new Employee("Tony Tester", 38000, new Day(1990,3,15)); ArrayAlg.shellSort(staff); int i; for (i = 0; i < staff.length; i++) System.out.println(staff[i]); }

abstract class Sortable { public abstract int compareTo(Sortable b); } class ArrayAlg { public static void shellSort(Sortable[] a) { int n = a.length; int incr = n / 2; while (incr >= 1) { for (int i = incr; i < n; i++) { Sortable temp = a[i]; int j = i; while (j >= incr && temp.compareTo(a[j - incr]) < 0) { a[j] = a[j - incr]; j -= incr; } a[j] = temp; } incr /= 2; }

class Employee extends Sortable { public Employee(String n, double s, Day d) { name = n; salary = s; hireDate = d; } public void raiseSalary(double byPercent) { salary *= 1 + byPercent / 100; } public String getName() { return name; } public double getSalary() { return salary; } public String toString() { return name + " " + salary + " " + hireYear(); } public int hireYear() { return hireDate.getYear(); }

public int compareTo(Sortable b) { Employee eb = (Employee)b; if (salary < eb.salary) return -1; if (salary > eb.salary) return 1; return 0; } private String name; private double salary; private Day hireDate; }

/** Apr 1998 Cay Horstmann */ import java.awt.*; import java.util.*; public class TileTest { public static void main(String[] args) { Tile[] a = new Tile[20]; int i; for (i = 0; i < a.length; i++) a[i] = new Tile(i, i, 10, 20, (int)(100 * Math.random())); Arrays.sort(a); for (i = 0; i < a.length; i++) System.out.println(a[i]); }

class Tile extends Rectangle implements Comparable { public Tile(int x, int y, int w, int h, int zz) { super(x, y, w, h); z = zz; } public int compareTo(Object b) { Tile tb = (Tile)b; return z - tb.z; } public String toString() { return super.toString() + "[z=" + z + "]"; } private int z; }

// Fig. 9.11: Shape.java // Definition of interface Shape public interface Shape { public abstract double area(); public abstract double volume(); public abstract String getName(); }

// Fig. 9.11: Point.java // Definition of class Point public class Point extends Object implements Shape { protected int x, y; // coordinates of the Point // no-argument constructor public Point() { setPoint( 0, 0 ); } // constructor public Point( int a, int b ) { setPoint( a, b ); } // Set x and y coordinates of Point public void setPoint( int a, int b ) { x = a; y = b; } // get x coordinate public int getX() { return x; } // get y coordinate public int getY() { return y; }

// convert the point into a String representation public String toString() { return "[" + x + ", " + y + "]"; } // return the area public double area() { return 0.0; } // return the volume public double volume() { return 0.0; } // return the class name public String getName() { return "Point"; } }

// Fig. 9.11: Circle.java // Definition of class Circle public class Circle extends Point { // inherits from Point protected double radius; // no-argument constructor public Circle() { // implicit call to superclass constructor here setRadius( 0 ); } // Constructor public Circle( double r, int a, int b ) { super( a, b ); // call the superclass constructor setRadius( r ); } // Set radius of Circle public void setRadius( double r ) { radius = ( r >= 0 ? r : 0 ); } // Get radius of Circle public double getRadius() { return radius; }

// Calculate area of Circle public double area() { return Math.PI * radius * radius; } // convert the Circle to a String public String toString() { return "Center = " + super.toString() + "; Radius = " + radius; } // return the class name public String getName() { return "Circle"; } }

// Fig. 9.11: Cylinder.java // Definition of class Cylinder public class Cylinder extends Circle { protected double height; // height of Cylinder // no-argument constructor public Cylinder() { // implicit call to superclass constructor here setHeight( 0 ); } // constructor public Cylinder( double h, double r, int a, int b ) { super( r, a, b ); // call superclass constructor setHeight( h ); } // Set height of Cylinder public void setHeight( double h ) { height = ( h >= 0 ? h : 0 ); } // Get height of Cylinder public double getHeight() { return height; }

// Calculate area of Cylinder (i.e., surface area) public double area() { return 2 * super.area() + 2 * Math.PI * radius * height; } // Calculate volume of Cylinder public double volume() { return super.area() * height; } // Convert a Cylinder to a String public String toString() { return super.toString() + "; Height = " + height; } // Return the class name public String getName() { return "Cylinder"; } }

// Fig. 9.11: Test.java // Driver for point, circle, cylinder hierarchy import javax.swing.JOptionPane; import java.text.DecimalFormat; public class Test { public static void main( String args[] ) { Point point = new Point( 7, 11 ); Circle circle = new Circle( 3.5, 22, 8 ); Cylinder cylinder = new Cylinder( 10, 3.3, 10, 10 ); Shape arrayOfShapes[]; arrayOfShapes = new Shape[ 3 ]; // aim arrayOfShapes[0] at subclass Point object arrayOfShapes[ 0 ] = point; // aim arrayOfShapes[1] at subclass Circle object arrayOfShapes[ 1 ] = circle; // aim arrayOfShapes[2] at subclass Cylinder object arrayOfShapes[ 2 ] = cylinder;

String output = point.getName() + ": " + point.toString() + "\n" + circle.getName() + ": " + circle.toString() + "\n" + cylinder.getName() + ": " + cylinder.toString(); DecimalFormat precision2 = new DecimalFormat( "0.00" ); // Loop through arrayOfShapes and print the name, // area, and volume of each object. for ( int i = 0; i < arrayOfShapes.length; i++ ) { output += "\n\n" + arrayOfShapes[ i ].getName() + ": " + arrayOfShapes[ i ].toString() + "\nArea = " + precision2.format( arrayOfShapes[ i ].area() ) + "\nVolume = " + precision2.format( arrayOfShapes[ i ].volume() ); } JOptionPane.showMessageDialog( null, output, "Demonstrating Polymorphism", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); }