330 likes | 353 Views
Computer Science 1 (Studio) 06A-Objects and Classes. What is Object Oriented Programming?. OOP is a practice which gained mainstream acceptance in the 1990’s thanks to languages like C++ and Java The goal behind OOP is to organize a program in a way which mirrors the real world
E N D
Computer Science 1 (Studio) 06A-Objects and Classes CS1s - 06A-Objects and Classes (v1.00)
What is Object Oriented Programming? • OOP is a practice which gained mainstream acceptance in the 1990’s thanks to languages like C++ and Java • The goal behind OOP is to organize a program in a way which mirrors the real world • The real world is composed entirely of objects which have personal attributes and activities • A Java program can be viewed as a series of objects which interact with each other CS1s - 06A-Objects and Classes (v1.00)
What is an Object? • The state defines the current object, and the behavior defines what the object can do • Consider a Circle object: the state is the current value/s of the data fields radius = 5 r=5 find Area the behavior is the set of supported methods find Circumference The real world OOP CS1s - 06A-Objects and Classes (v1.00)
What is an Object? • So an object encapsulates both its state and behaviors into one entity • The object is the singular, unifying construct radius = 5.0 find Area find Circumference CS1s - 06A-Objects and Classes (v1.00)
What is a Class? • A class is a blueprint for creating objects of the same type • A class defines the complete collection of data (properties) and methods (actions) • Consider a Circle class in Java: class Circle { double radius = 5.0; double findArea() { return radius * radius * Math.PI; } double findCircumference() { return 2 * Math.PI * radius; } } class name data methods CS1s - 06A-Objects and Classes (v1.00)
Objects vs Classes • In class activity: • Consider your Java textbook, “Introduction to Java Programming”. • Based on how this book is represented in the real world, draw a picture of the object • What is the state of this object? Identify the nouns you come up with when describing this textbook • What are its behaviors? Identify the verbs which describe what you can do with a book • Refer back to the Circle object for help • Now, define a Java class for this object • What would the name of the class be? • What would the class look like as Java code (data/methods)? • Refer back to the Circle class for help CS1s - 06A-Objects and Classes (v1.00)
Objects vs Classes • Identify each of the following as being an object or a class • Movie...Star Wars • Rose...Flower • Band...Pink Floyd • Acura NSX...Car • Programming Language...Java • College...RIT • Guinness...Beer CS1s - 06A-Objects and Classes (v1.00)
Instantiation • When you create an object from a class, we call that object an instance of that class • “Object” and “instance” are used interchangeably • You can create many instances from the same class • Creating an instance is referred to as instantiation radius = 5 radius = 5 radius = 5 find Area find Area find Area find Circumference find Circumference find Circumference CS1s - 06A-Objects and Classes (v1.00)
Instantiation • One class has the ability of generate any number of objects whose type is a Circle class Circle { double radius = 5.0; double findArea() { return radius * radius * Math.PI; } double findCircumference() { return 2 * Math.PI * radius; } } class instantiates radius = 5 radius = 5 radius = 5 objects find... find... find... find... find... find... CS1s - 06A-Objects and Classes (v1.00)
Instantiation • Each Circle object shares the same collection of behaviors and data • However, each Circle object maintains its own individual values for the data, and it’s own address in memory radius = 5 radius = 2 radius = 3 find... find... find... find... find... find... CS1s - 06A-Objects and Classes (v1.00)
Creating Objects • Objects are created using the new operator, followed by the class name new ClassName(); • To create a circle object: new Circle(); radius = 5 find... find... CS1s - 06A-Objects and Classes (v1.00)
Object Reference Variables • In order to use an object, we must have some way to get to it (usually by name) • A name that refers to an object is a reference variable • To create a reference variable, use the class name following by the reference variable name ClassNameobjectReference; • To create a reference to a Circle called myCircle: CirclemyCircle; myCircle reference name null reference value reference type Circle CS1s - 06A-Objects and Classes (v1.00)
Putting It All Together • To combine the entire process into one statement: Circle myCircle = new Circle(); • We now have a Circle reference called myCircle. It refers to the Circle object created by new on the right. myCircle radius = 5 address find... find... Circle CS1s - 06A-Objects and Classes (v1.00)
Primitives vs. Objects • With primitive types, like int, the variable holds the actual value (i.e. an integer) int myInt = 10; • Arrays are objects and the array name is a reference variable . The value of the reference variable is a reference to the object int myIntArray[] = new int [ 5 ]; myInt 10 int myIntArray 0 0 0 0 0 address1 int[] CS1s - 06A-Objects and Classes (v1.00)
Primitives vs. Objects • Draw the memory diagram which represents each declaration statement: double myDouble = 5.5; Generator myGen = new Generator(); Circle myCircle1 = new Circle(); char myChar = ’A’; String str = new String(“Hello”); Circle myCircle2 = new Circle(); String str2 = new String(“Goodbye”); CS1s - 06A-Objects and Classes (v1.00)
Primitive Assignment • Regardless of whether you are dealing with a primitive or a reference, the behavior with assignment is to copy the value • Primitive assignment: int i = 10; int j; j = i; i j 10 ??? int int i j 10 10 int int value 10 is copied CS1s - 06A-Objects and Classes (v1.00)
Reference Assignment • Reference/object creation: Circle circle1 = new Circle(); Circle circle2 = new Circle(); circle1 radius = 5 address1 find... find... Circle different values for address circle2 radius = 5 address2 find... find... Circle CS1s - 06A-Objects and Classes (v1.00)
Reference Assignment • Reference assignment: circle2 = circle1; circle1 radius = 5 address1 find... find... Circle address in circle1 is copied into circle2 circle2 radius = 5 address1 find... find... Circle CS1s - 06A-Objects and Classes (v1.00)
Primitives vs. Objects • Draw the memory diagram which represents each assignment statement: double myDouble = 5.5; Generator myGen = new Generator(); Circle myCircle1 = new Circle(); char myChar = ’A’; String str = new String(“Hello”); Circle myCircle2 = new Circle(); String str2 = new String(“Goodbye”); myDouble = 10; myChar = ’B’; str2 = str; myCircle2 = myCircle1; CS1s - 06A-Objects and Classes (v1.00)
Garbage Collection • What happens to the bottom object? • Objects occupy memory, so there must be a way to free the space this object occupies if it is not used anymore circle1 radius = 5 address1 find... find... Circle circle2 radius = 5 address1 find... find... Circle CS1s - 06A-Objects and Classes (v1.00)
Garbage Collection • When there are no references to an object, that object is free to be destroyed • The Java runtime system detects garbage and automatically reclaims the space the object occupies circle1 radius = 5 address1 find... find... Circle circle2 address1 POOF! Circle CS1s - 06A-Objects and Classes (v1.00)
Java vs C/C++ • Garbage collection is a huge reason why Java is the preferred introductory language to C or C++ • There is no automated garbage collection in C/C++ • It is up to the programmer to manually handle the deallocation of any unused objects • Failure to manage memory in C/C++ will result in a memory leak, which can cause the program to crash (or behave strangely) CS1s - 06A-Objects and Classes (v1.00)
Garbage Collection • In Java, if you don’t want a reference variable to refer to an object, set the value of the reference to null Circle circle1 = new Circle(); circle1 = null; circle1 radius = 5 address1 find... find... Circle circle1 POOF! null Circle CS1s - 06A-Objects and Classes (v1.00)
Accessing an Object’s Data and Methods • To access the data and methods in an object, use the dot notation • To access the instance data in an object: referenceVariable.data • For example: Circle circle1 = new Circle(); double radius = circle1.radius; circle1 radius radius = 5 address1 5.0 find... find... Circle int CS1s - 06A-Objects and Classes (v1.00)
Accessing an Object’s Data and Methods • To invoke an object’s instance method: referenceVariable.method(arguments); • For example: double area = circle1.findArea(); circle1 area radius = 5 address1 78.648.. find... find... Circle int CS1s - 06A-Objects and Classes (v1.00)
TestCircle.java • In class activity • Write a program, TestCircle.java • There will be two classes in this file. The first class is the Circle class presented earlier in the notes. Put it at the very bottom of your file • The second class is called TestCircle which should go at the top of your file • The TestCircle class must have a main method inside it (Circle does not!) • Inside the main method for TestCircle, you should create a reference variable called myCircle and assign it to a newly created Circle object • Using the instance, myCircle, change its radius to 12 • Print out myCircle’s radius, area and the circumference CS1s - 06A-Objects and Classes (v1.00)
What’s Wrong? • What’s wrong with the following code snippets? Circle myCircle; System.out.println(myCircle.findArea()); Circle myCircle = new Circle(); System.out.println(myCircle.radius()); System.out.println(myCircle.findArea); System.out.println(Circle.findArea()); int x; System.out.println(x); new Circle().findArea(); CS1s - 06A-Objects and Classes (v1.00)
TestFrame.java • In class activity • Copy the following program over to your local directory: /usr/local/pub/sps/courses/cs1/TestFrame/TestFrame.java • Compile the program and run it • Now look at the source code and answer these questions: • How many objects are created? • How many references are created and what are their names? • How many methods are invoked on each instance and what are their names? • Do any of the instance methods take parameters? CS1s - 06A-Objects and Classes (v1.00)
Constructors • One problem with our Circle class is that all objects created have a radius of 5 • A constructor is a special method which can be used to initialize an object’s data • The constructor has the same name as the class • Like all methods, the constructor can be overloaded to receive different parameters • The constructor does not have a return type (not even void) CS1s - 06A-Objects and Classes (v1.00)
Constructors class Circle { double radius; Circle(double r) { radius = r; System.out.println(“Circle(” + radius + “)”); } Circle() { radius = 5.0; System.out.println(“Circle(” + radius + “)”; } double findArea() { return radius * radius * Math.PI; } double findCircumference() { return 2 * Math.PI * radius; } } CS1s - 06A-Objects and Classes (v1.00)
Constructors • An example of overloaded constructors Circle circle1 = new Circle(10); // “Circle(10)” Circle circle2 = new Circle(); // “Circle()” • Calling new invokes a class constructor • If a class does not define any constructors, a default constructor is defined implicitly • Common programming blunder: public void Circle() { //no no } CS1s - 06A-Objects and Classes (v1.00)
TestCircle.java • In class activity • Modify the TestCircle.java program so it adds two constructors for the Circle class • A default constructor which sets the radius to 1.0 • An overloaded constructor which takes a double and sets the radius to that parameter • In the main method of TestCircle, create two Circle instances • The first instance, circle1, should be created with the default constructors • The second instance, circle2, should be created with a radius of 10.0 • Print out the radius, area and circumference of both instances CS1s - 06A-Objects and Classes (v1.00)
Revision History • Revision History • v1.00, 10/11/2004 10:11 PM, sps Initial revision. CS1s - 06A-Objects and Classes (v1.00)