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
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.
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