200 likes | 210 Views
Learn about building new objects, accessing data, and creating constructors in Java. Discover how to protect private data and control access with getter and setter methods.
E N D
More on Objects More, more, more on Objects CS 102
Where Do You Want To Go Today? • Building new objects • Accessors • this and That
Caution, Objects Under Construction! • Objects have structure and structure has to be built new means create a new instance of an object • How are objects actually built? • Objects know how to do things, including how to build themselves
Constructor Methods • Constructors are special methods that build new objects • Declaration looks just like a method declaration that has no result type public class Circle { double x_center, y_center, radius; public Circle(double x, double y, double r) { x_center = x; y_center = y; radius = r; } }
A Few Notes on Constructors I • Don't have to declare a constructor • Java creates a default constructor public class Circle { public double x_center, y_center, radius; // Uses the default constructor public double Area() { return 3.141592654 * radius * radius; } }
A Few Notes on Constructors II • Constructors have the same name as the class • No return type (why not?) • Constructor declarations are not members • Never inherited, so no hiding or overriding (More on this later)
Multiple Constructors public Circle(double x, double y, double r) { x_center = x; y_center = y; radius = r; } public Circle() { x_center = 0; y_center = 0; radius = 1; } public Circle(double x, double y) { x_center = x; y_center = y; radius = 1; }
Handling Multiple Constructors • How does Java tell the constructors apart? • Method signatures • Signatures include: • Name of the method • Number formal parameters • Types of formal parameters • Doesn't include the name of the parameter
Method Signatures • For this method: public Circle(double x, double y, double r) • Signature is: Circle(double, double, double) • Different methods, same signature public Circle(double x, double y) public Circle(double x, double r)
Back to Multiple Constructors • Multiple constructors are okay • Each constructor has to have a unique signature (All of a class's methods have to have unique signatures, not just constructors) • Method overloading: multiple versions of the same method (but with different signatures)
The Linda Tripp Problem • How do we keep private data private? • Avoid having object1 mess with object2's data Example: Student objects know student's grades, but one student shouldn't modify another student's grades. • Storing invalid data • Stuffing 12-hr time into a 24-hr time object
If It's Public... • Start with: public class Circle { public double x_center, y_center; public double radius, area; public Circle(double x, double y) { x_center = x; y_center = y; radius = 1; area = 3.141592654 } }
…other objects can mess with it • Create a circle Circle duPont = new Circle(100, 100); // Change the area, but leave r the same duPont.Area = 11; // Now a circle with radius 1 has area 11
Using private • Declare variables to be private public class Circle { private double x_center, y_center; private double radius, area; : : } • Now only Circle can modify the variables (x_center, y_center, ...)
Granting Access • Other classes might need to read or write the values stored in private members • Create accessor functions • Set (or settor) • Get (or gettor) • Control access through get and set
Setting and Getting • What's the difference between using get and set methods, and just making everything public? • Two benefits • Protect instance variables from outside meddling • Insulates class users from changes in variables
Example from Fig 6.5 // blah, blah, blah. Set invalid values to zero. public void setTime( int h, int m, int s ) { setHour( h ); // set the hour setMinute( m ); // set the minute setSecond( s ); // set the second } // set the hour public void setHour( int h ) { hour = ( ( h >= 0 && h < 24 ) ? h : 0 ); } // set the minute public void setMinute( int m ) { minute = ( ( m >= 0 && m < 60 ) ? m : 0 ); } // set the second public void setSecond( int s ) { second = ( ( s >= 0 && s < 60 ) ? s : 0 ); }
Me, a Name I Call Myself • How can objects refer to themselves? • Every object has a built-in reference to itself, called this • this can be used only in: • Body of a method or constructor • Initializer of an instance variable • Anywhere else is a compile-time error
This is Bad, public class Circle { public double x, y; public double r, area; public Circle(double x, double y) { x = x; // This doesn't do much, because we're y = y; // just assigning parameters to themselves r = 1; area = 3.141592654 } }
But this is okay public class Circle { public double x, y; public double r, area; public Circle(double x, double y) { this.x = x; // Now we're assigning the instance this.y = y; // variables to the parameters r = 1; area = 3.141592654 } } Better to avoid this mess altogether by using different names for data members and method parameters