Chapter 13 - Inheritance and Polymorphism The Object Class The equals Method The toString Method Polymorphism Dynamic Binding Compilation Details Polymorphism.

Slides:



Advertisements
Similar presentations
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Advertisements

METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
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.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
More about inheritance Exploring polymorphism 5.0.
Chapter 10 Classes Continued
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CMSC 202 Generics. Nov Generalized Code One goal of OOP is to provide the ability to write reusable, generalized code. Polymorphic code using.
What is inheritance? It is the ability to create a new class from an existing class.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 5, page 1 Sun Certified Java 1.4 Programmer Chapter 5 Notes Gary Lance
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Outline §Review of the last class l class variables and methods l method overloading and overriding §Inheritance and polymorphism l polymorphism l abstract.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
MIT AITI 2004 – Lecture 12 Inheritance. What is Inheritance?  In the real world: We inherit traits from our mother and father. We also inherit traits.
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
More about inheritance Exploring polymorphism 5.0.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Inheritance and Design CSIS 3701: Advanced Object Oriented Programming.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Interfaces and Inner Classes
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Inheritance ndex.html ndex.htmland “Java.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1)
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
OOP Basics Classes & Methods (c) IDMS/SQL News
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
1 Chapter 7 Object-Oriented Programming – Additional Details Object Creation - a Detailed Analysis Assigning a Reference Testing Objects For Equality.
CMSC 202 Polymorphism 2 nd Lecture. Aug 6, Topics Constructors and polymorphism The clone method Abstract methods Abstract classes.
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Object-Oriented Concepts
Chapter 11 Inheritance and Polymorphism
Computer Science II Exam 1 Review.
Chapter 13 - Inheritance and Polymorphism
Chapter 9 Inheritance and Polymorphism
Extending Classes.
Java Programming Language
Week 6 Object-Oriented Programming (2): Polymorphism
Inheritance Inheritance is a fundamental Object Oriented concept
Java Inheritance.
Chapter 9 Carrano Chapter 10 Small Java
Chapter 11 Inheritance and Polymorphism Part 1
Presentation transcript:

Chapter 13 - Inheritance and Polymorphism The Object Class The equals Method The toString Method Polymorphism Dynamic Binding Compilation Details Polymorphism with Arrays Abstract Methods And Classes protected Access Modifier 1

The Object Class The Object class is a superclass for all other classes. When declaring your own classes, you don't have to specify the Object class as a superclass - it's automatically a superclass. I'm only covering two of Object 's methods. The equals and toString methods are the most important Object methods.… 2

The equals Method For classes that don't have their own equals method, if objects from those classes are compared with the equals method, they inherit and use the Object class's equals method. The Object class's equals method returns true if the two reference variables that are being compared point to the same object; i.e., if the two reference variables contain the same address. 3

The equals Method Assuming that the Car class does not have its own equals method, what does this code fragment print? Car car1 = new Car("Honda"); Car car2 = car1; if ((car1.equals(car2) && (car1 == car2)) { System.out.println("cars are equal - first time"); } car2 = new Car("Honda"); if ((car1.equals(car2) || (car1 == car2)) { System.out.println("cars are equal - second time"); } Aside: the == operator works the same as the Object class's equals method; == returns true if the two reference variables point to the same object. 4

The equals Method Usually, the Object class's equals method is not good enough. You'll usually want to compare the contents of two objects rather than just whether two reference variables point to the same object. To do that, you'll need to have an equals method in the object's class definition that compares the contents of the two objects. 5

Defining Your Own equals Method Write an equals method for a Car class. Use this skeleton: public class Car { private String make; private int year; private String color; } // end class Car public class CarDriver { public static void main(String[] args) { Car car1 = new Car(); Car car2 = new Car(); if (car1.equals(car2)) { System.out.println("cars have identical features"); }... 6

The equals Method Note that equals methods are built into lots of Java's API classes. For example, the String class and the wrapper classes implement equals methods. As you'd expect, those equals methods test whether the contents of the two compared objects are the same (not whether the addresses of the two compared objects are the same). What does this code fragment print? String s1 = "hello", s2 = "he"; s2 += "llo"; if (s1 == s2) { System.out.println("\"==\" works"); } if (s1.equals(s2)) { System.out.println("\"equals\" works"); } 7

The toString Method The Object class's toString method returns a string that's a concatenation of the calling object's class name, sign, and a sequence of digits and letters (called a hashcode). Consider this code fragment: Object obj = new Object(); System.out.println(obj.toString()); Car car = new Car(); System.out.println(car.toString()); Here's the output: If a class is stored in a package, toString prefixes the class name with the class's package. The Object class is in the java.lang package. hashcode 8

The toString Method Retrieving the class name, sign, and a hashcode is usually worthless, so you'll almost always want to avoid calling the Object class's toString method and instead call an overriding toString method. In general, toString methods should return a string that describes the calling object's contents. You'll find lots of overriding toString methods in the Java API classes. For example, the Date class's toString method returns a Date object's month, day, year, hour, and second values as a single concatenated string. Since retrieving the contents of an object is such a common need, you should get in the habit of providing a toString method for most of your programmer-defined classes. Typically, your toString methods should simply concatenate the calling object's stored data and return the resulting string. Note that toString methods should not print the concatenated string value; they should just return it!!! 9

The toString Method Write a toString method for a Car class. Use this skeleton: public class Car { private String make; private int year; private String color;... } // end class Car public class CarDriver { public static void main(String[] args) { Car car = new Car("Honda", 1998, "silver"); System.out.println(car);... 10

The toString Method The toString method is automatically called when a reference variable is an argument in a System.out.println or System.out.print call. For example: System.out.println(car); The toString method is automatically called when a reference variable is concatenated (+ operator) to a string. For example: String carInfo = "Car data:\n" + car; Note that you can also call an object's toString method using the standard method-call syntax. For example: car.toString(); 11

The toString Method Write a toString method for a Counter class. Use this skeleton: public class Counter { private int count;... } // end class Counter public class CounterDriver { public static void main(String[] args) { Counter counter = new Counter(100); String message = "Current count = " + counter;... 12

Wrapper Classes' toString Methods All the primitive wrapper classes have toString methods that return a string representation of the given primitive value. For example: Integer.toString(22): evaluates to string "22" Double.toString(123.45): evaluates to string "123.45" 14

Polymorphism Polymorphism is when different types of objects respond differently to the same method call. To implement polymorphic behavior, declare a general type of reference variable that is able to refer to objects of different types. To declare a "general type of reference variable," use a superclass. Later, we'll use a programmer-defined superclass. For now, we'll keep things simple and use the predefined Object superclass. In the following Pets program, note how obj is declared to be an Object and note how the obj.toString() method call exhibits polymorphic behavior: If obj contains a Dog object, toString returns "Woof! Woof!" If obj contains a Cat object, toString returns "Meow! Meow!" 15

Polymorphism import java.util.Scanner; public class Pets { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); Object obj; System.out.print("Which type of pet do you prefer?\n" + "Enter d for dogs or c for cats: "); if (stdIn.next().equals("d")) { obj = new Dog(); } else { obj = new Cat(); } System.out.println(obj.toString()); System.out.println(obj); } // end main } // end Pets class Declare obj as a generic Object. Polymorphic method call. 16

Polymorphism public class Dog { public String toString() { return "Woof! Woof!"; } } // end Dog class public class Cat { public String toString() { return "Meow! Meow!"; } } // end Cat class 17

Dynamic Binding Polymorphism is a concept. Dynamic binding is a description of how that concept is implemented. More specifically, polymorphism is when different types of objects respond differently to the exact same method call. Dynamic binding is what the JVM does in order to match up a polymorphic method call with a particular method. We'll now describe how that "matching up" process works. Just before the JVM executes a method call, it looks at the method call's calling object. More specifically, it looks at the type of the object that's been assigned into the calling object's reference variable. If the assigned object is from class X, the JVM binds class X's method to the method call. If the assigned object is from class Y, the JVM binds class Y's method to the method call. After the JVM binds the appropriate method to the method call, the JVM executes the bound method. 18

Dynamic Binding Compilation Details If Dog implements a display method that prints "I'm a dog", would the following code work? Object obj = new Dog(); obj.display(); Be aware of these compiler issues when dynamic binding takes place: 1. When the compiler sees a method call,. (), it checks to see if the reference variable's class contains a method definition for the called method. 2. Normally, when you assign an object into a reference variable, the object's class and the reference variable's class are the same. But note in the above example how an object of type Dog is assigned into a reference variable of type Object. Such assignments only work if the right side's class is a subclass of the left side's class. 19

Polymorphism with Arrays The real usefulness of polymorphism comes when you have an array of generic reference variables and assign different types of objects to different elements in the array. That allows you to step through the array and for each array element, you call a polymorphic method. At runtime, the JVM uses dynamic binding to pick out the particular methods that apply to the different types of objects that are in the array. To illustrate polymorphism with arrays, we present a payroll program that stores payroll data in an employees array…. 20

Polymorphism with Arrays UML class diagram for the Payroll program: 21

Polymorphism with Arrays public class Payroll { public static void main(String[] args) { Employee[] employees = new Employee[100]; int day; // day of week (Sun=0, Mon=1,..., Sat=6) Hourly hourly; employees[0] = new Hourly("Aza", 25.00); employees[1] = new Salaried("Ben", 48000); employees[2] = new Hourly("Joby", 20.00); 22

Polymorphism with Arrays // This driver arbitrarily assumes that the payroll's month // starts on a Tuesday (day = 2) and contains 30 days. day = 2; for (int date=1; date<=30; date++) { day++; // change to the next day of the week day %= 7; // causes day of week to cycle from 0-6 repeatedly // Loop through all the employees for (int i=0; i<employees.length && employees[i] != null; i++) { 23

Polymorphism with Arrays if (day > 0 && day < 6 && employees[i] instanceof Hourly) { hourly = (Hourly) employees[i]; hourly.addHours(8); } The instanceof operator returns true if the object at its left is an instance of the class at its right. The cast operator is necessary because without it, you'd get a compilation error. Why? Because we're attempting to assign a superclass object into a subclass reference variable (employees is defined with an Employee superclass type and hourly is defined with an Hourly subclass type). If you want to assign a superclass object into a superclass reference variable, you can do it, but only if the "superclass object" really contains a subclass object and you include a cast operator. 24

Polymorphism with Arrays // Print hourly employee paychecks on Fridays. // Print salaried employee paychecks on 15th and 30th. if ((day == 5 && employees[i] instanceof Hourly) || (date%15 == 0 && employees[i] instanceof Salaried)) { employees[i].printPay(date); } } // end for i } // end for date } // end main } // end class Payroll 25

Polymorphism with Arrays public class Employee { private String name; //***************************************************** public Employee(String name) { this.name = name; } //***************************************************** public void printPay(int date) { System.out.printf("%2d %10s: %8.2f\n", date, name, getPay()); } // end printPay //***************************************************** // This dummy method is needed to satisfy the compiler. public double getPay() { System.out.println("error! in dummy"); return 0.0; } // end getPay } // end class Employee This method never executes; it's provided to satisfy the compiler. polymorphic method call 26

Polymorphism with Arrays public class Salaried extends Employee { private double salary; //*********************************************************** public Salaried(String name, double salary) { super(name); this.salary = salary; } // end constructor //*********************************************************** public double getPay() { return this.salary / 24; } // end getPay } // end class Salaried 27

Polymorphism with Arrays public class Hourly extends Employee { private double hourlyRate; private double hours = 0.0; //*********************************************************** public Hourly(String name, double rate) { super(name); hourlyRate = rate; } // end constructor //*********************************************************** public double getPay() { double pay = hourlyRate * hours; hours = 0.0; return pay; } // end getPay //*********************************************************** public void addHours(double hours) { this.hours += hours; } // end addHours } // end class Hourly 28

abstract Methods and Classes Declare a method to be abstract if the method's class is a superclass and the method is merely a "dummy" method for an overriding method(s) in a subclass(es). Java requires that when you define a method to be abstract, you must: Use an abstract method heading instead of a method definition. An abstract method heading is the same as a standard method heading except that it includes the abstract modifier and a trailing semicolon. Define an overriding version of that method in each of the superclass's subclasses. Define the superclass to be abstract by using the abstract modifier. In defining a class to be abstract, you're telling the compiler to not allow the class to be instantiated; i.e., if a program attempts to instantiate an abstract class, a compilation error is generated. 29

abstract Methods and Classes public abstract class Employee { private String name; public abstract double getPay(); //***************************************************** public Employee(String name) { this.name = name; } //***************************************************** public void printPay(int date) { System.out.printf("%2d %10s: %8.2f\n", date, name, getPay()); } // end printPay } // end class Employee 30

protected Access Modifier The protected access modifier is in between the public and private modifiers in terms of how accessible something is. protected members (instance variables, class variables, and methods) can only be accessed by classes within the subtree associated with the member's class. 31

protected Access Modifier When to use the protected access modifier: When you want a member to be directly accessible from any class within its subtree of classes, but you don't want it to be accessible elsewhere. Example: Suppose that the Employee class contains an employeeId instance variable and all of the descendant classes need to access it. To make access easier for the descendant classes, declare employeeId with the protected access modifier: protected int employeeId; Then, the descendant classes can access employeeId directly rather than having to use a getId() accessor method call. 32