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 objectoriented 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 -price: double
Fields
+setCode(String) +getCode(): String +setDescription(String) +getDescription(): String +setPrice(double) +getPrice(): double +getFormattedPrice(): String
Methods
How to Define a Class Class Declaration Instance Variables
/* Comments */ public class Product { private String code; private String description; private double price; public String getCode() {... } private String getDescription() {... } private double getPrice() {... } public void setCode(String newTitle) {... } private void setDescription(String newDescription) {... } private void setPrice(String newPrice) {... } public String getFormattedPrice() {... }
Methods
}
How to Define a Class Class Declaration Instance Variables
/* Comments */ public class Song { private String title; private String artist;
Song -title -artist
public String getTitle() {... } private String getArtist() {... }
Methods
+getTitle() +getArtist() +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
classes
Instructor
objects
leigh
name sfsu# department college office#
name: Leigh Jin sfsu#: 111111111 department: IS college: CoB office#: 332
Student
kelly
name sfsu# major degree GPA
name: Kelly Paul Sfsu#: 333333333 major: finance degree: MBA 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();