programming for geographical information analysis: core skills lecture 5: working with others’...

35
Programming for Geographical Information Analysis: Core Skills Lecture 5: Working with others’ code I: Inheritance

Upload: fidel-lathan

Post on 14-Dec-2015

215 views

Category:

Documents


1 download

TRANSCRIPT

  • Slide 1

Programming for Geographical Information Analysis: Core Skills Lecture 5: Working with others code I: Inheritance Slide 2 Review Classes are photocopy originals for objects. Usually we make an object from a class. The object contains all the code in the class. We can therefore call methods inside the object. pointObject.setX(200.0); We pass a method certain arguments. Can use objects variables directly but isnt usual. pointObject.x = 200.0; Slide 3 This lecture Inheritance Extension Interfaces UML Slide 4 Philosophy of Object Orientation Encapsulation. Data and the methods working on them are contained. You dont need to know how something is done, just what does it. Indeed, you shouldnt be able to find out. Java: private/public, static, final, class, methods. Polymorphism. One idea should be presented in a flexible form. Java: polymorphic methods. Inheritance. Classes should be able to inherit code from other classes and add their personal twist to it. Slide 5 Inheritance Say you go to the ATM of BlueBank Inc. It collects a PIN in an object. But... you belong to RedBank Plc., which has its own software. How does BlueBank send RedBank your details to confirm? The object BlueBank has wasnt written by programmers at RedBank, so it is bound to be different from what they want. BlueBank could have a class for every bank it deals with, but that would be complicated. Slide 6 Instead, all the banks design a class description that has the information they need. All banks promise the objects they send will match this class description as a minimum. The objects can have other stuff as well, but they must match the description at the least. Slide 7 Inheritance BlueBankRequest Is a subtype of GenericRequest Methods: getPin() setPin() Theres nothing to stop RedBank developing its own version with other methods along with getPin(). Methods demanded: getPin() Slide 8 Methods A method can take a subclass object where it expects the superclass. However, the method can then only use the superclass methods, and none of the subclass detail. Slide 9 BlueBankRequest b1 = new BlueBankRequest(); b1.setPin (getAtmPin()); boolean pinOk = redBankConnection.isPinOk(b1); boolean isPinOk (GenericRequest qr) { if (qr.getPin() == customerPin){ return true; } else { return false; } Because the isPinOk method takes in a GenericRequest and checks the things sent against that, it can guarantee that the getPin method will be there. As long as it just uses that, it will be fine. It doesnt need to know the object is more complicated. Slide 10 Inheritance The getPin code in GenericRequest could be: 1) full code that does the whole promised job; 2) empty, with classes just promising the method will be there; 3) something inbetween, with some methods full code, some just promises. Java builds these in through: extension: whole code available + what you write interfaces: just a set of promises + you write everything abstract classes: mix of code and promises Slide 11 This lecture Inheritance Extension Interfaces UML Slide 12 Extension Classes can be extended: public class GenericRequest { protected int pin; public int getPin() { return pin; } Slide 13 Inheritance: extends Inheriting classes have all the public or protected variables and methods of the inherited class. class BlueBankRequest extends GenericRequest { void setPin(int pinIn) { pin = pinIn; } Only need to define new variables and methods. The pin variable and getPin are inherited from GenericRequest. The parent class is called the Superclass, the inheritor is called the Subclass. Slide 14 Inheritance: super Subclasses cant access the private variables of Superclasses. Therefore, one way to set these is to use the Superclasss constructor. class BlueBankRequest extends GenericRequest { public BlueBankRequest() { super("Cheshire Cat Banking plc."); } The call to super must be the first thing in the Subclasses constructor. Slide 15 Inheritance: overriding What happens if you call a method in the Subclass the same thing as a method in the Superclass? If they have different arguments it doesnt matter the JVM will call the one that works with them. If the arguments are the same the Subclass will be used. The method is overridden. We can also use super. to refer to the Superclass methods and variables in the same way as we use this. to refer to the present class. If we declare the Superclass methods as final they cant be overridden by the Subclass. final Classes cant be inherited. Slide 16 Abstract Classes Classes that can only be inherited cant make objects from them. Include some methods and variables. Any Class that extends it must provide all the missing methods, or be declared abstract itself. Slide 17 abstract class GenericRequest { int pin = 0; int account = 0; void setAccNumber(int accountIn) { account = accountIn; } abstract int getPin () ; } class BlueBankRequest extends GenericRequest { int getPin () { return pin; } BlueBankRequest objects have the setAccNumber method without defining it, but they must define getPin because all GenericRequest inheritors must have it. Slide 18 Issues Note: 1)You dont actually have to have any content in the methods to get the compiler to see youve met your promises with abstract classes. 2)You can only extend one class or abstract class at a time. You cant, for example subclass both a menu item and a scrollbar what would it look like? Slide 19 Hierarchies Finally, with inheritance, we can build up flexible modelling and data- holding structures, where elements of behaviour can be replaced, and classes used to build up multiple subclasses. We see this, for example in ArcGIS Geodatabases: route66 instantiates Freeway which inherits Road which inherits Line. each has additional variables suitable for holding, e.g. road speed limits. But also in models: dale instantiates Human which inherits Primate which inherits Mammal with inherits Animal which inherits Agent. each has additional and overriding behaviours. Slide 20 Review If you have a class youd like to pick up the methods from, just extend it: class BlueBankRequest extends GenericRequest { From then on, you get all the class code for free! If the class is abstract, the compiler will force you to put in the abstract methods. Slide 21 This lecture Inheritance Extension Interfaces UML Slide 22 Interfaces Interfaces are the ultimate abstract class. They are lists of methods that must be defined in classes that implement them. All variables in them are final. Why the devil would you want to do that? You can make classes that rely on finding certain methods wherever they are used. By forcing people to implement these methods you can guarantee they exist. Slide 23 BlueBankRequest b1 = new BlueBankRequest(); b1.setPin (getAtmPin()); boolean pinOk = redBankConnection.isPinOk(b1); boolean pinOk (GenericRequest qr) { if (qr.getPin() == customerPin) return true; else return false; } Slide 24 public class BlueBankRequest implements GenericRequest { private int pin = 0; public void setPin (int p) { pin = p; } public int getPin () { return pin; } public interface GenericRequest { public int getPin (); } interfaces Slide 25 BlueBankRequest b1 = new BlueBankRequest(); b1.setPin (getAtmPin()); boolean pinOk = redBankConnection.isPinOk(b1); boolean isPinOk (GenericRequest qr) { if (qr.getPin() == customerPin) return true; else return false; } Slide 26 interface variables Interfaces classically also contain final static variables of use with the interface: public interface GenericRequest { public final static String STANDARDS_AGENCY = Bank Standards Inc.; public int getPin (); } BlueBankRequest b1 = new BlueBankRequest(); b1.setPin (1234); boolean ok = redBankConnection.isPinOk(b1); System.out.printToATM(Your connection is brought to you by + GenericRequest.STANDARDS_AGENCY); Slide 27 Importance Interfaces are the key link between any two pieces of code that need to talk to each other where there might need to be a wide variety of implementations of a standard. This includes: 1)Commercial systems. 2)Operating systems. 3)The interaction between the user interface and a program. 4)Interaction between components of a program you want to be swappable later. Slide 28 Inheritance We can extend a Class to give us all its functionality. class A extends B { We can implement an Interface, which means we promise to supply its methods. class A implements C { We can only extend one Class, but we can implement many Interfaces using commas. class A extends B implements C, D, E { Slide 29 This lecture Inheritance Extension Interfaces UML Slide 30 The Unified Modelling Language. A way of drawing code overviews so that its understood by any programmer. Usually part of a software development process. UML is an overview. Once developers have seen your UML, theyll look at your documentation (next lecture) for more information. Can be converted into raw code using some Computer Aided Software Engineering (CASE) tools. Slide 31 Why have an overview standard? In industry code tends to be part of much larger projects with multiple programmers working on it. Therefore we need standard overviews so that We can see whether an application can be made and how many people well need to work before we start coding. We can see where our code fits in when we start. We can see how to use other peoples objects. If you spontaneously combust, others can pick up your work easily. Slide 32 Parts of UML There are several types of model in the UML. Which one you use, and how you look at it, will depend on what youre trying to do. The secret is to use whats useful. Types of diagram Use Cases (What are the uses someone [or thing] may want from our software). Class Diagrams (What are the different relationships between Classes). Sequence Diagrams (What order does stuff happen in). State/Activity Diagrams (What are Classes like over time). Slide 33 The Class Diagram -/+/# = private/public/protected Solid arrow is inheritance from a class/abstract class. Interfaces have dashed arrows and are written: > Abstract classes are either light font or, more usually, italics. Variables at top, methods at bottom. Slide 34 Activity Diagram Replacement for older flowcharts. Beginning and end marked by circles (end by a double circle). Diamonds for decisions. Parallel code marked by bars. Slide 35 Next lecture Using others code II: Problems and Packages More methods Check out practice pieces on inheritance Check out UML resources Practical