Presentation is loading. Please wait.

Presentation is loading. Please wait.

Centre for Computer Technology ICT214 Object Oriented Design and Programming Week 02 – Classes, Objects, Instances Richard Salomon and Umesh Patel Centre.

Similar presentations


Presentation on theme: "Centre for Computer Technology ICT214 Object Oriented Design and Programming Week 02 – Classes, Objects, Instances Richard Salomon and Umesh Patel Centre."— Presentation transcript:

1 Centre for Computer Technology ICT214 Object Oriented Design and Programming Week 02 – Classes, Objects, Instances Richard Salomon and Umesh Patel Centre for Information and Communication Technology Box Hill Institute of TAFE

2 May 29, 2016May 29, 2016May 29, 2016 Copyright Box Hill Institute Contents at a Glance Introduction to classes Introduction to classes Methods Methods Introduction to Objects Introduction to Objects UML UML Pass by value Pass by value

3 May 29, 2016May 29, 2016May 29, 2016 Copyright Box Hill Institute Class Class - A blueprint for an object. Contains attributes and behaviours of each object that it defines Class - A blueprint for an object. Contains attributes and behaviours of each object that it defines Attributes or data of a class (aka fields) - describe the state of an object Attributes or data of a class (aka fields) - describe the state of an object Behaviours or methods of a class - describe what an object can do Behaviours or methods of a class - describe what an object can do

4 May 29, 2016May 29, 2016May 29, 2016 Copyright Box Hill Institute Data type – describes the type of an attribute (field) Data type – describes the type of an attribute (field) Instance - an object that is used by an application Instance - an object that is used by an application Method - set of instruction describing the operations performed by an object Method - set of instruction describing the operations performed by an object

5 May 29, 2016May 29, 2016May 29, 2016 Copyright Box Hill Institute Naming of Classes Must begin with a letter of any alphabet Must begin with a letter of any alphabet Can contain only letters, digits, underscores, or $ signs Can contain only letters, digits, underscores, or $ signs Can not be a Java keyword or literal Can not be a Java keyword or literal Case sensitive Case sensitive Start with an upper case letter and then use lower case letters, with the first character of a new word capitalised e.g. Person, MotorVehicle Start with an upper case letter and then use lower case letters, with the first character of a new word capitalised e.g. Person, MotorVehicle a file may have many classes but only one public class a file may have many classes but only one public class file name must match the public class name file name must match the public class name

6 May 29, 2016May 29, 2016May 29, 2016 Copyright Box Hill Institute Class Example public class Circle { static double radius; static double radius; final static double PI = 3.14159; final static double PI = 3.14159; public static void main(String args[ ]) public static void main(String args[ ]) { double area; double area; radius = 25; radius = 25; area = circleArea(radius); area = circleArea(radius); System.out.println(“\n\t Area of circle with radius of “ + radius + ” is ” + area + “\n”); System.out.println(“\n\t Area of circle with radius of “ + radius + ” is ” + area + “\n”); } static double circleArea(double r) static double circleArea(double r) { return ( PI * r * r) ; return ( PI * r * r) ; }}

7 May 29, 2016May 29, 2016May 29, 2016 Copyright Box Hill Institute Class data the keyword static defines data or methods as - shared - created in memory, - not an instance data i.e. it is class data the keyword static defines data or methods as - shared - created in memory, - not an instance data i.e. it is class data static (class) data and methods are loaded (and retained) in memory before any objects are created. static (class) data and methods are loaded (and retained) in memory before any objects are created.

8 May 29, 2016May 29, 2016May 29, 2016 Copyright Box Hill Institute Method Types Accessor or getter a method that gets data from an object Accessor or getter a method that gets data from an object Mutator or setter a method that sets data in an object Mutator or setter a method that sets data in an object

9 May 29, 2016May 29, 2016May 29, 2016 Copyright Box Hill Institute Method Data Local (temporary) variables for use by the method when operating on objects Local (temporary) variables for use by the method when operating on objects created on the stack; must be initialised before use created on the stack; must be initialised before use compiler (javac) does not allow uninitialised local variable references compiler (javac) does not allow uninitialised local variable references

10 May 29, 2016May 29, 2016May 29, 2016 Copyright Box Hill Institute Constructor Methods All class definitions include a constructor method to create objects All class definitions include a constructor method to create objects no return type (and no return values) no return type (and no return values) methodName = ClassName methodName = ClassName multiple constructors are OK multiple constructors are OK compiler will insert a NULL constructor if the programmer doesn’t define one compiler will insert a NULL constructor if the programmer doesn’t define one NULL constructor – without arguments NULL constructor – without arguments

11 May 29, 2016May 29, 2016May 29, 2016 Copyright Box Hill Institute Return value or type methods must define the type of data that they return, which in turn defir (nes the method’s type Return value or type methods must define the type of data that they return, which in turn defir (nes the method’s type Method Signature its name, it’s type, and data type of its arguments Method Signature its name, it’s type, and data type of its arguments Identifier - name of class, object or its variables Identifier - name of class, object or its variables Reference – identifier of the storage location of object instances or its attributes Reference – identifier of the storage location of object instances or its attributes Other (non-constructor) Methods

12 May 29, 2016May 29, 2016May 29, 2016 Copyright Box Hill Institute Object relationships and associations - how objects interact with each other Object relationships and associations - how objects interact with each other Access modifiers – protection keywords Access modifiers – protection keywords Protection keywords public, default, protected, private most data – private most methods – public Protection keywords public, default, protected, private most data – private most methods – public Inheritance - attributes and behaviour derived from other objects - subclass inherits from super class (parent) Inheritance - attributes and behaviour derived from other objects - subclass inherits from super class (parent) Instantiation - creation of an object Instantiation - creation of an object

13 May 29, 2016May 29, 2016May 29, 2016 Copyright Box Hill Institute Class Scope default scope - same directory or package default scope - same directory or package public scope - available to all public scope - available to all private scope - only available to methods within its own class private scope - only available to methods within its own class protected scope - available to methods of that class and methods in its subclasses protected scope - available to methods of that class and methods in its subclasses

14 May 29, 2016May 29, 2016May 29, 2016 Copyright Box Hill Institute Message – request for a method Message – request for a method Message sending objects send (and receive) messages to interact with each other [ method call ] Message sending objects send (and receive) messages to interact with each other [ method call ] Re-use once written and tested, objects and classes can be used in other applications Re-use once written and tested, objects and classes can be used in other applications

15 May 29, 2016May 29, 2016May 29, 2016 Copyright Box Hill Institute Object Mutability and destruction JVM manages memory and garbage collection using its System.gc() thread Object Mutability and destruction JVM manages memory and garbage collection using its System.gc() thread Encapsulation containing or packaging data and methods in a class Encapsulation containing or packaging data and methods in a class Information (data) hiding black box approach i.e. no need to publish details Information (data) hiding black box approach i.e. no need to publish details

16 May 29, 2016May 29, 2016May 29, 2016 Copyright Box Hill Institute Overloaded Methods methods with same name but different formats (or data types) are called overloaded methods methods with same name but different formats (or data types) are called overloaded methods method signature identifies the specific method used method signature identifies the specific method used

17 May 29, 2016May 29, 2016May 29, 2016 Copyright Box Hill Institute Modifiers Two types of modifiers 1. access specifiers: private, protected, public, default 2. qualifiers: static – class level final – immutable native – from another source in another language transient – not written to persistent storage

18 May 29, 2016May 29, 2016May 29, 2016 Copyright Box Hill Institute UML UML – Unified Modelling Language UML – Unified Modelling Language Diagrams Diagrams Use Case Diagramfunctionality Use Case Diagramfunctionality Class diagramstatic structure Class diagramstatic structure Object diagram Object diagram State chart Diagramdynamic behaviour State chart Diagramdynamic behaviour Collaboration diagramsobject interaction Collaboration diagramsobject interaction Activity diagramsdynamic nature Activity diagramsdynamic nature Component Component Deployment Deployment

19 May 29, 2016May 29, 2016May 29, 2016 Copyright Box Hill Institute UML Diagrams Rectangle describes classes and objects Rectangle describes classes and objects Lines describe relationships Lines describe relationships Symbols describe accessibility and strength of relationships Symbols describe accessibility and strength of relationships + public - private # protected default (no symbol) Links inheritance association aggregation Links inheritance association aggregation

20 May 29, 2016May 29, 2016May 29, 2016 Copyright Box Hill Institute UML Class Diagram Person Class Name Attibutes (fields) methods -name : String -dateOfBirth : String -id : integer +Person (String aName, String aDate, int aID)

21 May 29, 2016May 29, 2016May 29, 2016 Copyright Box Hill Institute Class definitions 1 /** 2 * Java Program: Person.java 3 * @author Teacher 4 * @version 2005 5 */ 6 public class Person 7 { 8 private String name; 9 private String dateOfBirth; 10 private int id; 11 12 public Person (String aName, String aDate, int aID) 13 { 14 name = aName; 15 dateOfBirth = aDate; 16 id = aID; 17 } 18 } class definition Constructor

22 May 29, 2016May 29, 2016May 29, 2016 Copyright Box Hill Institute Creation (instantiation) of Objects public static void main(String[ ] args) { Person Scott = new Person(“Scott”,”25/07/1985”,1234); } public static void main(String[ ] args) { Person Scott = new Person(“Scott”,”25/07/1985”,1234); } Instantiation or Creation of an object

23 May 29, 2016May 29, 2016May 29, 2016 Copyright Box Hill Institute Object creation within own class method within own class method by a method in another class by a method in another class The creation of an object consists of five steps The creation of an object consists of five steps

24 May 29, 2016May 29, 2016May 29, 2016 Copyright Box Hill Institute Steps in Creating an Object 1. Design the object attributes and behaviours 2. Define the object and create a class definition or a blueprint for the object 3. Create an object 4. Use the object

25 May 29, 2016May 29, 2016May 29, 2016 Copyright Box Hill Institute Discovery of Objects Write a simple description of the what your program is about, including its purpose and intended usage Write a simple description of the what your program is about, including its purpose and intended usage Underline all nouns and adjectives in one colour. The nouns and adjectives are potential objects and attributes, but not all make sense to be used. Underline all nouns and adjectives in one colour. The nouns and adjectives are potential objects and attributes, but not all make sense to be used.

26 May 29, 2016May 29, 2016May 29, 2016 Copyright Box Hill Institute Discovery of Objects Next underline al the verbs and adverbs in another colour. Next underline al the verbs and adverbs in another colour. The verbs and adverbs are your potential methods The verbs and adverbs are your potential methods Now define the classes, using the appropriate nouns and adjectives for attributes and the appropriate verbs and adverbs for the methods Now define the classes, using the appropriate nouns and adjectives for attributes and the appropriate verbs and adverbs for the methods

27 May 29, 2016May 29, 2016May 29, 2016 Copyright Box Hill Institute Class Definition Don’t make the class too broad Don’t make the class too broad You may have many classes You may have many classes It is better to have more tightly defined classes than fewer broad classes It is better to have more tightly defined classes than fewer broad classes You can have ONLY ONE public class in your application, and it must match your file name You can have ONLY ONE public class in your application, and it must match your file name

28 May 29, 2016May 29, 2016May 29, 2016 Copyright Box Hill Institute A constructor method is required to create an object. The constructor method has the same name as the class and no return type A constructor method is required to create an object. The constructor method has the same name as the class and no return type A programmer may choose not to define a constructor. In such a case, the compiler will create a null constructor. A programmer may choose not to define a constructor. In such a case, the compiler will create a null constructor. Standard methods are setters (mutators) and getters (accessors). Standard methods are setters (mutators) and getters (accessors). Custom methods are used to implement the application rules Custom methods are used to implement the application rules Class Definition

29 May 29, 2016May 29, 2016May 29, 2016 Copyright Box Hill Institute Use of Objects Objects interact with each other to accomplish different tasks Objects interact with each other to accomplish different tasks This interaction occurs when one object uses the public methods of the other object This interaction occurs when one object uses the public methods of the other object The dot operator is used to gain access to these methods. The dot operator is used to gain access to these methods. Object instance data is accessed through the reference variable of the object Object instance data is accessed through the reference variable of the object Class data is accessed through the use of the class name Class data is accessed through the use of the class name Class data is qualified with the use of the keyword static Class data is qualified with the use of the keyword static Only non-private data or methods are accessible to other objects Only non-private data or methods are accessible to other objects

30 May 29, 2016May 29, 2016May 29, 2016 Copyright Box Hill Institute Pass by value A copy of the variable value is passed. The original variable value remains unchanged A copy of the variable value is passed. The original variable value remains unchanged Return value is a copy of the local variable value NB the return value data type must match the data type of the method Return value is a copy of the local variable value NB the return value data type must match the data type of the method


Download ppt "Centre for Computer Technology ICT214 Object Oriented Design and Programming Week 02 – Classes, Objects, Instances Richard Salomon and Umesh Patel Centre."

Similar presentations


Ads by Google