Landing : Athabascau University

Reflections on the first few sections of unit 5

2023-12-0314:05:33

An Introduction to Object Oriented Programming Language.

Programming in the Large II: Objects and Classes.

The overview outlined below covers the following topics.

Objects, Instance Methods, and Instance Variables.

Lesson Objectives:

  • Understand the fundamentals of objects

  • Understand the concept of objects, classes and instances

  • Understand the essential elements of the class: get and set methods

  • Understand arrays of objects

Constructors and Object Initialization.

Lesson Objectives:

  • Understand about some essential features of classes, including initialization of instance variables and constructors.

  • Understand how memory is managed through “garbage collection”.

Some key points learnt so far:

  • OOP represents an attempt to make programs more closely model the way people think about and deal with the world.
  • At the heart of object-oriented programming, we have objects—entities that have. 
  • behaviours, that 
  • hold information, 
  • and that can interact with one another. 
  • An object should be thought as floating around independently in the computer's memory. In fact, there is a special portion of memory called the heap where objects live. 
  • Objects exist in the heap and can be accessed only through variables that hold references to the objects.
  • Instead of holding an object itself, a variable holds the information necessary to find the object in memory. This information is called a reference or pointer to the object. In effect, a reference to an object is the address of the memory location where the object is stored. When you use a variable of object type, the computer uses the reference in the variable to find the actual object.
  • From the point of view of programming, it is more exact to say that classes are used to create objects. A class is a kind of factory—or blueprint—for constructing objects. 
  • The non-static portions of classes describe objects.
  • The non-static parts of the class specify, or describe what variables and methods the objects will contain. 
  • This is part of the explanation of how objects differ from classes: Objects are created and destroyed as the program runs, and 
  • there can be many objects with the same structure, if they are created using the same class.

In the example below,

Student std = new Student("John Smith");
Student = object
std = variable
  • Non-static variables are usually not part of any one given class. 
  • There can be many objects created within a class, and each one will have its own variables. This is what it means for the non-static parts of the class to be a template for objects: Every object gets its own copy of the non-static part of the class. 
  • An object that is created using a class is said to be an instance of that class.  
  • Static methods are part of the class; 
  • non-static, or instance, methods become part of objects created from the class.
  • It is worth noting that in Java, no variable can ever hold an object. A variable can only hold a reference to an object.
  • In a program, objects are created using an operator called new, which creates an object and returns a reference to that object. (In fact, the new operator calls a special subroutine called a "constructor" in the class.) For example, assuming that std is a variable of type Student, declared as above, the assignment statement 

std = new Student();

  • In this case, "the variable - std refers to or points to the object, - Student,".

  • So, suppose that the variable std refers to an object that is an instance of class Student. That object contains instance variables name, test1, test2, and test3. These instance variables can be referred to as std.name, std.test1, std.test2, and std.test3. This follows the usual naming convention that when B is part of A, then the full name of B is A.B. For example, a program might include the lines
System.out.println("Hello, "  +  std.name  +  ".  Your test grades are:");
System.out.println(std.test1);
System.out.println(std.test2);
System.out.println(std.test3); 
  • When one object variable is assigned to another, only a reference is copied. The object referred to is not copied.  "The object is not in the variable. The variable just holds a pointer to the object."
  • It is worth noting that Strings are objects, for example the strings "Mary Jones" and "John Smith" as objects.  Strings are special objects, treated by Java in a special way. Since strings are objects, a variable of type String can only hold a reference to a string, not the string itself.  This explains why using the == operator to test strings for equality is not a good idea.
  • In the context of object-oriented programming, subroutines are often referred to as methods.
  • Java uses a procedure called garbage collection to reclaim memory occupied by objects that are no longer accessible to a program. 
  • In Java programming language, it is the responsibility of the system, not the programmer, to keep track of which objects are “garbage”.  This procedure ensures that errors such as the dangling pointer error (when the program tries to access an object that is no longer there.) & the memory error (where a programmer neglects to delete objects that are no longer in use.), that occurs with other programming languages that do not use the garbage collection procedure.

Getters and Setters

  • When writing new classes, it's a good idea to pay attention to the issue of access control. Making a member of a class public makes it accessible from anywhere, including from other classes. On the other hand, a private member can only be used in the class where it is defined.
  • Worth noting that accessor methods are more often referred to as getter methods. A getter method provides "read access" to a variable. (Sometimes for Boolean variables, "is" is used in place of "get". For example, a getter for a Boolean member variable named done might be called isDone().)

  • You might also want to allow "write access" to a private variable. 
  • That is, you might want to make it possible for other classes to specify a new value for the variable. This is done with a setter method. (If you don't like simple, Anglo-Saxon words, you can use the fancier term mutator method.) 
  • The name of a setter method should consist of 
  • "set" followed by 
  • a capitalized copy of the variable's name, and 
  • it should have a parameter with the same type as the variable
  • A setter method for the variable title could be written
public void setTitle( String newTitle ) {
   title = newTitle;
}
  • A getter and/or setter method defines a property of the class, that might or might not correspond to a variable. For example, if a class includes a public void instance method with signature setValue(double), then the class has a "property" named value of type double, and it has this property whether or not the class has a member variable named value.

Arrays and Objects

  • Arrays are objects and array variables hold pointers to arrays.

Constructors and Object Initialization.

  • OBJECT TYPES IN JAVA are very different from the primitive types. 
  • Simply declaring a variable whose type is given as a class does not automatically create an object of that class.  
  • Objects must be explicitly constructed
  • For the computer, the process of constructing an object means,  
  • first, finding some unused memory in the heap (the portion of the memory where and object lives) that can be used to hold the object and, 
  • second, filling in the object's instance variables
  • As a programmer, you don't care where in memory the object is stored, but you will usually want to exercise some control over what initial values are stored in a new object's instance variables. 
  • In many cases, you will also want to do more complicated initialization or bookkeeping every time an object is created.

Initializing Instance Variables

Constructors.

  • In Java, there is a special type of subroutine name a constructor.  

  • They are not instance methods since they do not belong to object.

Some characteristics of a constructor are as outlined below:

  • The definition of a constructor looks much like the definition of any other subroutine, with three exceptions. 

  • A constructor does not have any return type (not even void). 

  • The name of the constructor must be the same as the name of the class in which it is defined.

  • And the only modifiers that can be used on a constructor definition are the access modifiers public, private, and protected. (In particular, a constructor can't be declared static.)

  • However, a constructor does have a subroutine body of the usual form, a block of statements. 

  • There are no restrictions on what statements can be used. And a constructor can have a list of formal parameters.  
  • In fact, the ability to include parameters is one of the main reasons for using constructors.  
  • The parameters can provide data to be used in the construction of the object. 
  • It is important to note that:
  • A constructor call is more complicated than an ordinary subroutine or function call. It is helpful to understand the exact steps that the computer goes through to execute a constructor call:

  • First, the computer gets a block of unused memory in the heap, large enough to hold an object of the specified type.
  • It initializes the instance variables of the object. If the declaration of an instance variable specifies an initial value, then that value is computed and stored in the instance variable. Otherwise, the default initial value is used.
  • The actual parameters in the constructor, if any, are evaluated, and the values are assigned to the formal parameters of the constructor.
  • The statements in the body of the constructor, if any, are executed.
  • A reference to the object is returned as the value of the constructor call.
  • The end result of this is that you have a reference to a newly constructed object.