1 / 36

CE203 - Application Programming

CE203 - Application Programming. Udo Kruschwitz udo@essex.ac.uk. Module Administration. 10 lectures (2 hours each): Monday 9 :00 ( 4.722 ) 10 labs (2 hours each) Mon day/Thursday ( Lab 1 ) 2 assignments (10% each): Assignment 1 due 6th November (week 6)

brenna
Télécharger la présentation

CE203 - Application Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CE203 - Application Programming UdoKruschwitz udo@essex.ac.uk CE203 Part 0

  2. Module Administration • 10 lectures (2 hours each): Monday 9:00 (4.722) • 10 labs (2 hours each) Monday/Thursday (Lab 1) • 2 assignments (10% each): • Assignment 1 due 6th November (week 6) • Assignment 2 due 10th December (week 11) • Test (10%) on 14th November (week 7) • Two hour exam in May/June (70% of the module credit) • GLAs: WichitSombat (wsomba) / LudvigKihlman (lzkihl) • Module area: http://orb.essex.ac.uk/CE/CE203/ CE203 Part 0

  3. Acknowledgements • A lot of the material was prepared by Mike Sanderson • Some parts were prepared by Martin Waite • Additional material by me CE203 Part 0

  4. Recommended Reading There are many books that cover the material in this course – I will refer to Java How To Program(9thed.), H.M. Deitel & P.J.Deitel (Prentice Hall, 2012) Introduction to Java Programming (8thed.), Y.D. Liang (Pearson, 2011) If you choose to buy a different text-book you should check if it uses at least Java 5.0 and covers the material listed in the syllabus in the course catalogue entry for CE203. CE203 Part 0

  5. Recommended Reading 2 Other good books: • Learning Java (4thed.), P. Niemeyer and D. Leuck(O’Reilly, 2013) • Java in a Nutshell (5th ed.), D. Flanagan (O’Reilly, 2005) (reference book) CE203 Part 0

  6. Learning Outcomes • Demonstrate a knowledge of core Java application libraries • Explain the event-driven model underlying Java GUIs • Write Java programs with interactive graphical user interfaces (GUIs) • Write Java programs that interact with databases • Write Java programs that make efficient use of the Java collections package. • Work with the Java Security Manager to create secure program policies. CE203 Part 0

  7. Brief Outline • Review of object-oriented programming with Java • Applets • Exceptions • Collections • JDBC • Security • More GUI CE203 Part 0

  8. Motivating Examples . . . . . . CE203 Part 0

  9. Today: A Bit of Revision • What you should know by now • Abstract from the language-specific issues to get a more conceptual picture of object-oriented programming • Understanding the principles helps seeing the overall picture CE203 Part 0

  10. Java Characteristics • Simple • Object Oriented • Interpreted • Portable • Architecture-neutral • High-performance • Distributed • Robust • Secure • Multi-threaded • Dynamic ( Source: LIANG, Y.D., “Introduction to Java Programming”, 6thEdition, Prentice-Hall, 2007) CE203 Part 0

  11. Object Orientation – Classes & Objects An object is an instance of a class many objects of the same class can exist in the same program A class can exist in a program even if no instance of it exists some classes (abstract classes) cannot be instantiated at all CE203 Part 0

  12. Object Orientation - Attributes • Both class and object can have attributes • Attributes in Java are simply called variables • Class variables are shared by all instances of the class • Instance variables belong to a particular instance (or object) • The state of an object is given by the values of its instance variables CE203 Part 0

  13. Object Orientation - Methods • Methods can also be defined at either class or instance level • There are basically three kinds of methods access methods (get and set attribute values) service methods (to offer services to other objects) housekeeping methods (for internal use) CE203 Part 0

  14. Object Orientation – Information Hiding • External access to an object’s attributes and methods is controlled by means of the scope modifiers private, public and protected • Direct access to attributes is usually prohibited • Access is usually gained through secure public methods CE203 Part 0

  15. Object Orientation: Composition & Specialisation CE203 Part 0

  16. Parameterisation and References • In Java, objects are “reference types” (unlike primitive data types!) the value passed when the actual parameter is an object is a reference. • Parameters of primitive type are always passed using call by value the value associated with the actual parameter cannot be changed • Parameters that are objects are always passed using call by referencethe value(s) associated with the actual parameter can be changed CE203 Part 0

  17. Method Overloading We can supply several definitions for a method sum within the same class definition, as long as each definition has a different set of input types. int sum (inti, int j) // version 1 {return i + j;} double sum (double e, double f) // version 2 {return e + f;} int sum (inti, int j, int k) // version 3 {return i + j + k;} CE203 Part 0

  18. Method Overloading 2 sum(4, 5) will call version 1 on the previous slide sum(4.5, 5.5) will call version 2 on the previous slide sum(4, 5, 6)will call version 3 on the previous slide This process is known as static binding, because the decision about which version of the method to apply is made at the compilation stage. CE203 Part 0

  19. Class Definitions Class definitions provide a unit of scope:  they encapsulate data and operations on that data  they can specify the attributes and methods of an entire class  they can specify the attributes and methods of instances of the class Class definitions are hierarchical:  they can be written as extensions of existing class definitions  they can inherit or redefine parts of the superclassdefinitions CE203 Part 0

  20. Class Definitions 2 The class definition is (in part) a specification for an object • An object is an instanceof a classjust as the number 3 is an instance of the type int • Objects are created dynamically (at run time) using the keyword new and come into existence when memory is allocated for them on the heap • Objects cease to exist when they are no longer required (the space they occupied is automatically reclaimed by the garbage collector) CE203 Part 0

  21. Class Definitions 3 The class definition is (in part) a specification for the entire class  Any member (variable or method) introduced with the keyword static becomes part of the static context, and exists throughout program execution  the class can exist independently of objects of the class  static members can be used without creating objects CE203 Part 0

  22. Class Definitions 4 • A static variable is known as a class variable (one set of class variables is shared by all objects of the class) • A non-static variable is known as an instance variable (each object of the class has its own set of instance variables) CE203 Part 0

  23. Inheritance The class definition inherits method and variable definitions • methods and variables defined in the superclass are automatically included in the definitions of its subclasses • the inheritance is transitive, so classes inherit the attributes of the superclass of their superclass, and so on. • since every class is derived originally from the class Object, all objects inherit Object’s methods, e.g. toString • inherited members of the superclass can be redefined in the subclass (variable shadowing, method overriding) CE203 Part 0

  24. Accessibility The public and private modifiers may be omitted from class definitions and variable and method declarations. By default, their scope becomes the enclosing package.   CE203 Part 0

  25. Accessibility Rules • With no modifier, access is permitted from anywhere in the enclosing package. • The public modifier permits access from beyond the enclosing package. • The private modifier prevents access from outside the enclosing class. • The most restrictive modification applies CE203 Part 0

  26. Accessibility Examples • An unmodified member cannot be accessed from outside the package even if it is contained in a public class. • A publicmember cannot be accessed from outside the package if it is contained in an unmodified class. CE203 Part 0

  27. Classes and Instances As soon as the program starts, the class descriptions are activated: • space for class variablesis allocated at a location within static memory • the space remains allocated to those variables until the program stops • classes exist throughout program execution • class members (variables and methods) can always be used CE203 Part 0

  28. Classes and Instances 2 When a new instance (object) is created with new • space for the instance variables is allocated on the heap (newreturns a reference to that space), the reference may be assigned to more than one variable • if, at any point, there are no “live” references, the space is reclaimed • objects only exist while there are existing references to them • instance members (variables and methods) can only be used when an instance exists CE203 Part 0

  29. Specialisation Specialisation hierarchiesand Interfacesare closely related topics  They both provide support for polymorphism (the ability to define methods that can operate on many different types)which is implemented by means of dynamic binding(which means that the decision about which version of the method should be applied is made while the program is executing) CE203 Part 0

  30. Specialisation 2 Redefinition of methods is called method overriding. A superclass method can be completely redefined, or added to. When adding extra statements to the superclass method, the superclass method is called first using the super reference, e.g.  if a superclass method meth is added to in a subclass, then in the subclassthe first statement in the methdefinition is the call super.meth(), and then further statements are added for the additional subclass code CE203 Part 0

  31. Specialisation – Dynamic Binding When a method has been redefined (possibly more than once) in a hierarchy of classes, and that method is called on an object, then the closest definition (goingup the hierarchy from the object’s class) is applied, for example: CE203 Part 0

  32. Specialisation – Dynamic Binding 2 Example (continued): CE203 Part 0

  33. Specialisation – Dynamic Binding 3 Now consider the following situation: It will not be known until runtime whether the user will choose “cat” or “dog”, i.e.the decision about which version of noiseto use cannot be made by the compiler. CE203 Part 0

  34. Specialisation – Dynamic Binding 4 The decision about which version of the method to use is made when the method is called at run-time, and is therefore referred to as dynamic binding. Dynamic binding is possible in Java because the internal representation of an object includes information about its class definition. Dynamic binding supports the following kind of polymorphism. CE203 Part 0

  35. Specialisation – Dynamic Binding 5 Animal[] animalArray = new Animal[4]; … animalArray[0] = felix; animalArray[1] = rover; animalArray[2] = daisy; animalArray[3] = nanny; … for (inti = 0; i < 4; i++) System.out.println (animalArray[i].noise()); CE203 Part 0

  36. Abstract Classes Abstract classes are partial specifications: • they (typically) contain at least one abstract method (a prototype method definition with no body) • and they cannot be instantiated. Subclasses of the abstract class can be instantiated if they provide full definitions for the abstract methods. The top most levels of a large hierarchy may consist of several abstract classes. CE203 Part 0

More Related