You are on page 1of 26

Objects and Classes I

Instructor: Dr. Fahed Jubair


Object-Oriented Programming
• A programming paradigm that is based on ”objects”
• An object represents an entity in the real world
• An object is defined by a state and a behavior.
• A state is a set of data fields that describe attributes of this object
• An object behavior is a set of methods that describe actions that can be taken by
this object
• Example: “BankAccount” is an object that has
• A state (owner, balance, currency, creationDate, etc)
• A behavior (deposit, withdraw, freeze, etc)

© All rights reserved. 2


Classes in Java
• Java uses classes to represent objects
• A class is a template (i.e., a blueprint) that describes data fields and
methods of a certain object
• A java program is simply a set of classes, where a class may instantiate
other classes
• A Java program simulates the real world by describing objects and their
interactions
• In a java program, an object only has one class (i.e., template) but may
many instances of this class

© All rights reserved. 3


Example: Bank Account Class
Attributes

Constructor

1. What are the keywords


Methods public and private do?
2. What is the job of a
constructor?

© All rights reserved. 4


Example: Bank Account Test Class
Use new operator to instantiate a class

Use dot (.) operator to access


methods of a class

Class instances are used a reference


© All rights reserved. 5
Constructors
• A constructor must have the same name as the class itself
• Constructors do not have a return type (not even void) because they are
not methods
• Constructors are invoked when using the new operator to instantiate a
class
• Multiple constructors can be specified using overloading
• If no constructor is provided by the programmer, then the default no-
argument constructor will be used and all attributes will be given default
values
• 0 for numerics
• False for Booleans
• Null for objects
© All rights reserved. 6
Constructor Overloading
Overloading can be used to have
more than one constructor

“this” keyword refers to the current instance

© All rights reserved. 7


Packages
Package name

• In Java, a package is a directory that


usually contains related class
• Packages are useful for organizing your
code
• Java has built-in packages. Examples:
• java.io: contain classes responsible for
reading and writing to files
• java.lang: Java’s language fundamental classes
(String, Math, Exception, etc)
• java.util: miscellaneous utility classes
(Scanner, data structures, etc).

© All rights reserved. 8


Encapsulation
• A class encapsulates (bundles) its data fields and methods that
manipulate those data fields
• The private keyword is used to prevent outside classes from accessing those data
fields and methods

• A class exposes services (i.e., functionalities) that are allowed to be


accessed by other classes
• The public keyword is used to allow outside classes to access these methods, i.e,
public classes define the “interface” to the outside world

© All rights reserved. 9


Setter and Getter Methods
A good practice is to always declare
data fields as private

• Use setter methods if you want to allow


the outside world to modify attributes
• Setter methods are also called mutators

• Use getter methods if you want to allow the


outside world to access attributes
• Getter methods are also classed accessors

Question: where is the


© All rights reserved. constructor in this class? 10
The keyword final
The final keyword is used to declare
read-only variables, i.e., variables that
cannot be modified except when
instantiated inside the constructor

© All rights reserved. 11


Static Variables
A static variable is shared by all
instances of BankAccount

© All rights reserved. 12


Static Methods

static methods can be called


without instantiation

© All rights reserved. 13


Test Example

Use class name to access static methods

© All rights reserved. 14


Array of Objects

An array can hold objects, the same way


to holding primitive data types

© All rights reserved. 15


Passing Objects to Methods

Objects are passed by reference

A built-in function in Java that


returns a random real number
between 0 and 1
(inclusive of 0, but not of 1)

© All rights reserved. 16


Constants
• Constant variable are declared as
static & final variables
• Question: why is it okay to declare
constants as public attributes?

© All rights reserved. 17


Class Activity 1
• Create a class called Point2D, which represents a point in the 2D
cartesian space
• Point2D class has two attributes: x and y, which are real numbers that
represent the coordinates
• The coordinates x and y must be read-only variables
• Implement getter methods

© All rights reserved. 18


Special Methods: toString
• All classes in Java have the below method, by default:
public String toString() { … }
• The toString method returns a string representation of a class
• The toString method is called automatically when you attempt to
print the class using System.out.println
• The toString method has a default behavior: it returns the memory
address of an object
• It is often useful to override the toString method to determine how
the “stringified” name of a class is printed.

© All rights reserved. 19


toString Example

@Override annotation indicates that


this function is being overridden to
change its default behavior

© All rights reserved. 20


toString Test

// output
Pet [age=3, name=cat, species=Felidae]
Pet [age=2, name=dog, species=Canidae]

© All rights reserved. 21


Class Activity 2
• Add toString method to the Point2D class that you created in
activity 1
• Create a test for Point2D class to test your implementation of the
toString method

© All rights reserved. 22


Class Activity 3
• Create a class called Circle2D, which has two read-only attributes:
• Point2D center
• double radius
• Add a constructor that requires both attributes to be given
• Add getter methods
• Add toString method
• Create a test for Circle2D class to test your implementation of the all
method

© All rights reserved. 23


Unified Modeling Language (UML)
• UML is a standardized visual representation for classes
• UML diagrams are useful for getting a quick overview of your design
• A UML diagram represents a class by a rectangle with three parts, as
shown in below

Class Name
Use – to indicate private members
Attributes Use + to indicate public members

Methods

© All rights reserved. 24


Example
The Student Class
Student

-name: String
-regId: String
UML diagram -GPA: double
-major: String
<<create>> Student (name:String, regId:String)
+getName (): String
+getRegId (): String
+getGPA (): double
+setGPA (GPA:double): void
+getMajor (): String
+setMajor (major:String): void

© All rights reserved. 25


Home Activity

• Use one of the available online tools to


draw a UML diagram for the Time class
• Examples:
• https://www.lucidchart.com/
• https://www.draw.io/

© All rights reserved. 26

You might also like