Classes & Objects · 2009-09-15 · Classes & Objects Author: Boaz Kantor Created Date: 12/5/2008...
Transcript of Classes & Objects · 2009-09-15 · Classes & Objects Author: Boaz Kantor Created Date: 12/5/2008...
Classes & ObjectsAuthor: Boaz Kantor
The Interdisciplinary Center, Herzliya
Introduction to Computer Science
Winter 2008-9 Semester
© 2007-9 Boaz Kantor, IDC Herzliya
Agenda
Classes
Class Abstraction
Objects
Example: Ellipse Instantiation
Objects in the Real World
Constructors
References
Static Variables
Static Methods
Container/Utility Class
© 2007-9 Boaz Kantor, IDC Herzliya
message:
Type: StringBuffer
Value: “Average grade = 98.4”
Operations: append(String), toString(), …
Classes
int numOfStudents = 120;
int sumOfGrades = 11808;
double averageGrade =
(double)sumOfGrades/numOfStudents;
StringBuffer message = new StringBuffer ();
message.append(“Average grade = ”);
message.append(averageGrade);
System.out.println(message.toString());
© 2007-9 Boaz Kantor, IDC Herzliya
averageGrade:
Type: double
Value: 98.4
Class Abstraction
The definition of the class
What it looks like “on paper”
Class definition:◦ Name (mandatory)
◦ State (variables)
◦ Operations (methods)
How do we design?◦ Reason, logic
◦ Experience
◦ Creativity
◦ No school solution! (but bad solutions exist)
Abstraction vs. Implementation
© 2007-9 Boaz Kantor, IDC Herzliya
Objects
Realization of the abstraction
We can create many objects of the same class
All objects are defined the same
Each object has its own state (value)
Instantiation is done with “new”
© 2007-9 Boaz Kantor, IDC Herzliya
Class Ellipse
Object
orangeEllipseObject
greenEllipse
Object
redEllipse
Instantiation
Example: Ellipse Instantiation
Class Ellipse {
private String color;
public void setColor(String color) {
this.color = color;
}
}
© 2007-9 Boaz Kantor, IDC Herzliya
Class Ellipse
Object
orangeEllipseObject
greenEllipse
Object
redEllipse
Class SomeClass {
public static void main(String args[]) {
Ellipse redEllipse = new Ellipse();
redEllipse.setColor(“Red”);
}
}
InstantiationInstantiation
Example: Ellipse Instances
Class Ellipse {
private String color;
public void setColor(String color) {
this.color = color;
}
}
Class SomeClass {
public static void main(String args[]) {
Ellipse redEllipse = new Ellipse();
Ellipse greenEllipse = new Ellipse();
Ellipse orangeEllipse = new Ellipse();
redEllipse.setColor(“Red”);
greenEllipse.setColor(“Green”);
orangeEllipse.setColor(“Orange”);
greenEllipse.setColor(“Orange”);
}
}
Class Ellipse
Object
orangeEllipse
Object
greenEllipse
Object
redEllipse
Object
orangeEllipse
Object
greenEllipse
Object
redEllipse
Object
greenEllipse
© 2007-9 Boaz Kantor, IDC Herzliya
I am currently off
Objects in the Real World
© 2007-9 Boaz Kantor, IDC Herzliya
I am a TV set
Turn volume down
Switch channel up
Switch channel down
Turn me on! (or off)
I am currently on,
displaying channel 13,
volume level 17
Turn volume up
Class State Operations
Class Ellipse {
private String color;
public void setColor(String color) {
this.color = color;
}
}
Constructor
A method
Called when using “new”
Sets default state
© 2007-9 Boaz Kantor, IDC Herzliya
Class Ellipse {
private String color;
public void setColor(String color) {
this.color = color;
}
}
Example: Ellipse Instances
Class SomeClass {
public static void main(String args[]) {
Ellipse redEllipse = new Ellipse();
redEllipse.setColor(“Red”);
}
}
Class Ellipse
Object
redEllipse
Object
redEllipse
© 2007-9 Boaz Kantor, IDC Herzliya
References
Objects reside in memory
An object variable is like a finger, pointing
to the object location in memory
Ellipse redEllipse =
redEllipse.setColor(“Red”);
Ellipse otherEllipse = redEllipse;
otherEllipse.setColor(“Green”);
System.out.println(“Color: “ +
redEllipse.color);
© 2007-9 Boaz Kantor, IDC Herzliya
Memory
redEllipse reference
otherEllipse reference
new Ellipse();
Object EllipseObject EllipseObject Ellipse
Static Variables
Static variables belong to a class
Use with class name, not object
© 2007-9 Boaz Kantor, IDC Herzliya
Class Ellipse
Object
orangeEllipseObject
greenEllipse
Object
redEllipse
public static int changes;
© 2007-9 Boaz Kantor, IDC Herzliya
Class Ellipse {
private String color;
public void setColor(String color) {
this.color = color;
}
}
Class SomeClass {
public static void main(String args[]) {
System.out.println(“Changes: ” + );
Ellipse redEllipse = new Ellipse();
Ellipse orangeEllipse = new Ellipse();
redEllipse.setColor(“Red”);
System.out.println(“Changes: ” + );
orangeEllipse.setColor(“Orange”);
redEllipse.setColor(“Green”);
System.out.println(“Changes: ” + );
}
}
Static Methods
Static methods belong to a class
Use with class name, not object
© 2007-9 Boaz Kantor, IDC Herzliya
Class Ellipse
Object
orangeEllipseObject
greenEllipse
Object
redEllipse
public static int setChanges(int ch);
Container/Utility Class
A class with only static members
Has no state
Will never be instantiated
© 2007-9 Boaz Kantor, IDC Herzliya
Classes & ObjectsAuthor: Boaz Kantor
The Interdisciplinary Center, Herzliya
Introduction to Computer Science
Winter 2008-9 Semester
© 2007-9 Boaz Kantor, IDC Herzliya