250 likes | 341 Views
MIT AITI 2004 Lecture 7. Classes and Objects - Part I. The GradeBook Example. How we coded the GradeBook . . . Constructed an array of grades Wrote methods to perform operations on the array Passed the array as an argument to these methods. Procedural Programming.
E N D
MIT AITI 2004Lecture 7 Classes and Objects - Part I
The GradeBook Example • How we coded the GradeBook . . . • Constructed an array of grades • Wrote methods to perform operations on the array • Passed the array as an argument to these methods
Procedural Programming • The GradeBook we coded is an example of procedural programming • The program is written as one long sequence of operations • Sequences of operations can be placed into methods/functions/procedures.
A GradeBook Object • A new idea for our grade book program . . . • Construct a GradeBook object that stores an array of grades inside it • GradeBook has operations like printGrades and meanGrade to others can use. • Our code constructs a GradeBook and asks it to perform its operations.
Object-Oriented Programming • Creating a GradeBook object is an example of object-oriented programming • In OOP, we create objects that encapsulate both data and operations • Instead of creating one long sequence of operations, we create objects and define how they interact.
What is an Object? • An Object has two primary components: • state – properties of the object • behavior – operations the object can perform • Examples
Objects in Java • A class defines a new type of Object • To create a Object type to represent a light switch . . . class LightSwitch { // state and behavior here }
Fields • An Object's state is stored in variables called fields • Fields are declared (and optionally initialized) inside the braces of the class • Light switch example with a field . . . class LightSwitch { boolean on = true; }
Methods • An Object's behavior is defined by its methods • Methods, like fields, are written inside the braces of the class • Methods can access the fields (the state) of their object and can change them
Light Switch Example class LightSwitch { boolean on = true; // field boolean isOn() { // returns return on; // the state } void switch() { // changes on = !on; // the state } }
Constructing Objects • We use the new keyword to construct a new instance of an Object • We can assign this instance to a variable with the same type of the Object • Note: classes define new datatypes ! LightSwitch ls = new LightSwitch();
Using Fields and Methods • To access the field of an instance of an Object use instance.field • To access the method of an instance use instance.method(arguments) LightSwitch ls = new LightSwitch(); ls.on; ls.isOn(); ls.switch();
Example Using Light Switch • What does this main method print out? LightSwitch ls = new LightSwitch(); System.out.println(ls.on); ls.switch(); System.out.println(ls.isOn()); true false
Person Example class Person { String name = "Greg"; int age = 23; String getName() { return name; } void setName(String n) { name = n; } int getAge() { return age; } void setAge(int a) { age = a; } }
Constructing Person Objects • To create an instance of the Person class with a name of "George" and an age of 22 • Can we create a Person that has the name George and the age 22 from the moment it is created? • Answer: Yes! Person george = new Person(); george.setName("George"); george.setAge(22);
Constructors • Constructors are special methods used to construct an instance of a class • They have no return type • They have the same name as the class of the Object they are constructing • They initialize the state of the Object • Call the constructor by preceding it with the new keyword
Person Constructor class Person { String name; int age; Person(String n, int a) { name = n; age = a; } // . . . } • Now we can construct George as follows: Person george = new Person("George", 22);
Default Constructor • When you do not write a constructor in a class, it implicitly has a constructor with no arguments and an empty body • Result: every class has a constructor class LightSwitch { // Leaving out the constructor // is the same as . . . LightSwitch() {} }
Multiple Constructors • A class can have multiple constructors class LightSwitch { boolean on; LightSwitch() { on = true; } LightSwitch(boolean o) { on = o; } }
This Keyword • Instance can refer to itself with the keyword this class LightSwitch { boolean on; LightSwitch() { on = true; // this.on = true } LightSwitch(boolean on) { this.on = on; } }
Cascading Constructors • A constructor can call another constructor with this(arguments) class LightSwitch { boolean on; LightSwitch() { this(true); } LightSwitch(boolean on) { this.on = on; } }
Classes Recap • Classes have fields to store the state of the objects in the class and methods to provide the operations the objects can perform. • We construct instances of a class with the keyword new followed by a call a constructor method of the class. • We can assign an instance to a variable with a datatype named the same as the class. • If you do not provide a constructor, the class will have one with no arguments and no statements by default.
Apple Example class Apple { String color; double price; Apple(String color, double price) { this.color = color; this.price = price; } Apple(double price) { this("green", price); } String getColor() { return color; } double getPrice() { return price; } void setPrice(double p) { price = p; } }
Apple Quiz • What will these lines print out? Apple a = new Apple("red", 100.0); System.out.println(a.getColor()); System.out.println(a.getPrice()); a.setPrice(50.5); System.out.println(a.getPrice()); Apple b = new Apple(74.6); System.out.println(b.getColor()); System.out.println(b.getPrice()); b.setPrice(a.getPrice()); System.out.println(b.getPrice()); red 100.0 50.5 green 74.6 50.5