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

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Advertisements

Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Road Map Introduction to object oriented programming. Classes
Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
UML Basics & Access Modifier
Object Oriented Software Development
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Centre for Computer Technology ICT115 Object Oriented Design and Programming Week 2 Intro to Classes Richard Salomon and Umesh Patel Centre for Information.
Writing Classes (Chapter 4)
Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition.
An Object-Oriented Approach to Programming Logic and Design
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
The Java Programming Language
Centre for Computer Technology ICT214 Object Oriented Design and Programming Week 1 - Introduction Richard Salomon and Umesh Patel Centre for Information.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
© 2004 Pearson Addison-Wesley. All rights reserved September 12, 2007 Encapsulation ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Object Oriented Programming with Java 03 - Introduction to Classes and Objects.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Classes Modeling the Object. Objects model the world Classes are programmer defined types that model the parts of a system Class serve as blueprints for.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Centre for Computer Technology ICT115 Object Oriented Design and Programming Week 03 – Class Design, Object Discovery and Use Richard Salomon Centre for.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 3 A First Look at Classes and Objects.
Classes, Interfaces and Packages
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Unified Modeling Language (UML)
Comp1004: Building Better Objects II Encapsulation and Constructors.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 3: A First Look at Classes and Objects.
Topic: Classes and Objects
Classes C++ representation of an object
Chapter 3: Using Methods, Classes, and Objects
Programming Language Concepts (CIS 635)
Can perform actions and provide communication
Object Based Programming
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
Defining Your Own Classes Part 1
Corresponds with Chapter 7
Basics of OOP A class is the blueprint of an object.
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Object-Oriented Programming
Object Oriented Programming in java
2.1 Introduction to Object-Oriented Programming
Lecture 8 Object Oriented Programming (OOP)
CS 240 – Advanced Programming Concepts
Presentation transcript:

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

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

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

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

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

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 = ; final static double PI = ; 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) ; }}

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.

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

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

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

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

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

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

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

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

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

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

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

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

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)

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

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

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

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

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.

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

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

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

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

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