You are on page 1of 13

Classes and Objects

ISYS350
Leigh Jin
In Java, you should learn to...

• Organizing programs into classes, and use


these classes to create objects

• Defining a class by two aspects of its


structure:
– Attributes: what are the things that differentiate one
class of objects from another
– Methods: how objects should behave
Examples of Classes

ShoppingCart Button AlarmClock


-cartContents -label -alarmTime
+addToCart() -color -alarmMode
+removeFromCart() +setColor() +setAlarmTime()
+checkOut() +setLabel() +getAlarmTime()
+dePress() +isAlarmSet()
+unDepress() +snooze()
How encapsulation works
• The fields of a class store the data of a class.
• The methods of a class define the tasks that a class can perform.
Often, these methods provide a way to work with the fields of a
class.
• Encapsulation is one of the fundamental concepts of object-
oriented programming.
• With encapsulation, the fields in a class are defined as private and
can be hidden from other classes, and the methods in a class can
be modified or improved without changing the way that other
classes use them.
• get() and set() methods are created to allow public access to the
private fields that are hidden from public.
UML diagramming notes
• UML (Unified Modeling Language) is the industry standard used to
describe the classes and objects of an object-oriented application.
• The minus sign (-) in a UML class diagram marks the fields and
methods that can’t be accessed by other classes, while the plus sign
(+) signifies that access is allowed.
• For each field, the name is given, followed by a colon, followed by
the data type.
• For each method, the name is given, followed by a set of
parentheses, followed by a colon and the data type of the value
that’s going to be returned.
• If a method doesn’t require any parameters, the parentheses are left
empty. Otherwise, the data type of each parameter is listed in them.
A class diagram for the Product class

Product
-code: String
-description: String Fields
-price: double

+setCode(String)
+getCode(): String
+setDescription(String)
+getDescription(): String Methods
+setPrice(double)
+getPrice(): double
+getFormattedPrice(): String
How to Define a Class
/* Comments */
Class Declaration public class Product
{ private String code;
Instance Variables
private String description;
private double price;

public String getCode()


{... }
private String getDescription()
Methods
{... }
private double getPrice()
{... }
public void setCode(String newTitle)
{... }
private void setDescription(String newDescription)
{... }
private void setPrice(String newPrice)
{... }
public String getFormattedPrice()
{... }

}
How to Define a Class
/* Comments */
public class Song Song
Class Declaration
{ private String title; -title
Instance Variables
private String artist;
-artist
public String getTitle()
{... } +getTitle()
+getArtist()
private String getArtist()
Methods
{... } +setTitle()
public void setTitle(String newTitle) +setArtist()
{... }
+play()
private void setArtist(String newArtist)
{... }

public void play()


{... }

}
Class and Object
• A class is a template used to create
multiple objects with similar features.
Example: Mouse in general
• An object is also called an instance,
with very specific values of attributes.
Example: micky
• Class can be used as a data type, and
object as a variable.
So instead of int i;
You have: Mouse micky;
Instructor leigh;
Classes vs. Objects

name name
classes sfsu#
sfsu#
department major
Instructor college
Student degree
office# GPA

name: Leigh Jin name: Kelly Paul


objects sfsu#: 111111111 Sfsu#: 333333333

leigh department: IS kelly major: finance


college: CoB degree: MBA
office#: 332 GPA: 3.90
The relationship between a class and its objects
Product
-code: String
-description: String
-price: double

product1
-code = "java"
-description = "Murach's Beginning Java 2"
-price = 49.50
product2
-code = "mcb2"
-description = "Murach's Mainframe COBOL"
-price = 59.50
How to Create an object
/* Comments */
public class TestProduct
{ public static void main(String[ ] args)
{

Product product1 = new Product(“Java”, “Beginning Java”, 49.50);


Product product2 = new Prouct(“mcb2”, “Mainframe COBOL”, 59.50);
System.out.println(“Java Book Price: “+product1.getFormattedPrice());
System.out.println(“Cobal Book Price: “+product2.getFormattedPrice());

}
Object Oriented Basics in Java
• “Class” is similar to “general type”, and “object” is
similar to “unique individual”
• Two essential things to define a class are:
attributes (instance variables) and actions
(methods)
• There are essentially two types of Java
programs, those define classes, and those
intended to be run containing main() method
• The main() method usually involves creation of
objects, and objects act different methods
– Example: Dog myDog = new Dog(“Kyle”, “Poodle”);
myDog.bark();

You might also like