relationships between real things
play

Relationships Between Real Things Man walks dog Dog strains at - PowerPoint PPT Presentation

Relationships Between Real Things Man walks dog Dog strains at leash Dog wears collar CSE 143 Man wears hat Girl feeds dog Girl watches dog Object & Class Relationships Inheritance Dog eats food Man holds


  1. Relationships Between Real Things • Man walks dog • Dog strains at leash • Dog wears collar CSE 143 • Man wears hat • Girl feeds dog • Girl watches dog Object & Class Relationships – Inheritance • Dog eats food • Man holds briefcase Reading: Ch. 9, 14 • Dog bites man 10/9/2003 (c) 2001-3, University of Washington 02-1 10/9/2003 (c) 2001-3, University of Washington 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 .... Employee Airplane • HAS-A: An airplane has seats (and lights and wings and engines and... has-a • These are so important and common that programming is-a wings seats languages have special features to model them • Some of these you know (maybe without knowing you know) Supervisor • Some of them we’ll learn about in this course, starting now, with inheritance. 10/9/2003 (c) 2001-3, University of Washington 02-3 10/9/2003 (c) 2001-3, University of Washington 02-4 CSE143 Au03 02-1

  2. Composition: "has a" Picturing the Relationships • Classes and objects can be related in several ways • Dog Fido; //might be 6 years old, brown, owned by • One way: composition , aggregation , or reference Marge, etc. • Dog has-a owner, dog has-a age, dog has-a name, etc. • Dog Apollo; //might be 2 years old, no owner, etc. • In java: one object refers to another object • via an instance variable • In Java, it is a mistake to think of the parts of an object as being "inside" the whole. public class Dog { private String name;/ // this dog's name Fido private int age; //this dog's age private Person owner; // this dog's owner private Dog mother, father; // this dog’s parents age name private Color coatColor; //etc, etc. } owner • One can think of the dog as "composed" of various objects: color "composition" 10/9/2003 (c) 2001-3, University of Washington 02-5 10/9/2003 (c) 2001-3, University of Washington 02-6 Drawing Names and Objects Drawing Names and Objects • Names and objects • A name might not refer to any object anonymous object of type • Very different things! • One object might have more than one name Dog • In general, names refer to objects • i.e., might be more than one reference to it • Objects can refer to other objects using instance variable • An object might not have any name names • “anonymous” 6 an object of Fido (a name) 6 another type Dog an object of object of type another Fido age (instance Dog object of type refers to type Dog Dog var. name) mother refers to age MyDoggie (instance var. name) mother Fifi 10/9/2003 (c) 2001-3, University of Washington 02-7 10/9/2003 (c) 2001-3, University of Washington 02-8 CSE143 Au03 02-2

  3. Specialization – "is a" "is-a" in Programming • Specialization relations can form classification • Classes (and interfaces) can be related via specialization hierarchies • one class/interface is a special kind of another class/interface • cats and dogs are special kinds of mammals; • Rectangle class is a kind of Shape mammals and birds are special kinds of animals; • The general mechanism for representing “is-a” is animals and plants are special kinds of living things inheritance • lines and triangles are special kinds of polygons; • Java interfaces are a special case of this 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 10/9/2003 (c) 2001-3, University of Washington 02-9 10/9/2003 (c) 2001-3, University of Washington 02-10 Inheritance Inheritance: The Main Programming Facts • Java provides direct support for “is-a” relations • Subclass inherits all instance variables and methods of the inherited class • likewise C++, C#, and other object-oriented languages • All instance variables and methods of the superclass are • Class inheritance automatically part of the subclass • one class can inherit from another class, • Constructors are a special case (later) meaning that it's is a special kind of the other • Subclass can add additional methods and instance • Terminology variables • Original class is called the base class or superclass • Subclass can provide different versions of inherited • Specializing class is called the derived class or subclass methods 10/9/2003 (c) 2001-3, University of Washington 02-11 10/9/2003 (c) 2001-3, University of Washington 02-12 CSE143 Au03 02-3

  4. B extends A Design Example: Employee Database • Suppose we want to generalize our Employee example to handle a more realistic situation A A's stuff • Application domain – kinds of employees • Hourly • Exempt • Boss A's stuff is B automatically A's stuff part of B B's stuff B's stuff 10/9/2003 (c) 2001-3, University of Washington 02-13 10/9/2003 (c) 2001-3, University of Washington 02-14 Design Process – Step 1 Design Process – Step 2 • Think up a class to model each “kind” of thing • Identify state/properties of each kind of thing 10/9/2003 (c) 2001-3, University of Washington 02-15 10/9/2003 (c) 2001-3, University of Washington 02-16 CSE143 Au03 02-4

  5. Design Process – Step 3 Key Observation • Identify actions (behaviors) that each kind of thing can • Many kinds of employees share common properties and do actions • We can factor common properties into a base class and use inheritance to create variations for specific classes 10/9/2003 (c) 2001-3, University of Washington 02-17 10/9/2003 (c) 2001-3, University of Washington 02-18 Generic Employees Specific Kinds of Employees /** Representation of a generic employee. */ • Hourly Employee • Exempt Employee public class Employee { public class HourlyEmployee public class HourlyEmployee // instance variables extends Employee { extends Employee { private String name; // employee name private int id; // employee id number // additional instance variables // additional instance variable /** Construct a new employee with the give name and id number… */ private double hours; // hours worked private double salary; // weekly pay public Employee(String name, int id) { private double hourlyPay; // pay rate this.name = name; /** Return pay earned */ this.id = id; } /** Return pay earned */ public double getPay( ) { /** Return the name of this employee */ public double getPay( ) { return salary; public String getName( ) { return name; } return hours * hourlyPay; } … } … /** Return the pay earned by this employee */ … } public double getPay( ) { return 0.0; } // ??? } … } 10/9/2003 (c) 2001-3, University of Washington 02-19 10/9/2003 (c) 2001-3, University of Washington 02-20 CSE143 Au03 02-5

  6. More Java In Pictures Employee If class D extends B (inherits from) B... Employee • Class D inherits all methods and fields from class B stuff • But... "all" is too strong • constructors are not inherited HourlyEmployee ExemptEmployee • 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 Employee Employee stuff stuff • Class D may contain additional (new) methods and fields HourlyEmp. ExemptEmp. stuff stuff • But has no way to delete any 10/9/2003 (c) 2001-3, University of Washington 02-21 10/9/2003 (c) 2001-3, University of Washington 02-22 Never to be Forgotten Method Overriding • If class D extends B, class D may provide an alternative If class D extends (inherits) from B... or replacement implementation of any method it would otherwise inherit from B • The definition in D is said to override the definition in B Every object of type D is also an object of type 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) • a D can do anything that a B can do (because of inheritance) • Can you override an instance variable? • a D can be used in any context where a B is appropriate • Not exactly... ask me in person if you're really curious 10/9/2003 (c) 2001-3, University of Washington 02-23 10/9/2003 (c) 2001-3, University of Washington 02-24 CSE143 Au03 02-6

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend