Presentation is loading. Please wait.

Presentation is loading. Please wait.

CE203 - Application Programming Autumn 2013CE203 Part 01 Udo Kruschwitz

Similar presentations

Presentation on theme: "CE203 - Application Programming Autumn 2013CE203 Part 01 Udo Kruschwitz"— Presentation transcript:

1 CE203 - Application Programming Autumn 2013CE203 Part 01 Udo Kruschwitz

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: Wichit Sombat (wsomba) / Ludvig Kihlman (lzkihl) Module area: Autumn 2013CE203 Part 02

3 Acknowledgements A lot of the material was prepared by Mike Sanderson Some parts were prepared by Martin Waite Additional material by me Autumn 2013CE203 Part 03

4 Recommended Reading There are many books that cover the material in this course – I will refer to Java How To Program (9 th ed.), H.M. Deitel & P.J.Deitel (Prentice Hall, 2012) Introduction to Java Programming (8 th ed.), 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. Autumn 2013CE203 Part 04

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

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

7 Brief Outline Review of object-oriented programming with Java Applets Exceptions Collections JDBC Security More GUI Autumn 2013CE203 Part 07

8 Motivating Examples............ Autumn 2013CE203 Part 08

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 Autumn 2013CE203 Part 09

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”, 6 th Edition, Prentice-Hall, 2007) Autumn 2013CE203 Part 010

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 Autumn 2013CE203 Part 011

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 Autumn 2013CE203 Part 012

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) Autumn 2013CE203 Part 013

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 Autumn 2013CE203 Part 014

15 Object Orientation: Composition & Specialisation Autumn 2013CE203 Part 015

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 Autumn 2013CE203 Part 016

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 (int i, int j) // version 1 {return i + j;} double sum (double e, double f) // version 2 {return e + f;} int sum (int i, int j, int k) // version 3 {return i + j + k;} Autumn 2013CE203 Part 017

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. Autumn 2013CE203 Part 018

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 superclass definitions Autumn 2013CE203 Part 019

20 Class Definitions 2 The class definition is (in part) a specification for an object  An object is an instance of a class just 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) Autumn 2013CE203 Part 020

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 Autumn 2013CE203 Part 021

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) Autumn 2013CE203 Part 022

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) Autumn 2013CE203 Part 023

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. Autumn 2013CE203 Part 024

25 Accessibility Rules Autumn 2013CE203 Part 025 1.With no modifier, access is permitted from anywhere in the enclosing package. 2.The public modifier permits access from beyond the enclosing package. 3.The private modifier prevents access from outside the enclosing class. 4.The most restrictive modification applies

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

27 Classes and Instances As soon as the program starts, the class descriptions are activated: space for class variables is 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 Autumn 2013CE203 Part 027

28 Classes and Instances 2 When a new instance (object) is created with new space for the instance variables is allocated on the heap ( new returns 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 Autumn 2013CE203 Part 028

29 Specialisation Specialisation hierarchies and Interfaces are 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) Autumn 2013CE203 Part 029

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 subclass the first statement in the meth definition is the call super.meth(), and then further statements are added for the additional subclass code Autumn 2013CE203 Part 030

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 (going up the hierarchy from the object’s class) is applied, for example: Autumn 2013CE203 Part 031

32 Specialisation – Dynamic Binding 2 Autumn 2013CE203 Part 032 Example (continued):

33 Specialisation – Dynamic Binding 3 Autumn 2013CE203 Part 033 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 noise to use cannot be made by the compiler.

34 Specialisation – Dynamic Binding 4 Autumn 2013CE203 Part 034 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.

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

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. Autumn 2013CE203 Part 036

Download ppt "CE203 - Application Programming Autumn 2013CE203 Part 01 Udo Kruschwitz"

Similar presentations

Ads by Google