Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: "has a" CSC Employee. Supervisor

Similar documents
Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

Java Object Oriented Design. CSC207 Fall 2014

CS111: PROGRAMMING LANGUAGE II

Inheritance and Polymorphism

Java Programming Lecture 7

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

CS111: PROGRAMMING LANGUAGE II

1.00 Lecture 13. Inheritance

Inheritance & Polymorphism

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

Programming in C# Inheritance and Polymorphism

Java How to Program, 8/e

What is Inheritance?

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Inheritance and Polymorphism. CS180 Fall 2007

More Relationships Between Classes

Inheritance (Outsource: )

Object Oriented Programming. Java-Lecture 11 Polymorphism

Instance Members and Static Members

Basic Object-Oriented Concepts. 5-Oct-17

Big software. code reuse: The practice of writing program code once and using it in many contexts.

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

Chapter 14 Abstract Classes and Interfaces

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

First IS-A Relationship: Inheritance

Inheritance -- Introduction

Example: Count of Points

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

OVERRIDING. 7/11/2015 Budditha Hettige 82

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Object Oriented Programming Part II of II. Steve Ryder Session 8352 JSR Systems (JSR)

Inheritance. Transitivity

Need to store a list of shapes, each of which could be a circle, rectangle, or triangle

Accelerating Information Technology Innovation

Inheritance & Polymorphism. Object-Oriented Programming

Lab 2: Object-Oriented Design 12:00 PM, Jan 31, 2018

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

Inheritance. CSE 142, Summer 2002 Computer Programming 1.

ECE 122. Engineering Problem Solving with Java

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

COMP200 INTERFACES. OOP using Java, from slides by Shayan Javed

CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

Inheritance, polymorphism, interfaces

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

UML & OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 3 08/30/2011

Chapter 7. Inheritance

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Lecture 2 and 3: Fundamental Object-Oriented Concepts Kenneth M. Anderson

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

Classes and Inheritance Extending Classes, Chapter 5.2

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

Chapter 10 Classes Continued. Fundamentals of Java

Inheritance CSC9Y4. Inheritance

Admin. CS 112 Introduction to Programming. Recap: OOP Analysis. Software Design and Reuse. Recap: OOP Analysis. Inheritance

Inheritance & Polymorphism

CS-202 Introduction to Object Oriented Programming

ITI Introduction to Computing II

PROGRAMMING LANGUAGE 2

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

COMP 110/L Lecture 19. Kyle Dewey

Object Fundamentals Part Two. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 3 09/01/2009

ITI Introduction to Computing II

Lecture 5: Inheritance

Java and OOP. Part 3 Extending classes. OOP in Java : W. Milner 2005 : Slide 1

C08: Inheritance and Polymorphism

CS111: PROGRAMMING LANGUAGE II

COMP 110/L Lecture 20. Kyle Dewey

1.00 Lecture 14. Exercise: Plants

More on Inheritance. Interfaces & Abstract Classes

Abstract Classes and Interfaces

Inheritance and Polymorphism

CS313D: ADVANCED PROGRAMMING LANGUAGE

What are the characteristics of Object Oriented programming language?

Inheritance and Polymorphism

Rules and syntax for inheritance. The boring stuff

COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism

Binghamton University. CS-140 Fall Dynamic Types

XC Total Max Score Grader

INHERITANCE. Spring 2019

CS 112 Introduction to Programming

Programming in the Large II: Objects and Classes (Part 2)

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Data Abstraction. Hwansoo Han

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

Admin. q Admin and recap q Class inheritance

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

Exercise: Singleton 1

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

Polymorphism. Arizona State University 1

drawobject Circle draw

CSE 530A. ER Model. Washington University Fall 2013

Practice for Chapter 11

Transcription:

CSC 143 Object & Class Relationships Inheritance Reading: Ch. 10, 11 Relationships Between Real Things Man walks dog Dog strains at leash Dog wears collar Man wears hat Girl feeds dog Girl watches dog Dog eats food Man holds briefcase Dog bites man 1/7/2005 Original University of Washington; used by permission; modified by Vince Offenback 02-1 1/7/2005 Original University of Washington; used by permission; modified by Vince Offenback 02-2 Common Relationship Patterns A few types of relationships occur extremely often IS-A: a supervisor is an employee (and a taxpayer and a sister and a skier and... HAS-A: An airplane has seats (and lights and wings and engines and... These are so important and common that programming languages have special features to model them Some of these you know (maybe without knowing you know) Some of them we ll learn about in this course, starting now, with inheritance. is-a Supervisor Airplane has-a wings seats 1/7/2005 Original University of Washington; used by permission; modified by Vince Offenback 02-3 1/7/2005 Original University of Washington; used by permission; modified by Vince Offenback 02-4 Composition: "has a" Classes and objects can be related in several ways One way: composition, aggregation, or reference Dog has-a owner, dog has-a age, dog has-a name, etc. In java: one object refers to another object via an instance variable public class Dog { private String name;/ // this dog's name private int age; // this dog's age private Person owner; // this dog's owner private Dog mother, father; // this dog s parents private Color coatcolor; // etc, etc. Composition: One can think of the dog as "composed" of various objects Picturing the Relationships Dog Fido; //might be 6 years old, brown, owned by Marge, etc. Dog Apollo; //might be 2 years old, no owner, etc. In Java, it is a mistake to think of the parts of an object as being "inside" the whole. Fido name color age owner 1/7/2005 Original University of Washington; used by permission; modified by Vince Offenback 02-5 1/7/2005 Original University of Washington; used by permission; modified by Vince Offenback 02-6 CSC 143 02-1

Drawing Names and Objects Drawing Names and Objects Names and objects Very different things! In general, names refer to objects Objects can refer to other objects using instance variable names Fido (a name) refers to an object of type Dog age mother 6 another object of type Dog A name might not refer to any object One object might have more than one name i.e., might be more than one reference to it An object might not have any name anonymous Fido MyDoggie refers to age mother 6 anonymous object of type Dog another object of type Dog Fifi 1/7/2005 Original University of Washington; used by permission; modified by Vince Offenback 02-7 1/7/2005 Original University of Washington; used by permission; modified by Vince Offenback 02-8 Specialization "is a" Specialization relations can form classification hierarchies cats and dogs are special kinds of mammals; mammals and birds are special kinds of animals; animals and plants are special kinds of living things lines and triangles are special kinds of polygons; rectangles, ovals, and polygons are special kinds of shapes Keep in mind: Specialization is not the same as composition A cat "is-a" animal vs. a cat "has-a" owner "is-a" in Programming Classes (and interfaces) can be related via specialization one class/interface is a special kind of another class/interface Rectangle class is a kind of Shape The general mechanism for representing is- a is inheritance 1/7/2005 Original University of Washington; used by permission; modified by Vince Offenback 02-9 1/7/2005 Original University of Washington; used by permission; modified by Vince Offenback 02-10 Inheritance Java provides direct support for is- a relations likewise C++, C#, and other object-oriented languages Class inheritance one class can inherit from another class, meaning that it's is a special kind of the other Terminology Original class is called the base class or superclass Specializing class is called the derived class or subclass Inheritance: The Main Programming Facts Subclass inherits all instance variables and methods of the inherited class All instance variables and methods of the superclass are automatically part of the subclass Constructors are a special case (later) Subclass can add additional methods and instance variables Subclass can provide different versions of inherited methods 1/7/2005 Original University of Washington; used by permission; modified by Vince Offenback 02-11 1/7/2005 Original University of Washington; used by permission; modified by Vince Offenback 02-12 CSC 143 02-2

A B B extends A A's A's B's A's is automatically part of B Design Example: Database Suppose we want to generalize our example to handle a more realistic situation Application domain kinds of employees Hourly Exempt Boss 1/7/2005 Original University of Washington; used by permission; modified by Vince Offenback 02-13 1/7/2005 Original University of Washington; used by permission; modified by Vince Offenback 02-14 Design Process Step 1 Think up a class to model each kind of thing Design Process Step 2 Identify state/properties of each kind of thing 1/7/2005 Original University of Washington; used by permission; modified by Vince Offenback 02-15 1/7/2005 Original University of Washington; used by permission; modified by Vince Offenback 02-16 Design Process Step 3 Identify actions (behaviors) that each kind of thing can do Key Observation Many kinds of employees share common properties and actions We can factor common properties into a base class and use inheritance to create variations for specific classes 1/7/2005 Original University of Washington; used by permission; modified by Vince Offenback 02-17 1/7/2005 Original University of Washington; used by permission; modified by Vince Offenback 02-18 CSC 143 02-3

Generic s /** Representation of a generic employee. */ public class { // instance variables private String name; // employee name private int id; // employee id number /** Construct a new employee with the give name and id number */ public (String name, int id) { this.name = name; this.id = id; /** Return the name of this employee */ public String getname( ) { return name; /** Return the pay earned by this employee */ public double getpay( ) { return 0.0; //??? Specific Kinds of s Hourly public class Hourly extends { // additional instance variables private double hours; // hours worked private double hourlypay; // pay rate /** Return pay earned */ public double getpay( ) { return hours * hourlypay; Exempt public class Exempt extends { // additional instance variable private double salary; // weekly pay /** Return pay earned */ public double getpay( ) { return salary; 1/7/2005 Original University of Washington; used by permission; modified by Vince Offenback 02-19 1/7/2005 Original University of Washington; used by permission; modified by Vince Offenback 02-20 Hourly HourlyEmp. In Pictures Exempt ExemptEmp. 1/7/2005 Original University of Washington; used by permission; modified by Vince Offenback 02-21 More Java If class D extends B (inherits from) B... Class D inherits all methods and fields from class B But... "all" is too strong constructors are not inherited (but there is a way to use superclass constructors during object creation) same is true of static methods and static fields although these static members are still available in inherited part of the object technicalities we will look at later Class D may contain additional (new) methods and fields But has no way to delete any 1/7/2005 Original University of Washington; used by permission; modified by Vince Offenback 02-22 Never to be Forgotten If class D extends (inherits) from B... Every object of type D is also an object of type B a D can do anything that a B can do (because of inheritance) a D can be used in any context where a B is appropriate Method Overriding If class D extends B, class D may provide an alternative or replacement implementation of any method it would otherwise inherit from B The definition in D is said to override the definition in B An overriding method cannot change the number of arguments or their types, or the type of the result [why?] can only provide a different body (implementation) Can you override an instance variable? Not exactly... ask after class if you're really curious 1/7/2005 Original University of Washington; used by permission; modified by Vince Offenback 02-23 1/7/2005 Original University of Washington; used by permission; modified by Vince Offenback 02-24 CSC 143 02-4

Polymorphism Polymorphic: "having many forms" A variable that can refer to objects of different types is said to be polymorphic Methods with polymorphic arguments are also said to be polymorphic public void printpay( e) { System.out.println(e.getPay( )); Polymorphic methods can be reused for many types Static and Dynamic Types With polymorphism, we can distinguish between Static type: the declared type of the variable (never changes) Dynamic type: the run-time class of the object the variable currently refers to (can change as program executes) Legal assignment depends on static type compatibility If dynamic type really matches static type, can implement a runtime check for this with casts 1/7/2005 Original University of Washington; used by permission; modified by Vince Offenback 02-25 1/7/2005 Original University of Washington; used by permission; modified by Vince Offenback 02-26 Static and Dynamic Types Which of these are legal? Illegal? Can you fix any of these with casts? What are the static and dynamic types of the variables after assignments? Hourly bart = new Hourly(); Exempt homer = new Exempt(); marge = new () marge = homer ; homer = bart; homer = marge; Static? Dynamic? Dynamic Dispatch "Dispatch" refers to the act of actually placing a method in execution at run- time When types are static, the compiler knows exactly what method must execute When types are dynamic... the compiler knows the name of the method but there could be ambiguity about which version of the method will actually be needed at run- time In this case, the decision is deferred until run-time, and we refer to it as dynamic dispatch The chosen method is the one matching the dynamic (actual) type of the object 1/7/2005 Original University of Washington; used by permission; modified by Vince Offenback 02-27 1/7/2005 Original University of Washington; used by permission; modified by Vince Offenback 02-28 Method Lookup: How Dynamic Dispatch Works When a message is sent to an object, the right method to run is the one in the most specific class that the object is an instance of Makes sure that method overriding always has an effect Method lookup (a.k.a. dynamic dispatch) algorithm: Start with the actual run-time class (dynamic type) of the receiver object (not the static type!) Search that class for a matching method If one is found, invoke it Otherwise, go to the superclass, and continue searching Example: e = new Hourly() System.out.println(e); // Hourly tostring( ) e = new Exempt() System.out.println(e); // Exempt tostring( ) 1/7/2005 Original University of Washington; used by permission; modified by Vince Offenback 02-29 What about getpay( )? Got to include it in so polymorphic code can use it (why?) public double getpay( e) { But no implementation really makes sense Class doesn t contain pay instance variables So including an implementation of this in is really bogus /** Return the pay earned by this employee */ public double getpay( ) { return 0.0; //??? 1/7/2005 Original University of Washington; used by permission; modified by Vince Offenback 02-30 CSC 143 02-5

Abstract Methods and Classees An abstract method is one that is declared but not implemented in a class /** Return the pay earned by this employee */ public abstract double getpay( ) ; A class that contains any abstract method(s) must itself be declared abstract public abstract class { Instances of abstract classes cannot be created Because they are missing implementations of one or more methods Using Abstract Classes An abstract class is intended to be extended Extending classes can override abstract methods they inherit to provide actual implementations class Hourly extends { /** Return the pay of this Hourly */ public double getpay( ) { return hoursworked * payrate; Instances of these extended classes can be created A class that extends an abstract class without overriding all inherited abstract methods is itself abstract (and can be further extended) A class that is not abstract is often called a concrete class 1/7/2005 Original University of Washington; used by permission; modified by Vince Offenback 02-31 1/7/2005 Original University of Washington; used by permission; modified by Vince Offenback 02-32 Class Object Object is at the root of the Java class heirarchy Every class extends Object, either explicitly or implicitly If extends does not appear in a class declaration, extends Object is assumed implicitly These are equivalent public class { public class extends Object { Object includes a small number of methods appropriate for all objects tostring, equals, a few others These methods are inherited by all classes, but can be overridden often necessary or at least a good idea Summary Object- oriented programming is hugely important Lots of new concepts and terms Lots of new programming and modeling power Used widely in real programs Ideas (so far!) Composition ("has a") vs. specialization ("is a") Inheritance Method overriding Polymorphism, static vs. dynamic types Method lookup, dynamic dispatch Abstract classes and methods 1/7/2005 Original University of Washington; used by permission; modified by Vince Offenback 02-33 1/7/2005 Original University of Washington; used by permission; modified by Vince Offenback 02-34 CSC 143 02-6