200 likes | 418 Views
Computer Science 111 Fundamentals of Computer Programming I. Working with our own classes. So much for learning programming?. Review of object concepts. So far we have defined a class for each program. In main we get an instance of the class and work with that single object.
E N D
Computer Science 111Fundamentals of Computer Programming I Working with our own classes
Review of object concepts • So far we have defined a class for each program. In main we get an instance of the class and work with that single object. • We have also made use of classes • From Java • From TerminalIO • From BreezySwing • In the spirit of decomposition and reusability, we now focus on defining our own classes and using multiple objects in a program.
Review of object concepts • When we define a class, we are making a template for the objects from the class – what kind of data and behavior will the objects have. The data is stored in instance variables and the behavior is given by the instance methods • An object is an instance of a class. It has • State – the current values of its instance variables • Behavior – defined by its methods
Review of object concepts • When we declare a variable with type specified by a class (as opposed to a primary type like int), the variable is actually a reference to an object of the given type. There is no memory allocated for the object until the object is instantiated. • We can have the variable reference an actual object by • getting a new object or • have it reference an existing object
Object Concepts MyClass myObj; myObj myObj = new MyClass(); myObj
MyClass myObj1 = new MyClass(); myObj1 MyClass myObj2 = new MyClass(); myObj2 myObj1 = myObj2; Object Concepts Note that there is no reference to the memory allocated for the first object. This will be reclaimed by Java’s garbage collector.
Mutators and Accessors • These are two general categories of methods that we provide. • Mutators are methods that change the object’s state; i.e., cause changes to the instance variables. An example would be the setNumber() methods to change state of GUI fields. Another example would be the updateModelFromRoll() method. • Accessors are methods that return information about the state. An example would be the getNumber() methods. • We provide public mutators and accessors only for the variables for which we want to allow change and access capabilities.
Class template public class <class name> extends <some class>{ // Declaration of instance variables private <type> <name>; … // Code for constructor methods public <class name>( ) { // Initialize instance variables … } // Code for the other methods public <return type><method name>(<params>){ … } … }
Class template by pieces public class <class name> extends <some class>{ // // Classes usually made public so usable by all clients // The class can act as server providing service to clients // // The class name is legal Java identifier. Convention is to // begin with upper case letter. // // The classes in Java form a hierarchy in a tree structure with // the class Object at the very top. Each class we write is a // subclass of some other class. If we do not specify // otherwise, it is made a direct subclass of Object. // // We specify the class we are subclassing by saying our class // extends the other class. The class we extend is called // the parent of our class, and our class is its child. The // child inherits the characteristics of the parent.
Class template by pieces // Declaration of instance variables private <type> <name>; … // We almost always make the instance variables private. // This keeps the users of our objects from directly // accessing these variables. We provide mutators // and accessors to do exactly what we allow and // nothing more. In fact, the instance variables and // their types need not be known. This allows for // implementation changes that do not effect client code. // This is referred to as information hiding.
Class template by pieces // Declaration of instance variables private <type> <name>; … // As an example, suppose we are writing a class for // some kind of temperature class. We might provide // methods for giving the temperature in Fahrenheit or // in Celsius, etc. There would be no need to store // the value in both systems. If we provided access to // an instance variable called fahrenheit, then all code // using this access would be dependent on our keeping // that implementation of our class. If, instead, we // provided access methods called getFahrenheit and // and getCelsius, we could change the implementation // without problems with the client code.
Class template by pieces // Code for constructor methods public <class name>( ) { // Initialize instance variables … } // Note that a constructor has the same name as the class. // Note also that a constructor does not have a return type. // A constructor is always made public. If not specified, this // is the default. // A constructor is executed when a new object is instantiated. // You can have constructors with parameters. // You can have more than one constructor – overloaded
Class template by pieces // Code for the other methods public <return type><method name>(<params>){ … } // Most of our methods will probably be public so they can be // used by various clients. // However, we still should make methods private if they // are “helper” methods for internal use by the objects of // the class we are defining.
Class template by pieces public class Temperature extends Object { private double fahrenheit; … public Temperature( ){ fahrenheit = 0.0; } public Temperature(double aTemp){ fahrenheit = aTemp; } … } … Temperature temperature1 = new Temperature( ); Temperature temperature2 = new Temperature(100.0);
toString methods • We usually provide a method named toString that returns some string representation of the object. • This method has no parameters. • It can be called explicitly like other methods. writer.println(MyObj.toString()); • However it is also called explicitly when we use string concatenation or use println with the object name itself. • str = str + “Today” + myObj.toString();is equivalent to:str = str + “Today” + myObj;