1 COS 260 DAY 19 Tony Gauvin. 2 Agenda Questions? 8 th Mini Quiz not corrected yet 9 Th Mini Quiz next class –Due November 19 Finish Discussion on More.

Slides:



Advertisements
Similar presentations
Understanding class definitions Looking inside classes 5.0.
Advertisements

Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
Inheritance. Class Relationships Composition: A class contains objects of other class(es) (actually, references to such objects) –A “has a” relationship.
Using interfaces Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling How would you find the maximum.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Improving structure with inheritance (Chapters 8 and 9)
Improving structure with inheritance
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
More about inheritance Exploring polymorphism. 02/12/2004Lecture 8: More about inheritance2 Main concepts to be covered method polymorphism static and.
CS2200 Software Development Lecture: Object class A. O’Riordan, 2008.
String Concatenation (operator overloading) 3.0.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Understanding class definitions – Part II –. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
CS102--Object Oriented Programming Review 1: Chapter 1 – Chapter 7 Copyright © 2008 Xiaoyan Li.
More about inheritance Exploring polymorphism 5.0.
1 Reflecting on the ticket machines Their behavior is inadequate in several ways: –No checks on the amounts entered. –No refunds. –No checks for a sensible.
More about inheritance Exploring polymorphism 3.0.
CC1007NI: Further Programming Week 2 Dhruba Sen Module Leader (Islington College)
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.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Improving structure with inheritance Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables Objects First with Java.
1 COS 260 DAY 2 Tony Gauvin. 2 Agenda Questions? Class roll call Blackboard Web Resources Objects and classes 1 st Mini quiz on chap1 terms and concepts.
Improving structure with inheritance Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables Objects First with Java.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Understanding class definitions
1 COS 260 DAY 3 Tony Gauvin. 2 Agenda Questions? 1 st Mini quiz on chap1 terms and concepts –Today In BlackBoard –30 min., M/C and short answer, open.
Overriding toString()
More about inheritance Exploring polymorphism 5.0.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
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.
1 COS 260 DAY 14 Tony Gauvin. 2 Agenda Questions? 6 th Mini quiz graded  Oct 29 –Chapter 6 Assignment 4 will be posted later Today –First two problems.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
1 COS 260 DAY 21 Tony Gauvin. 2 Agenda Questions? 8 th Mini Quiz corrected –Good results 9 Th Mini Quiz Today –40 min covering Chap 9 Assignment 5 Due.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
CSC142 NN 1 CSC 142 Overriding methods from the Object class: equals, toString.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
1 COS 260 DAY 17 Tony Gauvin. 2 Agenda Questions? 7 th Mini quiz –Chapter 7 –Password “GoBengals” –40 min Assignment 4 posted –Due Nov 9 (one week) Capstone.
1 COS 260 DAY 22 Tony Gauvin. 2 Agenda Questions? 9 th Mini Quiz corrected –Good results Assignment 5 Not corrected yet Assignment 6 Posted (one more)
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
1 COS 260 DAY 18 Tony Gauvin. 2 Agenda Questions? 7 th Mini quiz Graded –Good results 8 th Mini Quiz –Chap 8  Next class Assignment 4 Due Assignment.
Comp1004: Inheritance II Polymorphism. Coming up Inheritance Reminder Overriding methods – Overriding and substitution Dynamic Binding Polymorphism –
Object-oriented Programming in Java
Chapter 11 Inheritance and Polymorphism
More about inheritance
10 More about inheritance
More about inheritance
Lecture 17: Polymorphism (Part II)
COS 260 DAY 19 Tony Gauvin.
Understanding class definitions
COS 260 DAY 19 Tony Gauvin.
COS 260 DAY 11 Tony Gauvin.
More about inheritance
COS 260 DAY 18 Tony Gauvin.
Generic Classes and Methods
Overriding methods from the Object class: equals, toString
Understanding class definitions
COS 260 DAY 23 Tony Gauvin.
COS 260 DAY 4 Tony Gauvin.
Lecture 18: Polymorphism (Part II)
COS 260 DAY 23 Tony Gauvin.
Chapter 8 Class Inheritance and Interfaces
Presentation transcript:

1 COS 260 DAY 19 Tony Gauvin

2 Agenda Questions? 8 th Mini Quiz not corrected yet 9 Th Mini Quiz next class –Due November 19 Finish Discussion on More on Inheritance

3 Final Countdown Nov 16 –Finish Chap 9 Nov 19 –9 th Mini Quiz –Assignment 5 due –Begin Chap 10 Nov 23 –Finish Chapter 10 Nov 30 –10 th Mini quiz –Begin Chap 11 Dec 3 –Finish Chapter 11 –Assignment 6 due Dec 7 –11 th mini quiz –Begin Chapter 12 Dec 10 –Finish Chapter 12 –Assignment 7 Due Dec 10 AM –12 th mini quiz –Capstone Presentations Copyright © 2014 Pearson Education, Inc. Slide 1-3

4 Change to Grade Calculations 12 instead of 11 mini quizzes –Still best 3% each 7 instead of 8 assignments –AVG(7) = 40% of final grade –Was 5% each Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

More about inheritance Exploring polymorphism 5.0

6 Question What is the difference between static type and dynamic type in JAVA variable assignments? What is method polymorphism> Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

7 The instanceof operator Used to determine the dynamic type. Recovers ‘lost’ type information. Usually precedes assignment with a cast to the dynamic type: if(post instanceof MessagePost) { MessagePost msg = (MessagePost) post; … access MessagePost methods via msg … } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

8 The Object class’s methods Methods in Object are inherited by all classes. Any of these may be overridden. The toString method is commonly overridden: –public String toString() –Returns a string representation of the object. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

9 Overriding toString in Post Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public String toString() { String text = username + "\n" + timeString(timestamp); if(likes > 0) { text += " - " + likes + " people like this.\n"; } else { text += "\n"; } if(comments.isEmpty()) { return text + " No comments.\n"; } else { return text + " " + comments.size() + " comment(s). Click here to view.\n"; }

10 Overriding toString Explicit print methods can often be omitted from a class: System.out.println(post.toString()); Calls to println with just an object automatically result in toString being called: System.out.println(post); Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

11 StringBuilder Consider using StringBuilder as an alternative to concatenation: StringBuilder builder = new StringBuilder(); builder.append(username); builder.append('\n'); builder.append(timeString(timestamp)); … return builder.toString(); Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

12 Object equality What does it mean for two objects to be ‘the same’? –Reference equality. –Content equality. Compare the use of == with equals() between strings. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

13 Overriding equals Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public boolean equals(Object obj) { if(this == obj) { return true; } if(!(obj instanceof ThisType)) { return false; } ThisType other = (ThisType) obj; … compare fields of this and other }

14 Overriding equals in Student Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public boolean equals(Object obj) { if(this == obj) { return true; } if(!(obj instanceof Student)) { return false; } Student other = (Student) obj; return name.equals(other.name) && id.equals(other.id) && credits == other.credits; }

15 Overriding hashCode in Student Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling /** * Hashcode technique taken from * Effective Java by Joshua Bloch. */ public int hashCode() { int result = 17; result = 37 * result + name.hashCode(); result = 37 * result + id.hashCode(); result = 37 * result + credits; return result; }

16 Protected access Private access in the superclass may be too restrictive for a subclass. The closer inheritance relationship is supported by protected access. Protected access is more restricted than public access. We still recommend keeping fields private. –Define protected accessors and mutators. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

17 Access levels Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

18 Review The declared type of a variable is its static type. –Compilers check static types. The type of an object is its dynamic type. –Dynamic types are used at runtime. Methods may be overridden in a subclass. Method lookup starts with the dynamic type. Protected access supports inheritance. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling