Review Classes and Objects A class is a

Review: Classes and Objects § A class is a template for creating objects. § Write each class in its own. java file of the same name. § Classes/objects have fields, methods, . . . Point. java public class Point { int x, y; public void translate(int dx, int dy) { x += dx; y += dy; }. . .

Review § Instance methods are called with: <object>. <method>(<args>) p 1. translate(3, 7); § Class methods have an added static in their declaration and are called with: <class>. <method>(<args>) Character. is. Letter(c);

Constructors // Point class public class Point { int x; int y; public Point(int xval, int yval) { x = xval; y = yval; } public void translate(int dx, int dy) { x += dx; . . . § A constructor is a special method that is used to set the initial value of fields when creating new objects. § It does not have a return type. § Example: Point p 1 = new Point(3, 4);

§ If you don't provide a constructor, a default constructor with no arguments is provided for you. It automatically initializes all the fields to "zero". § You are allowed to have multiple constructors, but they must all have different argument "signatures". § If you do provide a constructor, then you don't get the default constructor with no arguments. § If you want the default constructor too, you can write it. public Point() { // default to (0, 0) } public Point(int xval, int yval) { x = xval; y = yval; }

§ Using the constructor to simplify the testing code Point. Tester. java public class Point. Tester { public static void main(String[] args) { Point p 1 = new Point(3, 4); Point p 2 = new Point(5, 6); p 1. translate(3, 7); p 2. translate(3, 7); System. out. printf("p 1 = (%d, %d)n", p 1. x, p 1. y); System. out. printf("p 2 = (%d, %d)n", p 2. x, p 2. y); } }

Activity: § Add another constructor method to Point that takes a Random number object as a parameter and initializes the Point to random values. Point. java // Point class public class Point { int x; int y; public Point() { // default to (0, 0) } public Point(int xval, int yval) { x = xval; y = yval; }

Objects are stored differently than primitive types. § Primitive types are stored directly in memory. § Objects are stored elsewhere in memory and the associated variable contains a reference to that location. Memory a pt 7 int a = 7; Point pt = new Point(9, 20); { Point 9 20

§ When you copy a primitive type, it copies the value. § When you copy an object, it copies the reference. Memory int a Point b Point = 7; pt 1 = new Point(9, 20); = 8; pt 2 = new Point(15, 25); b = a; pt 2 = pt 1; a pt 1 b pt 2 Implication: Now modifying pt 2 will also modify Point pt 1. They both refer to the same object! { { Point 7 8 X 7 9 20 15 25

The same thing happens when you pass parameters to a method. Implication: § When you pass a primitive modifying pt 2 also modifies pt 1 Memory type, it copies the value. they both refer to the same object! § When you pass an object, it copies the reference. int a = 7; Point pt 1 = new Point(9, 20); moo(a, pt 1); . . . public static int moo(int b, Point pt 2) {. . . a pt 1 7 b pt 2 7 { Point 9 20
![Activity: What does this code print? public static void main(String[] args) { int a Activity: What does this code print? public static void main(String[] args) { int a](http://slidetodoc.com/presentation_image_h2/1f8c8d42f3aab238a969a3815e1eb272/image-10.jpg)
Activity: What does this code print? public static void main(String[] args) { int a = 7; Point pt 1 = new Point(10, 15); mystery(a, pt 1); } System. out. println("a=" + a); System. out. printf("pt 1=(%d, %d)", pt 1. x, pt 1. y); public static void mystery(int b, Point pt 2) { b += 3; pt 2. translate(b, b); }

The Implicit Parameter § When you call an instance method, the object being used is implicitly passed to that method. Memory Point. Tester. java. . . Point pa = new Point(9, 20); pa. translate(1, 2); . . . Point. java pa implicit parameter dx dy . . . public void translate(int dx, int dy) { x += dx; y += dy; }. . . { Point x y 1 2 9 20

§ Only the implicit parameter is accessed without specifying the object. § Any explicit parameters must specify the object. Point. Tester. java Memory . . . Point pa = new Point(9, 20); Point pb = new Point(6, 16); dist = pa. distance(pb); . . . Point. java pa pb implicit parameter P 2 . . . public double distance(Point p 2) { int dx = x - p 2. x; int dy = y - p 2. y; return Math. sqrt(dx*dx+dy*dy); } { { Point x y 9 20 6 16

Standard Class Organization public class Point { int x; int y; public Point() { // default to (0, 0) } public Point(int xval, int yval) { x = xval; y = yval; } public void translate(int dx, int dy) { x += dx; y += dy; } public double distance(Point p 2) { int dx = x - p 2. x; int dy = y - p 2. y; return Math. sqrt(dx*dx+dy*dy); } fields constructors methods

§ Goal of object oriented programming is to make programs easier to write and understand. Terms: § Abstraction: Hiding the internal details of how things work by providing an easy to understand interface. This allows us to use objects without understanding how they work internally. § Encapsulation: Protecting the internal details of an object from being visible outside the object. Example: new Drawing. Panel(400, 400)

Encapsulation § You can protect the fields of a class from outside tinkering with the private keyword. // Point class public class Point { private int x; private int y; public Point() { // default to (0, 0) } public Point(int xval, int yval) { x = xval; y = yval; }. . . § The methods defined within the class can access the fields (x and y), but code outside the class can no longer read or write these private fields!
![public class Point. Tester { public static void main(String[] args) { Point p 1 public class Point. Tester { public static void main(String[] args) { Point p 1](http://slidetodoc.com/presentation_image_h2/1f8c8d42f3aab238a969a3815e1eb272/image-16.jpg)
public class Point. Tester { public static void main(String[] args) { Point p 1 = new Point(3, 4); Point p 2 = new Point(5, 6); p 1. translate(3, 7); p 2. translate(3, 7); System. out. printf("p 1 = (%d, %d)n", p 1. x, p 1. y); System. out. printf("p 2 = (%d, %d)n", p 2. x, p 2. y); } } Unresolved compilation problems: The field Point. x is not visible The field Point. y is not visible § You can no longer say: int xval = p 1. x; or p 1. y = yval;

Sometimes we still want the user of a class to be able to read or change the fields. It is common practice to have: § “getter” methods for reading (“accessor” methods) § “setter” methods for changing (“mutator” methods) public class Point { private int x; private int y; public int get. X() { return x; } public int get. Y() { return y; } public void set. Location(int xval, int yval) { x = xval; y = yval; }
![public class Point. Tester { public static void main(String[] args) { Point p 1 public class Point. Tester { public static void main(String[] args) { Point p 1](http://slidetodoc.com/presentation_image_h2/1f8c8d42f3aab238a969a3815e1eb272/image-18.jpg)
public class Point. Tester { public static void main(String[] args) { Point p 1 = new Point(3, 4); Point p 2 = new Point(5, 6); p 1. translate(3, 7); p 2. translate(3, 7); System. out. printf("p 1 = (%d, %d)n", p 1. get. X(), p 1. get. Y()); System. out. printf("p 2 = (%d, %d)n", p 2. get. X(), p 2. get. Y()); } } § This seems silly here for such a simple class. § For a complex class like Font, we can't allow a user to change the size field directly. § The Font class contains glyphs that are created based on the size. § Changing the size field would cause an inconsistency. § The font class has a private size field and a getter: int get. Size();

Private Methods § You can have private methods too. § I call these "helper" methods. They are just meant to help perform the processing needed within this class. They are not intended to be called from outside the class. An example from the Font class: // Rebuild the glyphs when any font parameters are changed private void rebuild. Glyphs() {. . . } § When labeled as private, other classes can not call this method.
![The final piece of the GREAT MYSTERY public static void main(String[] args) main() is The final piece of the GREAT MYSTERY public static void main(String[] args) main() is](http://slidetodoc.com/presentation_image_h2/1f8c8d42f3aab238a969a3815e1eb272/image-20.jpg)
The final piece of the GREAT MYSTERY public static void main(String[] args) main() is a public method. § The main() method must be declared as public so that it can be called from outside the class. § In all of the programs so far in this course, all other methods could have been private, since we have had only 1 class, and thus they aren't called from outside that class.

Variable Shadowing public class Point { private int x; private int y; . . . public void set. Location(int xval, int yval) { x = xval; y = yval; } § What if instead of xval & yval, we had written: public void set. Location(int x, int y) { x = x; y = y; } § This is called variable shadowing.

The Keyword this § The keyword this refers to the implicit parameter object that was used to call the method. § this is used to overcome variable shadowing. public class Point { private int x; private int y; . . . public void set. Location(int x, int y) { this. x = x; this. y = y; }

Discussion Question: Why might you choose to have variable shadowing? public class Point { private int x; private int y; . . . public void set. Location(int x, int y) { this. x = x; this. y = y; }
- Slides: 23