Class #02: Inheritance and Object-Oriented Design Software Design II (CS 220): M. Allen, 23 Jan. 18 Basic Class Diagrams Describes a class and how it can be used properly Sketch of properties and behaviors of objects in the class No extra details about how they actually work Class Name attributes (instance variables) operations (methods) + (String, int) 2 Attributes What does the minus sign here mean? class has two simple attributes, storing basic facts about each individual car Each different object instance in a program will have its own version of these different attributes + (String, int) class instance variables both have private access Store information, but that info cannot be accessed or altered from anywhere outside the class How do we read or write to such private variables? + (String, int) 3 4 1
Class Methods What does the plus sign mean here? Update methods Change something about state of an object These return void (take an action but don t give output) Query methods Tell us about object state non-void return: actually give us some info back + (String, int) These methods all have public access Can be used outside of the class (by other class code) Allows programmer to control exactly how variables are changed, and what information is revealed by the class + (String, int) 5 6 Code for a Lot Management Application Constructor Constructor method Used to create an object Has same name as class Has no return type in front Almost always public (WHY?) This constructor takes two parameters as input + (String, int) Manny Citron s Used s wants you to write an inventory-management program for them It needs to track their current stock of vehicles Needs to add and remove vehicles from the stock 7 8 2
Object-Oriented Design The Lot class uses instance variables of the class type It is an aggregate class, made up of other class objects We can sketch relationships between class diagrams Diamond arrow signifies a used by relationship Also known as a has-a relationship, in the sense that the Lot class has some instances as part of its own specification car1 car2 Lot + Lot() + add() + remove() + void prints() <<attributes>> <<methods>> A Hitch: Changing Problem Specification Manny soon realizes that your basic class isn t enough for his needs Also sells commercial vehicles (trucks/vans), with different carrying capacities (1/5/10 ton, ) 9 10 One Solution: Create a New Class Another Solution + (String, int) Truck int capacity + Truck(String, int, int) + void setcapacity(int) + int getcapacity() We could just duplicate the class for the Truck class (and maybe more), adding in the features we need A number of drawbacks: 1. Code repetition: we duplicate our work in multiple classes 2. Harder to write general-purpose code: our car-lot code will have to handle Truck and objects, and we may not know which ones we are going to get at any specific point (e.g. when a new one is added to the stock) Instead, we will use inheritance structures 11 12 3
Common Features in Classes Certain types of objects have things in common s/trucks/motorcycles Savings/checking/investment accounts In Java, such similarities are exploited by inheritance Code can be made simpler and more useful Similarities are written into the super-class (parent) Each sub-class (child) extends the parent Inherits all similarities, and can have particular differences of its own Child inherits methods and variables If these things are private, then they are not accessible to child If they are public (or protected), then the child can directly access them as if they were its very own Inheritance in Pictures and in Code Inheritance can be represented as a diagram, with openheaded arrows from children to parents Each child is a more specific kind of parent object This is called an is-a relationship, since each, for example, is actually a kind of Vehicle, too Vehicle Truck class Vehicle { class extends Vehicle { // data and methods // more data and methods class Truck extends Vehicle { // more data and methods 13 14 Object Diagrams for Inheritance Implementing the Vehicle Class Vehicle - String makemodel - int mileage + Vehicle(String, int) + (String, int) Truck int capacity + Truck(String, int, int) + void setcapacity(int) + int getcapacity() public class Vehicle { private String makemodel; private int mileage; public Vehicle( String mod, int miles ) { makemodel = mod; mileage = miles; public void setmakemodel( String s ) { makemodel = s; public void setmileage( int m ) { mileage = m; public String getmakemodel() { return makemodel; public int getmileage() { return mileage; Other than the necessary Vehicle() constructor, this is identical to the code that was originally part of the class If the class doesn t need to add any more features, then there we won t bother to do so 15 16 4
The Sub-Class Implementing the Class extends Vehicle for inheritance needs own constructor, but nothing else! + (String, int) Every other method and variable it needs can be inherited directly from the Vehicle class, without need of repetition public class extends Vehicle { public ( String mod, int miles ) { super( mod, miles ); The () constructor simply takes its inputs and passes them along to the super() Vehicle constructor, which itself then writes their values to the instance variables 17 18 Constructors and the super Command All methods and variables of a parent are inherited, except for the constructor Main reason: constructor must have same name as class itself Parent constructor must be called first to instantiate any necessary attribute variables, before child constructor can do any necessary work of its own The child runs its own constructor, and if it wants to use the parent constructor it uses the super reference super() is the parent constructor super.methodname() runs a method from the parent explicitly (often optional, as long as there is no other method with the same name, due to over-riding, etc.) Implementing the Truck Class Truck has extra instance variable Constructor runs super() to set common variables, then saves extra capacity variable locally public class Truck extends Vehicle { private int capacity; public Truck( String mod, int miles, int cap ) { super( mod, miles ); capacity = cap; /* post: result == makemodel */ public void setcapacity( int c ) { capacity = c; public int getcapacity() { return capacity; Has its own particular methods, unique to Truck class 19 20 5
This Week Topic: Inheritance and polymorphism Class meetings: Monday Thursday: regular classroom Friday: in the CS Lab (16 Wing) Optional Reading: Chapter 5 & 6 in text Office Hours: Wing 210 Monday/Wednesday/Friday, 10:00 AM 11:00 AM Tuesday/Thursday, 1:30 PM 3:00 PM 21 6