//Date1.java //simple class example. Note: Date1 is bad name, but we will create DateNs //with more features...and if in same folder can not have same name... //public class means is usable by any other code. public class Date1 { //"instance variables" / "fields": //each object has it own set of instance variables. //(variable of a class type is an "object"). //private access modifier means not directly accessible by client code. private int year; private int month; private int day; //"constructor". method with same name as class. //constructor has no return type and no return statement. //called automatically when creating/instantiating Date object. //public means callable by client code. //purpose is to correctly initialize object. public Date1(int newMonth, int newDay, int newYear) { month = newMonth; day = newDay; year = newYear; } //"getter"/"accessor" method to safely access instance variable public int getYear() { return year; } public int getMonth() { return month; } public int getDay() { return day; } //** "setter"/"mutator" method is one that changes the object. public void increment() { day++; //stub for the actual method to be written later... } //conventional to have a toString method that returns a string version //of the object for display purposes. Called automatically in contexts //where a String is expected, like println(""+data1Obj); public String toString() { return(month + "/" + day + "/" + year); } } //class members can be methods or data. //class members can be public or private. //Usually the data is private and the methods are public. //Public methods are the interface, the "messages" sent to the object. //Private data members are hidden, inaccessible to client code. //Each object has its own set of data members and conceptually its own //set of methods. // . operator to access public members. //The methods process the data. They do so correctly and securely. //Client code not trustworthy enough to access the data, nor should it be //burdened to have to do so. Also, implementation changes won't affect //client code. //Object knows how to process itself.