Quick Index
Coding Steps


Starting Point
Recall from the previous chapter that we setup the code for a class (with empty class body).

Let's now add the ivars (data).
public class Person {

}
Coding Ivars (Data)
Here is how we code an ivar
public class Person {

	private String firstName;

}
Explanation
Ivars are coded using this pattern:

  • private -- indicates the ivar is private meaning it is not directly accessible to object users
  • String -- this is the data type of the ivar (must be a valid available type)
  • firstName -- the name of the ivar (coder's choice except it must follow naming rules)
public class Person {


	
1
private
2
String
3
firstName; }


Encapsulation


Private and Encapsulation
It might seem funny that we hide an ivar.

But this is actually the essence of object oriented programming and is called encapsulation.

We don't want other code to directly manipulate the object's data. We will soon learn that we usually to provide some (controlled) access to the data through methods.
public class Person {


	private String firstName;


}
Public and Encapsulation
Here we have a small set of public methods for class Circle.

We'll learn that this is all we need to use type Circle.

Circle itself might contain complex code for drawing and etc., but we do not have to take the time to learn its inner workings. We trust that it is tested and it works and we simply just use it.
public Frame getBoundary()
public Point getAnchor()
public int getLongDimension()
public void drawOn(Graphics g, Color color)
public void moveBy(Point v)
public void setAnchor(Point anchorPoint)
public void setLongDimension(int longDimension)