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.

Slides:



Advertisements
Similar presentations
OOP (Java): Inheritance/ OOP Objectives – –to introduce inheritance, superclasses, subclasses, polymorphic data structures, and wrapper.
Advertisements

Inheritance in collections Week Main concepts to be covered Inheritance in ArrayList objects Subtyping Substitution.
Objects First With Java A Practical Introduction Using BlueJ Improving structure with inheritance 2.0.
Improving structure with inheritance Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Chapter 8 Improving Structure with Inheritance. The DoME Example The Database of Multimedia Entertainment We will be storing information about CDs and.
Using interfaces Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling How would you find the maximum.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Improving structure with inheritance (Chapters 8 and 9)
Improving structure with inheritance
Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.
Using Collections. Review of Collections Using an ArrayList It increases its capacity as necessary. It keeps a private count ( size() accessor). It keeps.
Inheritance Chapter 8.
CPSC150 Interfaces Chapter CPSC150 Inheritance Review No different than any other class. Has no access to or information about subclasses class.
More about inheritance Exploring polymorphism 5.0.
More about inheritance Exploring polymorphism 3.0.
15-Jul-15 Generics. ArrayList s and arrays A ArrayList is like an array of Object s, but... Arrays use [ ] syntax; ArrayList s use object syntax An ArrayList.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
Improving structure with inheritance Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables Objects First with Java.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
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.
Programming Fundamentals 2: Inheritance/ F II Objectives – –to introduce inheritance, superclasses, subclasses, polymorphic data structures,
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
COS 260 DAY 5 Tony Gauvin.
Objects First With Java A Practical Introduction Using BlueJ Supplementary Material for Java
More about inheritance Exploring polymorphism 5.0.
Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Final Review. From ArrayLists to Arrays The ArrayList : used to organize a list of objects –It is a class in the Java API –the ArrayList class uses an.
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 COS 260 DAY 10 Tony Gauvin. 2 Agenda Questions? 4 th Mini quiz Today –Chapter 4 Assignment 2 Due Capstone Discussion Proposals Due Oct 15 No class on.
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.
Composition When one class contains an instance variable whose type is another class, this is called composition. Instead of inheritance, which is based.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
Peyman Dodangeh Sharif University of Technology Fall 2014.
November 27, 2001Lecture 231  Previous Lecture: Parameter passing Method overloading  Today’s Lecture: Introduction to inheritance Class diagrams and.
1 COS 260 DAY 12 Tony Gauvin. 2 Agenda Questions? 5 th Mini quiz –Chapter 5 40 min Assignment 3 Due Assignment 4 will be posted later (next week) –If.
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.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. 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)
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
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,
Coming up ArrayList ArrayList vs Array – Declaration – Insertion – Access – Removal Wrapper classes Iterator object.
Coming up Inheritance – Code duplication – Super classes – Constructors Polymorphic collections – “Anywhere a super class is, a sub class can go” Casting.
More-Sophisticated Behavior
Programming in Java: lecture 7
9 Improving structure with inheritance
More about inheritance
COS 260 DAY 19 Tony Gauvin.
Continuing Chapter 11 Inheritance and Polymorphism
COS 260 DAY 19 Tony Gauvin.
COS 260 DAY 11 Tony Gauvin.
Extending Classes.
COS 260 DAY 11 Tony Gauvin.
More about inheritance
COS 260 DAY 18 Tony Gauvin.
COS 260 DAY 23 Tony Gauvin.
Objects First with Java
COS 260 DAY 23 Tony Gauvin.
COS 260 DAY 6 Tony Gauvin.
Chapter 11 Inheritance and Polymorphism Part 2
Review: libraries and packages
Improving structure with inheritance
Advanced Programming in Java
Presentation transcript:

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 5 Posted –Due November 19 Capstone progress reports over due –Next progress report due November 12 –Timing of proposal and reports is 10% of project grade Finish Discussion on Improving Structure with Inheritance

Improving structure with inheritance 5.0

4 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

5 Review (so far) Inheritance (so far) helps with: Avoiding code duplication Code reuse Easier maintenance Extendibility Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

6 Define Inheritance Superclass Subclass Hierarchy Subtype Parent class Child class Is-a Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

7 Subclasses and subtyping Classes define types. Subclasses define subtypes. Objects of subclasses can be used where objects of supertypes are required. (This is called substitution.) Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

8 Subtyping and assignment Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Vehicle v1 = new Vehicle();Vehicle v2 = new Car();Vehicle v3 = new Bicycle(); subclass objects may be assigned to superclass variables

9 Invalid Subtyping Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Car c1 = new Vehicle(); Car c2 = new Bicycles(); These are errors c1 & c2 must be a type that is a Superclass of the assigned object

10 Subtyping and parameter passing Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public class NewsFeed { public void addPost(Post post) {... } PhotoPost photo = new PhotoPost(...); MessagePost message = new MessagePost(...); feed.addPost(photo); //photo  subtype of Post feed.addPost(message); //message  subtype of Post subclass objects may be used as actual parameters for the superclass

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

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

13 Polymorphic variables Object variables in Java are polymorphic. (They can hold objects of more than one type.) They can hold objects of the declared type, or of subtypes of the declared type. Polymorphic means “many forms”. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

14 Casting We can assign subtype to supertype … … but we cannot assign supertype to subtype! Vehicle v; Car c = new Car(); v = c; // correct c = v; // compile-time error! Casting fixes this : c = (Car) v; (only ok if the vehicle really is a Car!) Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

15 Casting An object type in parentheses. Used to overcome 'type loss'. The object is not changed in any way. A runtime check is made to ensure the object really is of that type: –ClassCastException if it isn't! Use it sparingly. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

16 The Object class Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling All classes inherit from Object.

17 The Object Class cs/api/java/lang/Object.html cs/api/java/lang/Object.html clone() equals(object obj) finalize() getClass() toString() wait().. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

18 Polymorphic collections All collections in JAVA are polymorphic. The elements could simply be of type Object. public void add(Object element) public Object get(int index) Usually avoided by using a type parameter with the collection. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

19 Polymorphic collections A type parameter limits the degree of polymorphism: ArrayList Collection methods are then typed. Without a type parameter, ArrayList is implied. Likely to get an “unchecked or unsafe operations” warning. More likely to have to use casts. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

20 Collections and primitive types Potentially, all objects can be entered into collections because collections can accept elements of type Object and all classes are subtypes of Object. Great! But what about the primitive types: int, boolean, etc.? Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

21 Wrapper classes Primitive types are not objects types. Primitive-type values must be wrapped in objects to be stored in a collection! Wrapper classes exist for all primitive types: Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling simple typewrapper class intInteger floatFloat charCharacter...

22 Wrapper classes Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling int i = 18; Integer iwrap = new Integer(i); … int value = iwrap.intValue(); wrap the value unwrap it In practice, autoboxing and unboxing mean we don't often have to do this explicitly

23 Autoboxing and unboxing Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling private ArrayList markList; … public void storeMark(int mark) { markList.add(mark); } int firstMark = markList.remove(0); autoboxing unboxing

24 Review Inheritance allows the definition of classes as extensions of other classes. Inheritance –avoids code duplication –allows code reuse –simplifies the code –simplifies maintenance and extending Variables can hold subtype objects. Subtypes can be used wherever supertype objects are expected (substitution). Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling