Chapter FifteenModern Programming Languages1 A Second Look At Java.

Slides:



Advertisements
Similar presentations
Object Orientation Chapter SixteenModern Programming Languages, 2nd ed.1 Spring 2012.
Advertisements

Object Orientation Chapter SixteenModern Programming Languages, 2nd ed.1.
CS 2430 Day 28. Announcements Program 5 was posted (on Tuesday, 4/2/2013) Can work with a partner (sign up by today at 3:52pm) Exam 2 handed back on Monday.
A Second Look At Java Chapter FifteenModern Programming Languages, 2nd ed.1.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
1 Inheritance. 2 One class inherits from another if it describes a specialized subset of objects Terminology: inheritschild class subclass –the class.
1 Software Testing and Quality Assurance Lecture 12 - The Testing Perspective (Chapter 2, A Practical Guide to Testing Object-Oriented Software)
Object-Oriented PHP (1)
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
1 The Design of Class Mechanism for MOBY. 2 Earlier Software designers have to choose between the following schemes for the development Earlier Software.
1 Introduction to CS Agenda Syllabus Schedule Lecture: the management of complexity.
Chapter 10 Classes Continued
Interfaces and Inner Classes. What is an Interface?  What is “presented to the user”?  The public part of a class?  What is the substance of an interface?
Inheritance and Polymorphism CS351 – Programming Paradigms.
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
Subclasses and Subtypes CMPS Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Comparison of OO Programming Languages © Jason Voegele, 2003.
Abstract classes and Interfaces. Abstract classes.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
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.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
1 COMP313A Programming Languages Object Oriented Progamming Languages (3)
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
OO as a language for acm l OO phrase l Mental model of key concepts.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
CSSE501 Object-Oriented Development. Chapter 13: Multiple Inheritance  In this chapter we will investigate some of the problems that can arise when a.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Session 24 Chapter 12: Polymorphism. Polymorphism polymorphism comes from the Greek root for “many forms” polymorphism is about how we can use different.
Object-Oriented Programming Chapter Chapter
Inheritance Revisited Other Issues. Multiple Inheritance Also called combination--not permitted in Java, but is used in C++ Also called combination--not.
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
Object Oriented Programming
1. Perspectives on Design Principles – Semantic Invariants and Design Entropy Catalin Tudor 2.
Session 18 Lab 9 Re-cap, Chapter 12: Polymorphism & Using Sound in Java Applications.
Classes, Interfaces and Packages
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Overview of C++ Polymorphism
FEN 2014UCN Teknologi/act2learn1 Object-Oriented Programming “ The Three Pillars of OOP”: Encapsulation Inheritance Polymorphism The Substitution Principle.
Object-Oriented Programming “The Rest of the Story”, CS 4450 – Chapter 16.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Object Oriented Programming Lecture 3: Things OO.
CS 350 – Software Design The Decorator Pattern – Chapter 17 In this chapter we expand our e-commerce case study and learn how to use the Decorator Pattern.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
ISBN Chapter 12 Support for Object-Oriented Programming.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Modern Programming Languages
Inheritance and Polymorphism
Object Oriented Analysis and Design
Subtype Polymorphism, Subtyping vs
Object-Oriented Programming
Overview of C++ Polymorphism
Lecture 10 Concepts of Programming Languages
Presentation transcript:

Chapter FifteenModern Programming Languages1 A Second Look At Java

Chapter FifteenModern Programming Languages2 Subtype Polymorphism Does this declare x to be a reference to an object of the Person class? Not exactly—the type Person may include references to objects of other classes n Java has subtype polymorphism Person x;

Chapter FifteenModern Programming Languages3 Interfaces n A method prototype just gives the method name and type—no method body n An interface in Java is a collection of method prototypes public interface Drawable { void show(int xPos, int yPos); void hide(); }

Chapter FifteenModern Programming Languages4 Implementing Interfaces n A class can declare that it implements a particular interface Then it must provide public method definitions that match those in the interface – But interface method declarations are always implicitly public (and abstract)

Chapter FifteenModern Programming Languages5 Examples “Interface Inheritance” public class Icon implements Drawable { public void show(int x, int y) { … method body … } public void hide() { … method body … } …more methods and fields… } public class Square implements Drawable, Scalable { … all required methods of all interfaces implemented … }

Chapter FifteenModern Programming Languages6 Why Use Interfaces? n An interface can be implemented by many (unrelated) classes: n Interface name can be used as a reference type: public class Window implements Drawable … public class MousePointer implements Drawable … public class Oval implements Drawable … Drawable d; d = new Icon("i1.gif"); d.show(0,0); d = new Oval(20,30); d.show(0,0);

Chapter FifteenModern Programming Languages7 Interfaces as Capabilities n Implement multiple interfaces n Interface names are often adjectives – They describe capabilities n Example: AbleTest.java

Chapter FifteenModern Programming Languages8 Interface Inheritance n A commitment to implement a contract n No implementation is inherited n Disadvantage: – No code sharing n Advantage: – No code commitment – Freedom to implement any way you want

Chapter FifteenModern Programming Languages9 Extending Interfaces n An interface can extend another n The result is the union of all the declared methods interface PersistentStack extends Stack, Persistent {} class DynamicStack implements PersistentStack {…}

Chapter FifteenModern Programming Languages10 Inheritance n When a subclass extends its superclass, it inherits all its methods and fields – “Implementation Inheritance” – A lesser degree of separation of interface from implementation n (Nothing like this happens with interfaces—when a class implements an interface, all it gets is an obligation) n In addition to inheritance, you also get polymorphism

Chapter FifteenModern Programming Languages11 Inheritance Chains n A derived class can have more classes derived from it n All classes but one are derived from some class If you do not give an extends clause, Java supplies one: extends Object Object is the ultimate base class in Java

Chapter FifteenModern Programming Languages12 public class Icon { private int x,y; private int width; private int height; private Gif image; public void move (int newX, int newY) { x = newX; y = newY; } public Gif getImage() { return image; } } public class Label { private int x,y; private int width; private int height; private String text; public void move (int newX, int newY) { x = newX; y = newY; } public String getText() { return text; } } Two classes with a lot in common—but neither is a simple extension of the other.

Chapter FifteenModern Programming Languages13 public class Icon extends Graphic { private Gif image; public Gif getImage() { return image; } } public class Label extends Graphic { private String text; public String getText() { return text; } } Common code and data have been factored out into a common base class. public class Graphic { protected int x,y; protected int width,height; public void move(int newX, int newY) { x = newX; y = newY; } }

Chapter FifteenModern Programming Languages14 The Liskov Substitution Principle n An is-a relationship means that the subtype must be in all cases substitutable for a supertype – All depends on the methods involved – Think of the methods and their preconditions and postconditions as a contract – Subclasses must adhere to the contract – Can weaken the preconditions and/or strengthen postconditions (“require no more, promise no less”) n Don’t go crazy with inheritance n Prefer composition over inheritance

Chapter FifteenModern Programming Languages15 Extending And Implementing Classes can use extends and implements together n For every class, the Java language system keeps track of several properties, including: A: the interfaces it implements B: the methods it is obliged to define C: the methods that are defined for it D: the fields that are defined for it

Chapter FifteenModern Programming Languages16 Simple Cases For A Class n A method definition affects C only n A field definition affects D only An implements part affects A and B – All the interfaces are added to A – All the methods in them are added to B A: the interfaces it implements B: the methods it is obliged to define C: the methods that are defined for it D: the fields that are defined for it

Chapter FifteenModern Programming Languages17 Tricky Case For A Class An extends part affects all four: – All interfaces of the base class are added to A – All methods the base class is obliged to define are added to B – All methods of the base class are added to C – All fields of the base class are added to D A: the interfaces it implements B: the methods it is obliged to define C: the methods that are defined for it D: the fields that are defined for it

Chapter FifteenModern Programming Languages18 Multiple Inheritance n In some languages (such as C++) a class can have more than one base class n Seems simple at first: just inherit fields and methods from all the base classes n For example: a multifunction printer

Chapter FifteenModern Programming Languages19 Collision Problem n The different base classes are unrelated, and may not have been designed to be combined Scanner and Fax might both have a method named transmit When MultiFunction.transmit is called, what should happen?

Chapter FifteenModern Programming Languages20 Diamond Problem n A class may inherit from the same base class through more than one path If A defines a field x, then B has one and so does C Does D get two of them?

Chapter FifteenModern Programming Languages21 Solvable, But… n A language that supports multiple inheritance must have mechanisms for handling these problems n Can be tricky n The question is, is the additional power worth the additional language complexity? n Java’s designers did not think so

Chapter FifteenModern Programming Languages22 Living Without Multiple Inheritance One benefit of multiple inheritance is that a class can have several unrelated types (like Copier and Fax ) n This can be done in Java by using interfaces: a class can implement any number of interfaces n Another benefit is inheriting implementation from multiple base classes n This is harder to accomplish with Java

Chapter FifteenModern Programming Languages23 public class MultiFunction { private Printer myPrinter; private Copier myCopier; private Scanner myScanner; private Fax myFax; public void copy() { myCopier.copy(); } public void transmitScanned() { myScanner.transmit(); } public void sendFax() { myFax.transmit(); } … } Forwarding

Chapter FifteenModern Programming Languages24 Python MI Solution n When resolving the binding of an identifier: – 1) Look first in the object itself – 2) Then in its class – 3) Then in all base classes in declaration order – 4) Repeat 3 recursively as needed n Order of declaration of base classes matters!

Chapter FifteenModern Programming Languages25 Generics n Parametric polymorphism – Automatic in dynamically typed languages – (Why?) n Type variables in ML, Haskell n Templates in C++, D – Code generation n Generics in Java, C#, Ada, Eiffel – varying degrees of flexibility