CSC 520 – Advanced Object Oriented Programming, Fall, 2010 Thursday, September 30 Week 5, Generics and Inheritance Techniques, Meyer Ch. 10 & 16.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
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.
Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.
Generic programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Java Generics.
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java Generics. 2 The Dark Ages: Before Java 5 Java relied only on inclusion polymorphism  A polymorphism code = Using a common superclass Every class.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
C# Programming: From Problem Analysis to Program Design1 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
Chapter 10 Classes Continued
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 17 Animated Version Generics and Type Safety.
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
Programming Languages and Paradigms Object-Oriented Programming.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
Effective Java: Generics Last Updated: Spring 2009.
CSC 243 – Java Programming, Spring 2013 March 12, 2013 Week 7, Generics.
CMSC 202 Generics. Nov Generalized Code One goal of OOP is to provide the ability to write reusable, generalized code. Polymorphic code using.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
1 SystemVerilog Enhancement Requests Daniel Schostak Principal Engineer February 26 th 2010.
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.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Object-Oriented Programming Chapter Chapter
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Interfaces Chapter 9. 9 Creating Interfaces An interface is a contract. Every class that implements the interface must provide the interface’s defined.
1 CSE 331 Generics (Parametric Polymorphism) slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Introduction to Object-Oriented Programming Lesson 2.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Inheritance 2 Mehdi Einali Advanced Programming in Java 1.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Ceg860 (Prasad)L17IT1 Inheritance Techniques Subcontracting Anchored Types.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
CH10 Supplementary Material Prepared by Fatimah Alakeel Oct 2010.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
Java Generics. Lecture Objectives To understand the objective of generic programming To be able to implement generic classes and methods To know the limitations.
OOP Tirgul 10. What We’ll Be Seeing Today  Generics – A Reminder  Type Safety  Bounded Type Parameters  Generic Methods  Generics and Inner Classes.
CSC 243 – Java Programming, Fall, 2008 Tuesday, September 30, end of week 5, Interfaces, Derived Classes, and Abstract Classes.
Design issues for Object-Oriented Languages
The Object-Oriented Thought Process Chapter 03
Polymorphism in Methods
CSC 243 – Java Programming, Spring 2014
Inheritance and Polymorphism
Generics, Lambdas, Reflections
Section 11.1 Class Variables and Methods
CS240: Advanced Programming Concepts
Chapter 19 Generics Dr. Clincy - Lecture.
CSC 220 – Processing Spring, 2017
Presentation transcript:

CSC 520 – Advanced Object Oriented Programming, Fall, 2010 Thursday, September 30 Week 5, Generics and Inheritance Techniques, Meyer Ch. 10 & 16

Generic Interfaces / Classes We have been using generic interfaces and classes from the library. java.util.Set, java.util.HashSet java.util.Map, java.util.HashMap They allow containers to contain different types (classes) of objects. Generic parameters such as E (element type), K (key type) and V (value type) above are types bound by client code at compile time.

Using Generic Classes Client code supplies a binding for each generic parameter that must be an interface or class name. It cannot be a primitive type. A generic parameter may constrain the type of class or interface that client code can bind using a wildcard. ?, ? extends T, ? super T – details follow. These are Meyer’s constrained generics (16.4).

Building Generics A class or interface specifies its generic type. HashSet, HashMap, etc. ? extends T is a bounded wildcard, T or a subclass ? super T is a lower-bounded wildcard, T or a superclass ? is an unbounded wildcard, same as ? Extends Object The bound constrains client code type binding. The writer of a generic class must ensure type consistency within the class. The user can safely assume type compatibility if the compiler does not flag an error.

Erasure and Restrictions Unlike C++ template classes, the compiler generates only 1 copy of the class code for a generic class, erasing the generic type T information at run time. Every generic type T is treated as a java.lang.Object at run time. Writers of generic classes may need to cast to generic type T, E, K, V etc. in places. Writers of generic classes must ensure that those casts are valid. The compiler cannot ensure this.

Restrictions 1. No constructor – no “new T()” for generic T. 2. No “new T[]” – you must cast an “Object []” to a “T []” within the generic class code. This cast often fails at run time with a wildcard T. Make sure to test if you use arrays of wildcard generic types. 3. No generic T in a static field, static method or static initialization block of code. 4. Exception classes cannot be generic. parson/JavaLang/mapgame/Mymap.java

C++ Template classes A template class is a macro-like cookie cutter. The compiler (and possibly linker) create a distinct set of source methods in memory for every binding of the generic type (create all used methods for list, list, etc.) Cyclic cooperation between compiler and linker in the old days, went out the window with Microsoft. See STL reference.

Polymorphism Subclass polymorphism O-O inheritance allows a base interface or base class to take many forms in derived interfaces or classes Explicit parametric polymorphism Java, C++ or similar generic classes with type parameters. Implicit parametric polymorphism Python and other dynamically-typed languages allow multiple types to bind to operations and function parameters, return results, variables and fields.

Some Inheritance Issues from Meyer Ch. 16 Parents’ Invariant Rule The invariants of all the parents of a class apply to the class itself. Assertion Redeclaration Rule (1) A routine (method) redeclaration may only replace the original precondition by one equal or weaker, and the original postcondition by one equal or stronger. Universal Class Rule is Java’s java.lang.Object. Meyer’s frozen is Java final modifier, C++ const. Java and C++ provide class static methods and fields. Meyer also covers RTTI (run-time type identification) as related to guarded down casting.