Download presentation
Presentation is loading. Please wait.
Published byHortense Fields Modified over 9 years ago
1
OBJECTS AND CLASSES CITS1001
2
Concepts for this lecture class; object; instance method; parameter; signature data type multiple instances; state method calling; source code; method result Reading: Objects First with Java, Chapter 1 2
3
Objects represent ‘things’ from the real world, or from some problem domain e.g. the red car down there in the car park e.g. the lecturer talking to you now e.g. you! Classes represent all objects of a certain kind e.g. Car, Lecturer, Student A class is a group of objects that have similar characteristics and that exhibit similar behaviour An object is a specific instance of a class Classes and objects 3
4
Example The set of all students forms the class Student Each individual student is an object of the class Student John Smith and Janice Lee are instances of Student 4
5
Example The set of all dogs forms the class Dog Each individual dog is an object of the class Dog Spot, Rover, and Rex are all instances of Dog 5
6
Why do we use classes? To reduce complexity Often, we know how to deal with an object based purely on knowing its class, without knowing anything specifically about that particular instance For example, if we encounter a dog – i.e. an instance of the class Dog – we already have a basic understanding of how to deal with it, even if we have never previously met that particular dog We know that it might bark, or bite, or wag its tail, based purely on knowing that it is a Dog Barking, biting, and tail-wagging are best viewed as features of the class Dog, not of any individual dog 6
7
Describing a class We describe a class by listing the common features that are shared by all the objects in that class The attributes that each object has, and The actions that each object can perform Student number is an attribute of the class Student Every student has a student number; although each individual student has a different student number Barking is an action that all objects of the class Dog do Every dog barks; although different dogs do it differently, based on the attributes of a given individual 7
8
What is a Waiter ? A Waiter has the following attributes Name Tax File Number And the following actions Bring menus Take orders Bring meals This collection of attributes and actions defines the class of Waiters We can deal with any individual waiter, whether we have met them before or not, based solely on our knowledge of the class Waiter 8
9
Exercise Download and open the shapes BlueJ project Create objects from the classes Circle, Square, Triangle Right-click on the class and select new(...) Inspect the objects Double-click on any object in the object bench Invoke their methods Right-click on an object and select one of its methods Experiment! Making mistakes is OK – it is a good way of learning to program! 9
10
What's in an object? Objects have operations that can be invoked Java calls these methods An object usually does something when we invoke a method Objects have state The state is represented by the stored values of attributes in “fields” The state of an object is a “snapshot” of that object at a particular moment in time e.g. the class Student might have An attribute studentNumber, that never changes, and An attribute booksBorrowed, that does change 10
11
Object state Notice the types of the fields: int, String, boolean Types restrict the values that a field can take 11
12
Instances Multiple instances: Many similar objects can be created from a single class An object has attributes: values stored in fields The class defines what fields an object has Each object stores its own set of values (the state of the object) 12
13
A Java method /** * Move the circle horizontally by * 'distance' pixels. */ public void moveHorizontal(int distance) { erase(); xPosition += distance; draw(); } 13
14
About methods Methods often have parameters Parameters pass additional information needed to execute the method i.e. “input” to the method Parameters have types The type defines what kinds of values a parameter can take Methods may also return a result via a return value The head of a method is called its signature The signature describes the information needed to invoke the method Objects communicate by calling each other’s methods 14
15
Source code The source code of a class specifies three things The form of the state of each object i.e. what fields it has The behaviour of each object i.e. what methods it has How objects are created i.e. how the fields are initialized by a constructor A Java application is a collection of classes 15
16
Return values All of the methods in the figures project have void return types; but … … methods may return a result via a return value Such methods have a non-void return type More on this in future lectures 16
17
Summary of concepts introduced You should now be able to give an explanation of each of these terms object; class; method; parameter; signature; data type; multiple instances; state; method calling; source code; result Try it! Turn to code example 1 17
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.