01-303 F22 Object-Oriented Programming
01-303 F22 Object-Oriented Programming
CMPS 303
Dr. Osama Halabi
Data Structures
Publishing as Pearson Addison-Wesley
CMPS 303
Dr. Osama Halabi
Lecture
Text
Text
2
11
Chapter 4
Chapter 2
Making Decisions
Object-Oriented Programming
If, if/else, if/else if, switch
Game Design & Development 2 © Osama Halabi
Concept
✤ Object-Oriented Programming (OOP) = Designing the
application based on the objects discovered when
analyzing the problem
✤ A class is code that describes a particular type of object. It
specifies the data that an object can hold (attributes), and
the actions that an object can perform (methods).
✤ A class is a programmer-defined data type and
objects are variables of that type
A class contains attributes and methods
Game Design & Development 3 © Osama Halabi
Class vs. Object
Class
Template for
making Objects
Class is a “template” or
Object is an actual
“blueprint” that
describes the structure instance of a class.
of its objects (properties It holds values for
and methods). the properties.
getBalance
information about
balance
getOnwer
the object
setOnwer
✤Methods – functions
the object can
BankAccount contains attributes
perform
and methods
Game Design & Development 7 © Osama Halabi
Class Example
private data
Methods
Attributes
withdraw
deposit
accountNo
accountName
balance
getBalance
Data Structures © Osama Halabi & 2004 Pearson Education, Inc. &
8 © 2011 The McGraw-Hill Companies, Inc. All rights reserved.
UML Diagram and Java Code
Instance Variables
Constructors
Methods
Variables that are defined inside the class but outside all methods.
initialize the objects during public Account (int id, String name,
double balance) {
object construction. this.id = id;
this.name = name;
✤ Example
this.balance = balance;
}
Account saraAcct = new
Account(123, "Sara", 100.0);
Composition Inheritance
Every class has one and only one direct superclass (single inheritance). Java does
not support multiple inheritance
Every class in Java implicitly inherits class Object
Game Design & Development 19 © Osama Halabi
Static Variable
vs
Instance Variable
20
Instant Variable
Instance variables are created when an object is created with the use of the
key word 'new' and destroyed when the object is destroyed.
The instance variables are visible for all methods, constructors and block in
the class. Normally it is recommended to make these variables private
(access level).
Instance variables have default values. For numbers the default value is 0,
for Booleans it is false and for object references it is null. Instance variables
can be accessed directly by calling the variable name inside the class.
There would only be one copy of each Every instance of the class share the same static variable
class variable per class, regardless of Any change in the static variable can be seen by all objects
how many objects are created from it. (instances)
Static variables are created when the program starts and destroyed when the
program stops.
25
Static Methods
✤ A static method belongs to the class rather than the object (instance) of the class.
✦ Also known as “class methods” (vs. “instance methods”)
✤ A static method can only access the static attributes.
✦ Static methods can’t access instance methods and instance variables directly.
✤ A static method can be called without the need for creating an instance of
the class.
✦ You call a static method through the class name
ClassName.functionName(arguments);
✦ For example, the Math class has a static method called cos
‣ You can call Math.cos(3.5) without creating an object of the Math class
✤ E.g., the main method is a static method so the system can call it without first
creating an object
Game Design & Development © Osama Halabi
Static Method Example
class Calculator {
public static void printSum(int a, int b) {
System.out.println("Sum = " + (a + b));
}
}
public class Main {
public static void main(String[] args) {
Calculator.printSum(5, 10);
printHello();
}
public static void printHello () {
System.out.println("Hello");
}
}
Game Design & Development 27 © Osama Halabi
Generics
Generic Method
public static void print(Employee x[])
To write a method to print {
an array of employees. for (int i=0;i<x.length;i++)
System.out.println(x[i]);
}
✤Java includes support for writing generic classes and methods that can
operate on a variety of data types while often avoiding the need for
explicit casts.
✤The generics framework allows us to define a class in terms of a set of
formal type parameters, which can then be used as the declared type for
variables, parameters, and return values within the class definition.
✤Those formal type parameters are later specified when using the generic
class as a type elsewhere in a program.
Pair<String,Double>[] holdings;
/*
holdings = new Pair<String,Double>[25]; // illegal; compile error
*/
holdings = new Pair[25]; // correct, but warning about unchecked cast
holdings[0] = new Pair<>("ORCL", 32.07); // valid element assignment
public WaitingList(int s)
{
a=(E[])new Object[s];
count=0;
}
The object type is specified when creating an object of generic class, for example
WaitingList<Student> ws = new WaitingList<Student>(5);
Note that type parameters can represent only reference types, not primitive types
(like int, double and char).
Game Design & Development 36 © Osama Halabi
END OF
End of
Object Oriented
Programming