SlideShare una empresa de Scribd logo
1 de 21
OBJECTS AND CLASSES
SYED AFAQ ALI SHAH
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
• 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
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
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
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
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
• A class is a blue print from which individual objects are created. A
sample of a class is given below:
8
Class
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
9
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
11
Object state
• Notice the types of the fields:
int, String, boolean
• Types restrict the values that a field can take
12
• Example of creating an object is given below:
13
Creating Objects
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)
14
• Methods can have parameters (arguments) to provide
additional information for a task.
• The header of a method is called its signature. It
provides information needed to invoke that method.
• The additional values that some methods require are
called parameters.
• When calling, for example, the moveHorizontal
void moveHorizontal(int distance)
This is called the signature of the method.
• The part enclosed by parentheses (int distance) is the
information about the required parameter.
• For each parameter, it defines a type and a name.
15
About methods
SCI101 Object Oriented Computing, (c) 2014 16
Parameters (Cont …)
A Java method
/**
* Move the circle horizontally by
* 'distance' pixels.
*/
public void moveHorizontal(int distance)
{
erase();
xPosition += distance;
draw();
}
17
• Parameters have types. The type defines what kinds of
values a parameter can take.
• A type specifies what kind of data can be passed to a
parameter. The type int signifies whole numbers (also
called “integer” numbers, hence the abbreviation “int”).
• If it has a parameter, the type and name of that
parameter is displayed.
18
Data Types
• In the list of methods for a circle class, you will see one
method with a different parameter type: the “color”
method has a parameter of type String.
• The string type indicates that a section of text (for
example, a word or a sentence) is expected.
• Strings are always enclosed within double quotes. For
example, to enter the word red as a string, type:
"red"
• Java supports several other data types, including
decimal numbers and characters.
19
1.5 Data Types (Cont …)
• Result mean methods may return information about an
object via a return value. Such methods have a non-void
return type
• Some methods may have void return types
• Suppose student class has changeName method.
• The signature of changeName states:
void changeName(String replacementName)
• The word void indicates that this method does not return
any result.
• Methods with return values enable us to get information
from an object via a method call.
20
Return Values
• In this lecture, we have explored the basics of classes and
objects.
• We have discussed the fact that objects are specified by
classes.
• Classes represent the general concept of things, while objects
represent concrete instances of a class. We can have many
objects of any class.
• Objects have methods that we use to communicate with
them.
• We can use a method to make a change to the object or to
get information from the object.
• Methods can have parameters, and parameters have types.
• Methods have return types, which specify what type of data
they return.
• If the return type is void, they do not return anything.
SCI101 Object Oriented Computing, (c) 2014 21
Summary
• Objects store data in fields (which also have types).
• All the data values of an object together are referred to
as the object’s state.
• Objects are created from class definitions that have
been written in a particular programming language.
• Much of programming in Java is about learning to write
class definitions.
• A large Java program will have many classes, each with
many methods that call each other in many different
ways.
• To learn to develop Java programs, we need to learn
how to write class definitions, including fields and
methods, and how to put these classes together well.
SCI101 Object Oriented Computing, (c) 2014 22
Summary (Cont …)

Más contenido relacionado

La actualidad más candente (20)

Java Data Types and Variables
Java Data Types and VariablesJava Data Types and Variables
Java Data Types and Variables
 
[OOP - Lec 06] Classes and Objects
[OOP - Lec 06] Classes and Objects[OOP - Lec 06] Classes and Objects
[OOP - Lec 06] Classes and Objects
 
[OOP - Lec 01] Introduction to OOP
[OOP - Lec 01] Introduction to OOP[OOP - Lec 01] Introduction to OOP
[OOP - Lec 01] Introduction to OOP
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Object Oriented Paradigm
Object Oriented ParadigmObject Oriented Paradigm
Object Oriented Paradigm
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
 
Oops concept on c#
Oops concept on c#Oops concept on c#
Oops concept on c#
 
C# by Zaheer Abbas Aghani
C# by Zaheer Abbas AghaniC# by Zaheer Abbas Aghani
C# by Zaheer Abbas Aghani
 
Oop java
Oop javaOop java
Oop java
 
Java data types, variables and jvm
Java data types, variables and jvm Java data types, variables and jvm
Java data types, variables and jvm
 
CLASS & OBJECT IN JAVA
CLASS & OBJECT  IN JAVACLASS & OBJECT  IN JAVA
CLASS & OBJECT IN JAVA
 
Lect 1-class and object
Lect 1-class and objectLect 1-class and object
Lect 1-class and object
 
3. Data types and Variables
3. Data types and Variables3. Data types and Variables
3. Data types and Variables
 
Data types
Data typesData types
Data types
 
Introducing classes
Introducing classesIntroducing classes
Introducing classes
 
Introduction to oop and java fundamentals
Introduction to oop and java fundamentalsIntroduction to oop and java fundamentals
Introduction to oop and java fundamentals
 
Object-oriented concepts
Object-oriented conceptsObject-oriented concepts
Object-oriented concepts
 
Oops Concept Java
Oops Concept JavaOops Concept Java
Oops Concept Java
 
Quick Scala
Quick ScalaQuick Scala
Quick Scala
 

Similar a Lecture 1 - Objects and classes

Ch 2 Library Classes.pptx
Ch 2 Library Classes.pptxCh 2 Library Classes.pptx
Ch 2 Library Classes.pptxKavitaHegde4
 
Ch 2 Library Classes.pdf
Ch 2 Library Classes.pdfCh 2 Library Classes.pdf
Ch 2 Library Classes.pdfKavitaHegde4
 
L9 wrapper classes
L9 wrapper classesL9 wrapper classes
L9 wrapper classesteach4uin
 
Object Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptxObject Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptxethiouniverse
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptxEpsiba1
 
Object Oriented Programming.pptx
Object Oriented Programming.pptxObject Oriented Programming.pptx
Object Oriented Programming.pptxSAICHARANREDDYN
 
Introduction to OOP with java
Introduction to OOP with javaIntroduction to OOP with java
Introduction to OOP with javaSujit Kumar
 
02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.pptYonas D. Ebren
 
It 405 materi 3 objek dan kelas
It 405 materi 3   objek dan kelasIt 405 materi 3   objek dan kelas
It 405 materi 3 objek dan kelasAyi Purbasari
 
ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in javaAtul Sehdev
 

Similar a Lecture 1 - Objects and classes (20)

Ch 2 Library Classes.pptx
Ch 2 Library Classes.pptxCh 2 Library Classes.pptx
Ch 2 Library Classes.pptx
 
Ch 2 Library Classes.pdf
Ch 2 Library Classes.pdfCh 2 Library Classes.pdf
Ch 2 Library Classes.pdf
 
O6u CS-315A OOP Lecture (1).pdf
O6u CS-315A OOP Lecture (1).pdfO6u CS-315A OOP Lecture (1).pdf
O6u CS-315A OOP Lecture (1).pdf
 
C# by Zaheer Abbas Aghani
C# by Zaheer Abbas AghaniC# by Zaheer Abbas Aghani
C# by Zaheer Abbas Aghani
 
Java tutorial part 3
Java tutorial part 3Java tutorial part 3
Java tutorial part 3
 
L9 wrapper classes
L9 wrapper classesL9 wrapper classes
L9 wrapper classes
 
Lecture 5.pptx
Lecture 5.pptxLecture 5.pptx
Lecture 5.pptx
 
Object Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptxObject Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptx
 
9 cm604.14
9 cm604.149 cm604.14
9 cm604.14
 
Object concepts
Object conceptsObject concepts
Object concepts
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptx
 
Object Oriented Programming.pptx
Object Oriented Programming.pptxObject Oriented Programming.pptx
Object Oriented Programming.pptx
 
Java
JavaJava
Java
 
Object concepts
Object conceptsObject concepts
Object concepts
 
Introduction to OOP with java
Introduction to OOP with javaIntroduction to OOP with java
Introduction to OOP with java
 
02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt
 
It 405 materi 3 objek dan kelas
It 405 materi 3   objek dan kelasIt 405 materi 3   objek dan kelas
It 405 materi 3 objek dan kelas
 
oop 3.pptx
oop 3.pptxoop 3.pptx
oop 3.pptx
 
ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in java
 
Java
Java Java
Java
 

Más de Syed Afaq Shah MACS CP

Lecture 7- Iterator and for loop over arrays
Lecture 7- Iterator and for loop over arraysLecture 7- Iterator and for loop over arrays
Lecture 7- Iterator and for loop over arraysSyed Afaq Shah MACS CP
 
Lecture 5 - Interaction with for each and while loops
Lecture 5 - Interaction with for each and while loopsLecture 5 - Interaction with for each and while loops
Lecture 5 - Interaction with for each and while loopsSyed Afaq Shah MACS CP
 
Lecture 4 - Object Interaction and Collections
Lecture 4 - Object Interaction and CollectionsLecture 4 - Object Interaction and Collections
Lecture 4 - Object Interaction and CollectionsSyed Afaq Shah MACS CP
 
Lecture 3 Conditionals, expressions and Variables
Lecture 3   Conditionals, expressions and VariablesLecture 3   Conditionals, expressions and Variables
Lecture 3 Conditionals, expressions and VariablesSyed Afaq Shah MACS CP
 
Lecture 2 - Classes, Fields, Parameters, Methods and Constructors
Lecture 2 - Classes, Fields, Parameters, Methods and ConstructorsLecture 2 - Classes, Fields, Parameters, Methods and Constructors
Lecture 2 - Classes, Fields, Parameters, Methods and ConstructorsSyed Afaq Shah MACS CP
 

Más de Syed Afaq Shah MACS CP (7)

Lecture 8 Library classes
Lecture 8 Library classesLecture 8 Library classes
Lecture 8 Library classes
 
Lecture 7- Iterator and for loop over arrays
Lecture 7- Iterator and for loop over arraysLecture 7- Iterator and for loop over arrays
Lecture 7- Iterator and for loop over arrays
 
Lecture 6 - Arrays
Lecture 6 - ArraysLecture 6 - Arrays
Lecture 6 - Arrays
 
Lecture 5 - Interaction with for each and while loops
Lecture 5 - Interaction with for each and while loopsLecture 5 - Interaction with for each and while loops
Lecture 5 - Interaction with for each and while loops
 
Lecture 4 - Object Interaction and Collections
Lecture 4 - Object Interaction and CollectionsLecture 4 - Object Interaction and Collections
Lecture 4 - Object Interaction and Collections
 
Lecture 3 Conditionals, expressions and Variables
Lecture 3   Conditionals, expressions and VariablesLecture 3   Conditionals, expressions and Variables
Lecture 3 Conditionals, expressions and Variables
 
Lecture 2 - Classes, Fields, Parameters, Methods and Constructors
Lecture 2 - Classes, Fields, Parameters, Methods and ConstructorsLecture 2 - Classes, Fields, Parameters, Methods and Constructors
Lecture 2 - Classes, Fields, Parameters, Methods and Constructors
 

Último

DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Intelisync
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 

Último (20)

DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 

Lecture 1 - Objects and classes

  • 1. OBJECTS AND CLASSES SYED AFAQ ALI SHAH
  • 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. • A class is a blue print from which individual objects are created. A sample of a class is given below: 8 Class
  • 9. 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 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 11
  • 11. Object state • Notice the types of the fields: int, String, boolean • Types restrict the values that a field can take 12
  • 12. • Example of creating an object is given below: 13 Creating Objects
  • 13. 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) 14
  • 14. • Methods can have parameters (arguments) to provide additional information for a task. • The header of a method is called its signature. It provides information needed to invoke that method. • The additional values that some methods require are called parameters. • When calling, for example, the moveHorizontal void moveHorizontal(int distance) This is called the signature of the method. • The part enclosed by parentheses (int distance) is the information about the required parameter. • For each parameter, it defines a type and a name. 15 About methods
  • 15. SCI101 Object Oriented Computing, (c) 2014 16 Parameters (Cont …)
  • 16. A Java method /** * Move the circle horizontally by * 'distance' pixels. */ public void moveHorizontal(int distance) { erase(); xPosition += distance; draw(); } 17
  • 17. • Parameters have types. The type defines what kinds of values a parameter can take. • A type specifies what kind of data can be passed to a parameter. The type int signifies whole numbers (also called “integer” numbers, hence the abbreviation “int”). • If it has a parameter, the type and name of that parameter is displayed. 18 Data Types
  • 18. • In the list of methods for a circle class, you will see one method with a different parameter type: the “color” method has a parameter of type String. • The string type indicates that a section of text (for example, a word or a sentence) is expected. • Strings are always enclosed within double quotes. For example, to enter the word red as a string, type: "red" • Java supports several other data types, including decimal numbers and characters. 19 1.5 Data Types (Cont …)
  • 19. • Result mean methods may return information about an object via a return value. Such methods have a non-void return type • Some methods may have void return types • Suppose student class has changeName method. • The signature of changeName states: void changeName(String replacementName) • The word void indicates that this method does not return any result. • Methods with return values enable us to get information from an object via a method call. 20 Return Values
  • 20. • In this lecture, we have explored the basics of classes and objects. • We have discussed the fact that objects are specified by classes. • Classes represent the general concept of things, while objects represent concrete instances of a class. We can have many objects of any class. • Objects have methods that we use to communicate with them. • We can use a method to make a change to the object or to get information from the object. • Methods can have parameters, and parameters have types. • Methods have return types, which specify what type of data they return. • If the return type is void, they do not return anything. SCI101 Object Oriented Computing, (c) 2014 21 Summary
  • 21. • Objects store data in fields (which also have types). • All the data values of an object together are referred to as the object’s state. • Objects are created from class definitions that have been written in a particular programming language. • Much of programming in Java is about learning to write class definitions. • A large Java program will have many classes, each with many methods that call each other in many different ways. • To learn to develop Java programs, we need to learn how to write class definitions, including fields and methods, and how to put these classes together well. SCI101 Object Oriented Computing, (c) 2014 22 Summary (Cont …)