COS 260 DAY 19 Tony Gauvin.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Inheritance. Class Relationships Composition: A class contains objects of other class(es) (actually, references to such objects) –A “has a” relationship.
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
More about inheritance Exploring polymorphism. 02/12/2004Lecture 8: More about inheritance2 Main concepts to be covered method polymorphism static and.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
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.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Programming Fundamentals 2: Inheritance/ F II Objectives – –the use of super, overriding, method polymorphism (dynamic binding), protected.
Improving structure with inheritance Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables Objects First with Java.
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.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Topic 4 Inheritance.
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and.
Objects First With Java A Practical Introduction Using BlueJ Method overriding 2.0.
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.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Creating Classes from Other Classes Appendix D © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Session 06: C# OOP-3 Inheritance and Polymorphism. Static and dynamic type of an object. FEN AK - IT: Softwarekonstruktion.
(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.
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)
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.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
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.
Polymorphism 1. Reuse of code: every time a new sub-class is defined, programmers are reusing the code in a super-class. All non-private members of a.
Understanding class definitions Exploring source code 6.0.
Comp1004: Inheritance II Polymorphism. Coming up Inheritance Reminder Overriding methods – Overriding and substitution Dynamic Binding Polymorphism –
Improving structure with inheritance Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables © 2017 Pearson Education,
9 Improving structure with inheritance
Lecture 12 Inheritance.
Chapter 11 Inheritance and Polymorphism
More about inheritance
10 More about inheritance
More about inheritance
COS 260 DAY 17 Tony Gauvin.
COS 260 DAY 19 Tony Gauvin.
Understanding class definitions
COS 260 DAY 9 Tony Gauvin.
COS 260 DAY 13 Tony Gauvin.
Understanding class definitions
CSE 143 Lecture 22 The Object class; Polymorphism read
Lecture 19 - Inheritance (Contd).
More about inheritance
COS 260 DAY 10 Tony Gauvin.
COS 260 DAY 18 Tony Gauvin.
CSE 143 Lecture 22 The Object class; Polymorphism read
Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”
Generic Classes and Methods
COS 260 DAY 19 Tony Gauvin.
COS 260 DAY 11 Tony Gauvin.
COS 260 DAY 23 Tony Gauvin.
COS 260 DAY 14 Tony Gauvin.
COS 260 DAY 4 Tony Gauvin.
COS 260 DAY 23 Tony Gauvin.
F II 8. More on Inheritance Objectives
Presentation transcript:

COS 260 DAY 19 Tony Gauvin

Agenda More about Inheritance Mini quiz 8 Graded Assignment 5 Due Questions? Assignment 4 Not corrected yet Top priority Assignment 5 Due Assignment 6 posted Due Nov 13 Mini quiz 8 Graded Great results Mini Quiz 9 will be Nov 17 No class on November 10 – Veteran’s Day observance Capstone progress report OVERDue More about Inheritance

Capstone Project Capstone Project Description Fall 2017.pdf October 2 Proposal Due E-mailed to Tony in Blackboard October 27 Progress Report E-mailed to Tony in Blackboard November 16 Progress Report E-mailed to Tony in Blackboard November 30 Progress Report E-mailed to Tony in Blackboard December 12@8AM All Deliverables & Presentation Due Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

More about inheritance Objects First with Java More about inheritance Exploring polymorphism 6.0 © David J. Barnes and Michael Kölling

Main concepts to be covered method polymorphism static and dynamic type overriding dynamic method lookup protected access © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

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

The inheritance hierarchy © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Conflicting output What we want What we have Leonardo da Vinci Had a great idea this morning. But now I forgot what it was. Something to do with flying ... 40 seconds ago - 2 people like this. No comments. Alexander Graham Bell [experiment.jpg] I think I might call this thing 'telephone'. 12 minutes ago - 4 people like this. What we want Leonardo da Vinci 40 seconds ago - 2 people like this. No comments. Alexander Graham Bell 12 minutes ago - 4 people like this. What we have © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

The problem The display method in Post only prints the common fields. Inheritance is a one-way street: A subclass inherits the superclass fields. The superclass knows nothing about its subclass’s fields. Network – V2 © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Attempting to solve the problem Place display where it has access to the information it needs. Each subclass has its own version. But Post’s fields are private. NewsFeed cannot find a display method in Post. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Static type and dynamic type A more complex type hierarchy requires further concepts to describe it. Some new terminology: static type dynamic type method dispatch/lookup © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Static and dynamic type What is the type of c1? Car c1 = new Car(); What is the type of v1? Vehicle v1 = new Car(); © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Static and dynamic type The declared type of a variable is its static type. The type of the object a variable refers to is its dynamic type. The compiler’s job is to check for static-type violations. for(Post post : posts) { post.display(); // Compile-time error. } © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Overriding: the solution display method in both super- and subclasses. Satisfies both static and dynamic type checking. Ex 11.2 © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Overriding Superclass and subclass define methods with the same signature. Each has access to the fields of its class. Superclass satisfies static type check. Subclass method is called at runtime – it overrides the superclass version. What becomes of the superclass version? © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Distinct static and dynamic types © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Method lookup No inheritance or polymorphism. The obvious method is selected. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Method lookup Inheritance but no overriding. The inheritance hierarchy is ascended, searching for a match. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Method lookup Polymorphism and overriding. The ‘first’ version found is used. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Method lookup summary The variable is accessed. The object stored in the variable is found. The class of the object is found. The class is searched for a method match. If no match, the superclass is searched. This is repeated until a match is found, or the class hierarchy is exhausted. Overriding methods take precedence – they override inherited copies. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Super call in methods Overridden methods are hidden ... ... but we often still want to be able to call them. An overridden method can be called from the method that overrides it. super.method(...) Compare with the use of super in constructors. EX 11.3 © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Calling an overridden method public void display() { super.display(); System.out.println(" [" + filename + "]"); System.out.println(" " + caption); } © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Method polymorphism We have been discussing polymorphic method dispatch. A polymorphic variable can store objects of varying types. Method calls are polymorphic. The actual method called depends on the dynamic object type. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

The instanceof operator Used to determine the dynamic type. Identifies ‘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 … } © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

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.  ClassName @ hashcode © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Overriding toString in Post 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"; return text + " " + comments.size() + " comment(s). Click here to view.\n"; © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

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); © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

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(); © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

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. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Overriding equals 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 } © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Overriding equals in Student 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; © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Overriding hashCode in Student /** * 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; } © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

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. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Access levels © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

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. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.