SlideShare a Scribd company logo
Object – Oriented Programming
Week 7 – Objects
Ferdin Joe John Joseph, PhD
Faculty of Information Technology
Thai-Nichi Institute of Technology
Classes and Objects in Java
3
Classes and Objects
• A Java program consists of one or more
classes
• A class is an abstract description of objects
• Here is an example class:
– class Dog { ...description of a dog goes here... }
• Here are some objects of that class:
Faculty of Information Technology,
Thai-Nichi Institute of Technology
4
More Objects
• Here is another example of a class:
– class Window { ... }
• Here are some examples of Windows:
Faculty of Information Technology,
Thai-Nichi Institute of Technology
5
Classes contain data definitions
• Classes describe the data held by each of its
objects
• Example:
– class Dog {
String name;
int age;
...rest of the class...
}
Data usually goes first in a class
• A class may describe any number of objects
– Examples: "Fido", 3; "Rover", 5; "Spot", 3;
• A class may describe a single object, or even no objects at
all Faculty of Information Technology,
Thai-Nichi Institute of Technology
6
Classes contain methods
• A class may contain methods that describe the behavior of
objects
• Example:
– class Dog {
...
void bark() {
System.out.println("Woof!");
}
}
Methods usually go after the data
• When we ask a particular Dog to bark, it says “Woof!”
• Only Dog objects can bark; the class Dog cannot bark
Faculty of Information Technology,
Thai-Nichi Institute of Technology
7
Methods contain statements
• A statement causes the object to do
something
– (A better word would be “command”—but it
isn’t)
• Example:
– System.out.println("Woof!");
– This causes the particular Dog to “print”
(actually, display on the screen) the
characters Woof!
Faculty of Information Technology,
Thai-Nichi Institute of Technology
8
Methods may contain temporary
data
• Data described in a class exists in all objects
of that class
– Example: Every Dog has its own name and age
• A method may contain local temporary data
that exists only until the method finishes
• Example:
– void wakeTheNeighbors( ) {
int i = 50; // i is a temporary variable
while (i > 0) {
bark( );
i = i – 1;
} Faculty of Information Technology,
Thai-Nichi Institute of Technology
9
Diagram of program structure
• A program consists
of one or more
classes
• Typically, each class
is in a separate .java
file
Program
File File
File
File
Class
Variables
Constructors
Methods
Variables
Variables
Statements
Statements
Faculty of Information Technology,
Thai-Nichi Institute of Technology
10
Summary
• A program consists of one or more classes
• A class is a description of a kind of object
– In most cases, it is the objects that do the actual work
• A class describes data, constructors, and methods
– An object’s data is information about that object
– An object’s methods describe how the object behaves
– A constructor is used to create objects of the class
• Methods (and constructors) may contain temporary data
and statements (commands)
Faculty of Information Technology,
Thai-Nichi Institute of Technology
Writing and running programs
• When you write a program, you are writing classes and all
the things that go into classes
• Your program typically contains commands to create
objects (that is, “calls” to constructors)
– Analogy: A class is like a cookie cutter, objects are like cookies.
• When you run a program, it creates objects, and those
objects interact with one another and do whatever they do
to cause something to happen
– Analogy: Writing a program is like writing the rules to a game;
running a program is like actually playing the game
• You never know how well the rules are going to work until
you try them out
Faculty of Information Technology,
Thai-Nichi Institute of Technology
Getting started
• Question: Where do objects come from?
– Answer: They are created by other objects.
• Question: Where does the first object come from?
– Answer: Programs have a special main method, not part of any
object, that is executed in order to get things started
• public static void main(String[ ] args) {
Dog fido = new Dog("Fido", 5); // creates a Dog
}
• The special keyword static says that the main method
belongs to the class itself, not to objects of the class
– Hence, the main method can be “called” before any objects are
created
– Usually, the main method gets things started by creating one or
more objects and telling them what to do
Faculty of Information Technology,
Thai-Nichi Institute of Technology
12
13
A bad program
• public class Dog {
String name;
int age;
Dog(String name, int age) {
this.name = name;
this.age = age;
}
public static void main(String[] args) {
bark();
}
void bark() {
System.out.println("Woof!");
}
}
--------- new Dog("Fido", 5).bark();
Faculty of Information Technology,
Thai-Nichi Institute of Technology
14
A complete program
class Dog {
String name;
int age;
Dog(String n, int age) {
name = n;
this.age = age;
}
void bark() {
System.out.println("Woof!");
}
void wakeTheNeighbors( ) {
int i = 50;
while (i > 0) {
bark( );
i = i – 1;
}
}
public static void main(String[ ] args) {
Dog fido = new Dog("Fido", 5);
fido.wakeTheNeighbors();
}
} // ends the class
Faculty of Information Technology,
Thai-Nichi Institute of Technology

More Related Content

What's hot

How would you implement multiple inheritance in java
How would you implement multiple inheritance in javaHow would you implement multiple inheritance in java
How would you implement multiple inheritance in java
Tyagi2636
 
Introduction to oop
Introduction to oop Introduction to oop
Introduction to oop
Kumar
 
Methods and constructors
Methods and constructorsMethods and constructors
Methods and constructors
Ravi_Kant_Sahu
 

What's hot (17)

C# Summer course - Lecture 1
C# Summer course - Lecture 1C# Summer course - Lecture 1
C# Summer course - Lecture 1
 
How would you implement multiple inheritance in java
How would you implement multiple inheritance in javaHow would you implement multiple inheritance in java
How would you implement multiple inheritance in java
 
SKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPTSKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPT
 
Introduction to oop
Introduction to oop Introduction to oop
Introduction to oop
 
INHERITANCE-Oopc ppt-ta4
INHERITANCE-Oopc ppt-ta4INHERITANCE-Oopc ppt-ta4
INHERITANCE-Oopc ppt-ta4
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oops
 
C#, OOP introduction and examples
C#, OOP introduction and examplesC#, OOP introduction and examples
C#, OOP introduction and examples
 
Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented Programming
 
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
 
Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented Programming
 
4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT
 
Java programming -Object-Oriented Thinking- Inheritance
Java programming -Object-Oriented Thinking- InheritanceJava programming -Object-Oriented Thinking- Inheritance
Java programming -Object-Oriented Thinking- Inheritance
 
Need of object oriented programming
Need of object oriented programmingNeed of object oriented programming
Need of object oriented programming
 
Abstract class and Interface
Abstract class and InterfaceAbstract class and Interface
Abstract class and Interface
 
Methods and constructors
Methods and constructorsMethods and constructors
Methods and constructors
 
Java object oriented programming concepts - Brainsmartlabs
Java object oriented programming concepts - BrainsmartlabsJava object oriented programming concepts - Brainsmartlabs
Java object oriented programming concepts - Brainsmartlabs
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 

Similar to Classes and Objects

Similar to Classes and Objects (20)

Pj01 x-classes and objects
Pj01 x-classes and objectsPj01 x-classes and objects
Pj01 x-classes and objects
 
Object concepts
Object conceptsObject concepts
Object concepts
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Object concepts
Object conceptsObject concepts
Object concepts
 
oop.pptx
oop.pptxoop.pptx
oop.pptx
 
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 concepts
Oop conceptsOop concepts
Oop concepts
 
Concepts of oop1
Concepts of oop1Concepts of oop1
Concepts of oop1
 
130704798265658191
130704798265658191130704798265658191
130704798265658191
 
Lecture 1 - Objects and classes
Lecture 1 - Objects and classesLecture 1 - Objects and classes
Lecture 1 - Objects and classes
 
Lecture 5.pptx
Lecture 5.pptxLecture 5.pptx
Lecture 5.pptx
 
Java for beginners
Java for beginnersJava for beginners
Java for beginners
 
Object Oriented Programming (OOP) Introduction
Object Oriented Programming (OOP) IntroductionObject Oriented Programming (OOP) Introduction
Object Oriented Programming (OOP) Introduction
 
OOP History and Core Concepts
OOP History and Core ConceptsOOP History and Core Concepts
OOP History and Core Concepts
 
introduction of Object oriented programming
introduction of Object oriented programmingintroduction of Object oriented programming
introduction of Object oriented programming
 
OOAD chapter 1
OOAD chapter 1 OOAD chapter 1
OOAD chapter 1
 
Object-Oriented concepts.pptx
Object-Oriented concepts.pptxObject-Oriented concepts.pptx
Object-Oriented concepts.pptx
 
OOP Presentation.pptx
OOP Presentation.pptxOOP Presentation.pptx
OOP Presentation.pptx
 
OOP Presentation.pptx
OOP Presentation.pptxOOP Presentation.pptx
OOP Presentation.pptx
 
python note.pdf
python note.pdfpython note.pdf
python note.pdf
 

More from Ferdin Joe John Joseph PhD

More from Ferdin Joe John Joseph PhD (20)

Invited Talk DGTiCon 2022
Invited Talk DGTiCon 2022Invited Talk DGTiCon 2022
Invited Talk DGTiCon 2022
 
Week 12: Cloud AI- DSA 441 Cloud Computing
Week 12: Cloud AI- DSA 441 Cloud ComputingWeek 12: Cloud AI- DSA 441 Cloud Computing
Week 12: Cloud AI- DSA 441 Cloud Computing
 
Week 11: Cloud Native- DSA 441 Cloud Computing
Week 11: Cloud Native- DSA 441 Cloud ComputingWeek 11: Cloud Native- DSA 441 Cloud Computing
Week 11: Cloud Native- DSA 441 Cloud Computing
 
Week 10: Cloud Security- DSA 441 Cloud Computing
Week 10: Cloud Security- DSA 441 Cloud ComputingWeek 10: Cloud Security- DSA 441 Cloud Computing
Week 10: Cloud Security- DSA 441 Cloud Computing
 
Week 9: Relational Database Service Alibaba Cloud- DSA 441 Cloud Computing
Week 9: Relational Database Service Alibaba Cloud- DSA 441 Cloud ComputingWeek 9: Relational Database Service Alibaba Cloud- DSA 441 Cloud Computing
Week 9: Relational Database Service Alibaba Cloud- DSA 441 Cloud Computing
 
Week 7: Object Storage Service Alibaba Cloud- DSA 441 Cloud Computing
Week 7: Object Storage Service Alibaba Cloud- DSA 441 Cloud ComputingWeek 7: Object Storage Service Alibaba Cloud- DSA 441 Cloud Computing
Week 7: Object Storage Service Alibaba Cloud- DSA 441 Cloud Computing
 
Week 6: Server Load Balancer and Auto Scaling Alibaba Cloud- DSA 441 Cloud Co...
Week 6: Server Load Balancer and Auto Scaling Alibaba Cloud- DSA 441 Cloud Co...Week 6: Server Load Balancer and Auto Scaling Alibaba Cloud- DSA 441 Cloud Co...
Week 6: Server Load Balancer and Auto Scaling Alibaba Cloud- DSA 441 Cloud Co...
 
Week 5: Elastic Compute Service (ECS) with Alibaba Cloud- DSA 441 Cloud Compu...
Week 5: Elastic Compute Service (ECS) with Alibaba Cloud- DSA 441 Cloud Compu...Week 5: Elastic Compute Service (ECS) with Alibaba Cloud- DSA 441 Cloud Compu...
Week 5: Elastic Compute Service (ECS) with Alibaba Cloud- DSA 441 Cloud Compu...
 
Week 4: Big Data and Hadoop in Alibaba Cloud - DSA 441 Cloud Computing
Week 4: Big Data and Hadoop in Alibaba Cloud - DSA 441 Cloud ComputingWeek 4: Big Data and Hadoop in Alibaba Cloud - DSA 441 Cloud Computing
Week 4: Big Data and Hadoop in Alibaba Cloud - DSA 441 Cloud Computing
 
Week 3: Virtual Private Cloud, On Premise, IaaS, PaaS, SaaS - DSA 441 Cloud C...
Week 3: Virtual Private Cloud, On Premise, IaaS, PaaS, SaaS - DSA 441 Cloud C...Week 3: Virtual Private Cloud, On Premise, IaaS, PaaS, SaaS - DSA 441 Cloud C...
Week 3: Virtual Private Cloud, On Premise, IaaS, PaaS, SaaS - DSA 441 Cloud C...
 
Week 2: Virtualization and VM Ware - DSA 441 Cloud Computing
Week 2: Virtualization and VM Ware - DSA 441 Cloud ComputingWeek 2: Virtualization and VM Ware - DSA 441 Cloud Computing
Week 2: Virtualization and VM Ware - DSA 441 Cloud Computing
 
Week 1: Introduction to Cloud Computing - DSA 441 Cloud Computing
Week 1: Introduction to Cloud Computing - DSA 441 Cloud ComputingWeek 1: Introduction to Cloud Computing - DSA 441 Cloud Computing
Week 1: Introduction to Cloud Computing - DSA 441 Cloud Computing
 
Sept 6 2021 BTech Artificial Intelligence and Data Science curriculum
Sept 6 2021 BTech Artificial Intelligence and Data Science curriculumSept 6 2021 BTech Artificial Intelligence and Data Science curriculum
Sept 6 2021 BTech Artificial Intelligence and Data Science curriculum
 
Hadoop in Alibaba Cloud
Hadoop in Alibaba CloudHadoop in Alibaba Cloud
Hadoop in Alibaba Cloud
 
Cloud Computing Essentials in Alibaba Cloud
Cloud Computing Essentials in Alibaba CloudCloud Computing Essentials in Alibaba Cloud
Cloud Computing Essentials in Alibaba Cloud
 
Transforming deep into transformers – a computer vision approach
Transforming deep into transformers – a computer vision approachTransforming deep into transformers – a computer vision approach
Transforming deep into transformers – a computer vision approach
 
Week 11: Programming for Data Analysis
Week 11: Programming for Data AnalysisWeek 11: Programming for Data Analysis
Week 11: Programming for Data Analysis
 
Week 10: Programming for Data Analysis
Week 10: Programming for Data AnalysisWeek 10: Programming for Data Analysis
Week 10: Programming for Data Analysis
 
Week 9: Programming for Data Analysis
Week 9: Programming for Data AnalysisWeek 9: Programming for Data Analysis
Week 9: Programming for Data Analysis
 
Week 8: Programming for Data Analysis
Week 8: Programming for Data AnalysisWeek 8: Programming for Data Analysis
Week 8: Programming for Data Analysis
 

Recently uploaded

一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
ewymefz
 
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
axoqas
 
一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单
ewymefz
 
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
ewymefz
 
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
vcaxypu
 
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
vcaxypu
 
Computer Presentation.pptx ecommerce advantage s
Computer Presentation.pptx ecommerce advantage sComputer Presentation.pptx ecommerce advantage s
Computer Presentation.pptx ecommerce advantage s
MAQIB18
 
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
yhkoc
 
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
ewymefz
 
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
nscud
 
Professional Data Engineer Certification Exam Guide  _  Learn  _  Google Clou...
Professional Data Engineer Certification Exam Guide  _  Learn  _  Google Clou...Professional Data Engineer Certification Exam Guide  _  Learn  _  Google Clou...
Professional Data Engineer Certification Exam Guide  _  Learn  _  Google Clou...
Domenico Conte
 
Investigate & Recover / StarCompliance.io / Crypto_Crimes
Investigate & Recover / StarCompliance.io / Crypto_CrimesInvestigate & Recover / StarCompliance.io / Crypto_Crimes
Investigate & Recover / StarCompliance.io / Crypto_Crimes
StarCompliance.io
 
一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单
enxupq
 

Recently uploaded (20)

Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
 
Jpolillo Amazon PPC - Bid Optimization Sample
Jpolillo Amazon PPC - Bid Optimization SampleJpolillo Amazon PPC - Bid Optimization Sample
Jpolillo Amazon PPC - Bid Optimization Sample
 
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
 
一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单
 
社内勉強会資料_LLM Agents                              .
社内勉強会資料_LLM Agents                              .社内勉強会資料_LLM Agents                              .
社内勉強会資料_LLM Agents                              .
 
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
 
2024-05-14 - Tableau User Group - TC24 Hot Topics - Tableau Pulse and Einstei...
2024-05-14 - Tableau User Group - TC24 Hot Topics - Tableau Pulse and Einstei...2024-05-14 - Tableau User Group - TC24 Hot Topics - Tableau Pulse and Einstei...
2024-05-14 - Tableau User Group - TC24 Hot Topics - Tableau Pulse and Einstei...
 
Using PDB Relocation to Move a Single PDB to Another Existing CDB
Using PDB Relocation to Move a Single PDB to Another Existing CDBUsing PDB Relocation to Move a Single PDB to Another Existing CDB
Using PDB Relocation to Move a Single PDB to Another Existing CDB
 
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
 
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
 
Computer Presentation.pptx ecommerce advantage s
Computer Presentation.pptx ecommerce advantage sComputer Presentation.pptx ecommerce advantage s
Computer Presentation.pptx ecommerce advantage s
 
Adjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTESAdjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTES
 
tapal brand analysis PPT slide for comptetive data
tapal brand analysis PPT slide for comptetive datatapal brand analysis PPT slide for comptetive data
tapal brand analysis PPT slide for comptetive data
 
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
 
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
 
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
 
Professional Data Engineer Certification Exam Guide  _  Learn  _  Google Clou...
Professional Data Engineer Certification Exam Guide  _  Learn  _  Google Clou...Professional Data Engineer Certification Exam Guide  _  Learn  _  Google Clou...
Professional Data Engineer Certification Exam Guide  _  Learn  _  Google Clou...
 
Investigate & Recover / StarCompliance.io / Crypto_Crimes
Investigate & Recover / StarCompliance.io / Crypto_CrimesInvestigate & Recover / StarCompliance.io / Crypto_Crimes
Investigate & Recover / StarCompliance.io / Crypto_Crimes
 
一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单
 

Classes and Objects

  • 1. Object – Oriented Programming Week 7 – Objects Ferdin Joe John Joseph, PhD Faculty of Information Technology Thai-Nichi Institute of Technology
  • 3. 3 Classes and Objects • A Java program consists of one or more classes • A class is an abstract description of objects • Here is an example class: – class Dog { ...description of a dog goes here... } • Here are some objects of that class: Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 4. 4 More Objects • Here is another example of a class: – class Window { ... } • Here are some examples of Windows: Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 5. 5 Classes contain data definitions • Classes describe the data held by each of its objects • Example: – class Dog { String name; int age; ...rest of the class... } Data usually goes first in a class • A class may describe any number of objects – Examples: "Fido", 3; "Rover", 5; "Spot", 3; • A class may describe a single object, or even no objects at all Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 6. 6 Classes contain methods • A class may contain methods that describe the behavior of objects • Example: – class Dog { ... void bark() { System.out.println("Woof!"); } } Methods usually go after the data • When we ask a particular Dog to bark, it says “Woof!” • Only Dog objects can bark; the class Dog cannot bark Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 7. 7 Methods contain statements • A statement causes the object to do something – (A better word would be “command”—but it isn’t) • Example: – System.out.println("Woof!"); – This causes the particular Dog to “print” (actually, display on the screen) the characters Woof! Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 8. 8 Methods may contain temporary data • Data described in a class exists in all objects of that class – Example: Every Dog has its own name and age • A method may contain local temporary data that exists only until the method finishes • Example: – void wakeTheNeighbors( ) { int i = 50; // i is a temporary variable while (i > 0) { bark( ); i = i – 1; } Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 9. 9 Diagram of program structure • A program consists of one or more classes • Typically, each class is in a separate .java file Program File File File File Class Variables Constructors Methods Variables Variables Statements Statements Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 10. 10 Summary • A program consists of one or more classes • A class is a description of a kind of object – In most cases, it is the objects that do the actual work • A class describes data, constructors, and methods – An object’s data is information about that object – An object’s methods describe how the object behaves – A constructor is used to create objects of the class • Methods (and constructors) may contain temporary data and statements (commands) Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 11. Writing and running programs • When you write a program, you are writing classes and all the things that go into classes • Your program typically contains commands to create objects (that is, “calls” to constructors) – Analogy: A class is like a cookie cutter, objects are like cookies. • When you run a program, it creates objects, and those objects interact with one another and do whatever they do to cause something to happen – Analogy: Writing a program is like writing the rules to a game; running a program is like actually playing the game • You never know how well the rules are going to work until you try them out Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 12. Getting started • Question: Where do objects come from? – Answer: They are created by other objects. • Question: Where does the first object come from? – Answer: Programs have a special main method, not part of any object, that is executed in order to get things started • public static void main(String[ ] args) { Dog fido = new Dog("Fido", 5); // creates a Dog } • The special keyword static says that the main method belongs to the class itself, not to objects of the class – Hence, the main method can be “called” before any objects are created – Usually, the main method gets things started by creating one or more objects and telling them what to do Faculty of Information Technology, Thai-Nichi Institute of Technology 12
  • 13. 13 A bad program • public class Dog { String name; int age; Dog(String name, int age) { this.name = name; this.age = age; } public static void main(String[] args) { bark(); } void bark() { System.out.println("Woof!"); } } --------- new Dog("Fido", 5).bark(); Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 14. 14 A complete program class Dog { String name; int age; Dog(String n, int age) { name = n; this.age = age; } void bark() { System.out.println("Woof!"); } void wakeTheNeighbors( ) { int i = 50; while (i > 0) { bark( ); i = i – 1; } } public static void main(String[ ] args) { Dog fido = new Dog("Fido", 5); fido.wakeTheNeighbors(); } } // ends the class Faculty of Information Technology, Thai-Nichi Institute of Technology