Classes and Objects: ISYS350 Leigh Jin
Classes and Objects: ISYS350 Leigh Jin
Classes and Objects: ISYS350 Leigh Jin
ISYS350
Leigh Jin
In Java, you should learn to...
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;
}
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)
{... }
}
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
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)
{
}
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();