SlideShare a Scribd company logo
1 of 26
Objects & Classes
Dr. M H B Ariyaratne
https://goo.gl/SM2SfF
What Is an Object?
● An object is a software bundle of related state and behavior
● Used to model the real-world objects that you find in everyday life
● Ex. A Dog, A Bicycle
● Real-world observations all translate into the world of object-oriented
programming.
Example Real World Object
● Dog Object
○ states
■ Name
■ Colour
■ Breed
■ hungry
○ behaviour
■ bark
■ wagging tail
Software Object
Conceptually similar to real-world objects
Stores its state in fields/variables
Exposes its behavior through methods
Knows hot to perform actions and communicate with others
A Person Driving from Point A to B
A B
A B
1. Create A Person Object
● States
○ Name: Buddhika
● Methods
○ Walk
○ Drive
2. Create a Car Object
● States
○ Name : LEAF
● Methods
○ Be Driven
Person Driving a Car
Bring the Objects Together
Exercise 1 - Identify Objects
Exercise 2
● Select one or the objects from Exercise 1
● Identify five states of that object
● Identify five behaviours of that Object
Exercise 3
● Identify states of a Patient Object
● Identify behaviours of a Patient Object
● Identify states of an Investigation Object
● Identify behaviours of an Investigation Object
● Identify states of a Medicine Object
● Identify Behaviours of Medicine Object
● Identify states of a Bill Object
● Identify behaviours of a Bill Object
● Identify states of a Bill Item Object
● Identify behaviours of a Bill Item Object
dm+d data Model
What Is a Class?
A blueprint or prototype from which objects are created
A Classification
Describes a thing
It has Properties/Attributes and Methods/Procedures
Advantages
● Modularity
○ source code for an object can be written and maintained independently of the source code for
other objects. Once created, an object can be easily passed around inside the system.
● Information-hiding
○ By interacting only with an object's methods, the details of its internal implementation remain
hidden from the outside world.
● Code reuse
○ If an object already exists (perhaps written by another software developer), you can use that
object in your program. This allows specialists to implement/test/debug complex, task-
specific objects, which you can then trust to run in your own code.
● Pluggability and debugging ease
○ If a particular object turns out to be problematic, you can simply remove it from your
application and plug in a different object as its replacement. This is analogous to fixing
mechanical problems in the real world. If a bolt breaks, you replace it, not the entire machine.
In the real world, you'll often find many individual objects all of the same kind.
There may be thousands of other bicycles in existence, all of the same make and
model. Each bicycle was built from the same set of blueprints and therefore
contains the same components.
In object-oriented terms, we say that a bicycle is an instance of the class of
objects known as bicycles. A class is the blueprint from which individual objects
are created.
OOP
Programming Technique & Software Design Philosophy
Model Objects instead of actions
Data NOT globally shared with the rest of the program
Program seen as interaction objects
Reusable objects with code reuse
Declare a Class
public class Dog {
String breed;
int ageC
String color;
void barking() {
}
void hungry() {
}
void sleeping() {
}
}
Constructor
● Every class has a constructor
● Can Explicitly write a constructor or Java compiler will builds a default
constructor for that class
● Each time a new object is created, at least one constructor will be invoked
● Should have the same name as the class
● A class can have more than one constructor.
Example of Constructor
public class Puppy {
public Puppy() {
}
public Puppy(String name) {
// This constructor has one parameter, name.
}
}
Variable Types
● Local variables −
○ defined inside methods, constructors or blocks
○ initialized within the method
○ destroyed when the method has completed
● Instance variables
○ defined within a class but outside any method
○ initialized when the class is instantiated
○ can be accessed from inside any method, constructor or blocks of that particular class.
● Class variables
○ declared within a class, outside any method
○ with the static keyword
Create an Object
Use “new” Keyword
1. Declaration − A variable declaration with a variable name with an object
type.
2. Instantiation − The 'new' keyword is used to create the object.
3. Initialization − The 'new' keyword is followed by a call to a constructor. This
call initializes the new object.
Example
Puppy myPuppy = new Puppy( "tommy" );
Accessing Instance Variables and Methods
/* First create an object */
ObjectReference = new Constructor();
/* Now call a variable as follows */
ObjectReference.variableName;
/* Now you can call a class method as follows */
ObjectReference.MethodName();
Source File Declaration Rules
● There can be only one public class per source file.
● A source file can have multiple non-public classes.
● The public class name should be the name of the source file as well which should be appended by
.java at the end
● If the class is defined inside a package, then the package statement should be the first statement
in the source file.
● If import statements are present, then they must be written between the package statement and
the class declaration. If there are no package statements, then the import statement should be
the first line in the source file.
● Import and package statements will imply to all the classes present in the source file. It is not
possible to declare different import and/or package statements to different classes in the source
file.
Exercise - Create Java Classes
Patient Class
Investigation Class
Medicine Class
Bill Class
Bill Item Class
Exercise - Create Java Objects
Patient Objects
Investigation Objects
Medicine Objects
Bill Objects
Bill Item Objects
Thank you

More Related Content

What's hot

[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOP[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOPMuhammad Hammad Waseem
 
Object as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younasObject as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younasShahzad Younas
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functionsHarsh Patel
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member FunctionsMuhammad Hammad Waseem
 
Object oriented programming concept- Saurabh Upadhyay
Object oriented programming concept- Saurabh UpadhyayObject oriented programming concept- Saurabh Upadhyay
Object oriented programming concept- Saurabh UpadhyaySaurabh Upadhyay
 
Classes and Objects in C#
Classes and Objects in C#Classes and Objects in C#
Classes and Objects in C#Adeel Rasheed
 
Friend function & friend class
Friend function & friend classFriend function & friend class
Friend function & friend classAbhishek Wadhwa
 
Object Oriented Programming - Basic Concepts
Object Oriented Programming - Basic ConceptsObject Oriented Programming - Basic Concepts
Object Oriented Programming - Basic ConceptsArunkumar Kupppuswamy
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)Ritika Sharma
 
CLTL python course: Object Oriented Programming (1/3)
CLTL python course: Object Oriented Programming (1/3)CLTL python course: Object Oriented Programming (1/3)
CLTL python course: Object Oriented Programming (1/3)Rubén Izquierdo Beviá
 
2- Introduction to java II
2-  Introduction to java II2-  Introduction to java II
2- Introduction to java IIGhadeer AlHasan
 
[OOP - Lec 09,10,11] Class Members & their Accessing
[OOP - Lec 09,10,11] Class Members & their Accessing[OOP - Lec 09,10,11] Class Members & their Accessing
[OOP - Lec 09,10,11] Class Members & their AccessingMuhammad Hammad Waseem
 
Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2Mahmoud Alfarra
 

What's hot (20)

[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOP[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOP
 
Object as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younasObject as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younas
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
 
java - oop's in depth journey
java - oop's in depth journeyjava - oop's in depth journey
java - oop's in depth journey
 
[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 
Object oriented programming concept- Saurabh Upadhyay
Object oriented programming concept- Saurabh UpadhyayObject oriented programming concept- Saurabh Upadhyay
Object oriented programming concept- Saurabh Upadhyay
 
Classes and Objects in C#
Classes and Objects in C#Classes and Objects in C#
Classes and Objects in C#
 
Classes and objects in java
Classes and objects in javaClasses and objects in java
Classes and objects in java
 
Friend function & friend class
Friend function & friend classFriend function & friend class
Friend function & friend class
 
Oops concepts
Oops conceptsOops concepts
Oops concepts
 
Object Oriented Programming - Basic Concepts
Object Oriented Programming - Basic ConceptsObject Oriented Programming - Basic Concepts
Object Oriented Programming - Basic Concepts
 
C# Types of classes
C# Types of classesC# Types of classes
C# Types of classes
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
CLTL python course: Object Oriented Programming (1/3)
CLTL python course: Object Oriented Programming (1/3)CLTL python course: Object Oriented Programming (1/3)
CLTL python course: Object Oriented Programming (1/3)
 
Basic concept of Object Oriented Programming
Basic concept of Object Oriented Programming Basic concept of Object Oriented Programming
Basic concept of Object Oriented Programming
 
2- Introduction to java II
2-  Introduction to java II2-  Introduction to java II
2- Introduction to java II
 
[OOP - Lec 09,10,11] Class Members & their Accessing
[OOP - Lec 09,10,11] Class Members & their Accessing[OOP - Lec 09,10,11] Class Members & their Accessing
[OOP - Lec 09,10,11] Class Members & their Accessing
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2
 

Similar to 8. objects & classes (20)

oopm 2.pdf
oopm 2.pdfoopm 2.pdf
oopm 2.pdf
 
Lecture 5.pptx
Lecture 5.pptxLecture 5.pptx
Lecture 5.pptx
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Md02 - Getting Started part-2
Md02 - Getting Started part-2Md02 - Getting Started part-2
Md02 - Getting Started part-2
 
Lesson 13 object and class
Lesson 13 object and classLesson 13 object and class
Lesson 13 object and class
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascript
 
Regex,functions, inheritance,class, attribute,overloding
Regex,functions, inheritance,class, attribute,overlodingRegex,functions, inheritance,class, attribute,overloding
Regex,functions, inheritance,class, attribute,overloding
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
Problem solving with python programming OOP's Concept
Problem solving with python programming OOP's ConceptProblem solving with python programming OOP's Concept
Problem solving with python programming OOP's Concept
 
oop.pptx
oop.pptxoop.pptx
oop.pptx
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
A350103
A350103A350103
A350103
 
Oop ppt
Oop pptOop ppt
Oop ppt
 
Question and answer Programming
Question and answer ProgrammingQuestion and answer Programming
Question and answer Programming
 
Java unit 7
Java unit 7Java unit 7
Java unit 7
 
Mca 504 dotnet_unit3
Mca 504 dotnet_unit3Mca 504 dotnet_unit3
Mca 504 dotnet_unit3
 
python interview prep question , 52 questions
python interview prep question , 52 questionspython interview prep question , 52 questions
python interview prep question , 52 questions
 
advance-dart.pptx
advance-dart.pptxadvance-dart.pptx
advance-dart.pptx
 

More from M H Buddhika Ariyaratne (17)

14. collections
14. collections14. collections
14. collections
 
13. interfaces
13. interfaces13. interfaces
13. interfaces
 
12. arrays
12. arrays12. arrays
12. arrays
 
11. java methods
11. java methods11. java methods
11. java methods
 
10. inheritance
10. inheritance10. inheritance
10. inheritance
 
9. strings
9. strings9. strings
9. strings
 
7. flowcharts and pseudocode
7. flowcharts and pseudocode7. flowcharts and pseudocode
7. flowcharts and pseudocode
 
6. java operators
6. java operators6. java operators
6. java operators
 
5. variables & data types
5. variables & data types5. variables & data types
5. variables & data types
 
4. mathematics for computing
4. mathematics for computing4. mathematics for computing
4. mathematics for computing
 
3. introduction to java
3. introduction to java3. introduction to java
3. introduction to java
 
2. data structures & algorithms
2. data structures & algorithms2. data structures & algorithms
2. data structures & algorithms
 
1. introduction mathematics for computing and object oriented programming
1. introduction   mathematics for computing  and  object oriented programming1. introduction   mathematics for computing  and  object oriented programming
1. introduction mathematics for computing and object oriented programming
 
Gis for healthcare introduction
Gis for healthcare   introductionGis for healthcare   introduction
Gis for healthcare introduction
 
Open hospital management information system
Open hospital management information systemOpen hospital management information system
Open hospital management information system
 
Who am i and what i have done
Who am i and what i have doneWho am i and what i have done
Who am i and what i have done
 
Electronic health record system for sri lankan general practitioners
Electronic health record system for sri lankan general practitionersElectronic health record system for sri lankan general practitioners
Electronic health record system for sri lankan general practitioners
 

Recently uploaded

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
 
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
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
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
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 

Recently uploaded (20)

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
 
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)
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
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...
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 

8. objects & classes

  • 1. Objects & Classes Dr. M H B Ariyaratne https://goo.gl/SM2SfF
  • 2. What Is an Object? ● An object is a software bundle of related state and behavior ● Used to model the real-world objects that you find in everyday life ● Ex. A Dog, A Bicycle ● Real-world observations all translate into the world of object-oriented programming.
  • 3. Example Real World Object ● Dog Object ○ states ■ Name ■ Colour ■ Breed ■ hungry ○ behaviour ■ bark ■ wagging tail
  • 4. Software Object Conceptually similar to real-world objects Stores its state in fields/variables Exposes its behavior through methods Knows hot to perform actions and communicate with others
  • 5. A Person Driving from Point A to B A B A B
  • 6. 1. Create A Person Object ● States ○ Name: Buddhika ● Methods ○ Walk ○ Drive
  • 7. 2. Create a Car Object ● States ○ Name : LEAF ● Methods ○ Be Driven
  • 8. Person Driving a Car Bring the Objects Together
  • 9. Exercise 1 - Identify Objects
  • 10. Exercise 2 ● Select one or the objects from Exercise 1 ● Identify five states of that object ● Identify five behaviours of that Object
  • 11. Exercise 3 ● Identify states of a Patient Object ● Identify behaviours of a Patient Object ● Identify states of an Investigation Object ● Identify behaviours of an Investigation Object ● Identify states of a Medicine Object ● Identify Behaviours of Medicine Object ● Identify states of a Bill Object ● Identify behaviours of a Bill Object ● Identify states of a Bill Item Object ● Identify behaviours of a Bill Item Object dm+d data Model
  • 12. What Is a Class? A blueprint or prototype from which objects are created A Classification Describes a thing It has Properties/Attributes and Methods/Procedures
  • 13. Advantages ● Modularity ○ source code for an object can be written and maintained independently of the source code for other objects. Once created, an object can be easily passed around inside the system. ● Information-hiding ○ By interacting only with an object's methods, the details of its internal implementation remain hidden from the outside world. ● Code reuse ○ If an object already exists (perhaps written by another software developer), you can use that object in your program. This allows specialists to implement/test/debug complex, task- specific objects, which you can then trust to run in your own code. ● Pluggability and debugging ease ○ If a particular object turns out to be problematic, you can simply remove it from your application and plug in a different object as its replacement. This is analogous to fixing mechanical problems in the real world. If a bolt breaks, you replace it, not the entire machine.
  • 14. In the real world, you'll often find many individual objects all of the same kind. There may be thousands of other bicycles in existence, all of the same make and model. Each bicycle was built from the same set of blueprints and therefore contains the same components. In object-oriented terms, we say that a bicycle is an instance of the class of objects known as bicycles. A class is the blueprint from which individual objects are created.
  • 15. OOP Programming Technique & Software Design Philosophy Model Objects instead of actions Data NOT globally shared with the rest of the program Program seen as interaction objects Reusable objects with code reuse
  • 16. Declare a Class public class Dog { String breed; int ageC String color; void barking() { } void hungry() { } void sleeping() { } }
  • 17. Constructor ● Every class has a constructor ● Can Explicitly write a constructor or Java compiler will builds a default constructor for that class ● Each time a new object is created, at least one constructor will be invoked ● Should have the same name as the class ● A class can have more than one constructor.
  • 18. Example of Constructor public class Puppy { public Puppy() { } public Puppy(String name) { // This constructor has one parameter, name. } }
  • 19. Variable Types ● Local variables − ○ defined inside methods, constructors or blocks ○ initialized within the method ○ destroyed when the method has completed ● Instance variables ○ defined within a class but outside any method ○ initialized when the class is instantiated ○ can be accessed from inside any method, constructor or blocks of that particular class. ● Class variables ○ declared within a class, outside any method ○ with the static keyword
  • 20. Create an Object Use “new” Keyword 1. Declaration − A variable declaration with a variable name with an object type. 2. Instantiation − The 'new' keyword is used to create the object. 3. Initialization − The 'new' keyword is followed by a call to a constructor. This call initializes the new object.
  • 21. Example Puppy myPuppy = new Puppy( "tommy" );
  • 22. Accessing Instance Variables and Methods /* First create an object */ ObjectReference = new Constructor(); /* Now call a variable as follows */ ObjectReference.variableName; /* Now you can call a class method as follows */ ObjectReference.MethodName();
  • 23. Source File Declaration Rules ● There can be only one public class per source file. ● A source file can have multiple non-public classes. ● The public class name should be the name of the source file as well which should be appended by .java at the end ● If the class is defined inside a package, then the package statement should be the first statement in the source file. ● If import statements are present, then they must be written between the package statement and the class declaration. If there are no package statements, then the import statement should be the first line in the source file. ● Import and package statements will imply to all the classes present in the source file. It is not possible to declare different import and/or package statements to different classes in the source file.
  • 24. Exercise - Create Java Classes Patient Class Investigation Class Medicine Class Bill Class Bill Item Class
  • 25. Exercise - Create Java Objects Patient Objects Investigation Objects Medicine Objects Bill Objects Bill Item Objects

Editor's Notes

  1. https://goo.gl/SM2SfF