cs1400_05
What is this
"this" is a special variable in Java, which does not have to be declared. Java makes it available automatically in instance methods. It can be used in instance methods, and it holds a reference to the object that contains the method (or, in terms of messages, the object that received the message that is being processed). It provides a way to refer to "this object." If x is an instance variable, it can also be referred to as this.x within the same class. If doSomething() is an instance method, it can also be called as this.doSomething() within the same class.
What is an abstract class, and how can you recognize an abstract class in Java.
An abstract class is one that cannot be used to create objects. It exists only as a basis for making subclasses, and it expresses all the properties and behaviors that those subclasses have in common. In Java, a class can be marked with the modifier abstract to make it abstract. For example, abstract public class Vehicle { ... It will then be a syntax error to try to call a "new Vehicle" constructor. (Note: Only a class that has been marked as abstract can contain abstract instance methods.)
What is meant by the terms instance variable and instance method
Instance variables and instance methods are non-static variables and methods in a class. This means that they do not belong to the class itself. Instead, they specify what variables and methods are in an object that belongs to that class. That is, the class contains the source code that defines instance variables and instance methods, but actual instance variables and instance methods are contained in objects. (Such objects are called "instances" of the class.) Thus, instance variables and instance methods are the data and the behaviors of objects.
What are instance variables and instance methods
Instance variables and instance methods are what we have been calling "non-static variables" and "non-static subroutines." That is, they are variables and subroutines that are declared in a class but are not marked as "static." When a class is loaded into the computer, instance variables and methods do not become part of the class in memory; instead, they become part of each object that is constructed from that class, and each object gets its own copy of the instance variables and methods.
Explain the term polymorphism.
Polymorphism refers to the fact that different objects can respond to the same method in different ways, depending on the actual type of the object. This can occur because a method can be overridden in a subclass. In that case, objects belonging to the subclass will respond to the method differently from objects belonging to the superclass. (Note: If B is a subclass of A, then a variable of type A can refer to either an object of type A or an object of type B. Let's say that var is such a variable and that action() is a method in class A that is redefined in class B. Consider the statement "var.action()". Does this execute the method from class A or the method from class B The answer is that there is no way to tell! The answer depends on what type of object var refers to, a class A object or a class B object. The method executed by var.action() depends on the actual type of the object that var refers to, not on the type of the variable var. This is the real meaning of polymorphism.)
Java uses "garbage collection" for memory management. Explain what is meant here by garbage collection. What is the alternative to garbage collection
The purpose of garbage collection is to identify objects that can no longer be used, and to dispose of such objects and reclaim the memory space that they occupy. If garbage collection is not used, then the programmer must be responsible for keeping track of which objects are still in use and disposing of objects when they are no longer needed. If the programmer makes a mistake, then there is a "memory leak," which might gradually fill up memory with useless objects until the program crashes for lack of memory.
Suppose that Kumquat is the name of a class and that fruit is a variable of type Kumquat. What is the meaning of the statement "fruit = new Kumquat();"? That is, what does the computer do when it executes this statement? (Try to give a complete answer. The computer does several things.)
This statement creates a new object belonging to the class Kumquat, and it stores a reference to that object in the variable fruit. More specifically, when the computer executes this statement, it allocates memory to hold a new object of type Kumquat. It calls a constructor, which can initialize the instance variables of the object as well as perform other tasks. A reference to the new object is returned as the value of the expression "new Kumquat()". Finally, the assignment statement stores the reference in the variable, fruit. So, fruit can now be used to access the new object.
Modify the following class so that the two instance variables are private and there is a getter method and a setter method for each instance variable:
To make a variable private, just add the word "private" in front of each declaration. We need two methods for each variable. One of them returns the value of the variable. The other provides a new value for the variable. The names for these methods should follow the usual naming convention for getter and setter methods. (Note that my setter methods use the special variable this so that I can use the same name for the parameter of the method as is used for the instance variable. This is a very common pattern.)
Explain carefully what null means in Java, and why this special value is necessary.
When a variable is of object type (that is, declared with a class or interface as its type rather than one of Java's primitive types), the value stored in the variable is not an object. Objects exist in a part of memory called the heap, and the variable holds a pointer or reference to the object. Null is a special value that can be stored in a variable to indicate that it does not actually point to any object.
Object-oriented programming uses classes and objects. What are classes and what are objects? What is the relationship between classes and objects?
When used in object-oriented programming, a class is a factory for creating objects. (We are talking here about the non-static part of the class.) An object is a collection of data and behaviors that represent some entity (real or abstract). A class defines the structure and behaviors of all entities of a given type. An object is one particular "instance" of that type of entity. For example, if Dog is a class, than Lassie would be an object of type Dog.
What is a constructor? What is the purpose of a constructor in a class?
A constructor is a special kind of subroutine in a class. It has the same name as the name of the class, and it has no return type, not even void. A constructor is called with the new operator in order to create a new object. Its main purpose is to initialize the newly created object, but in fact, it can do anything that the programmer wants it to do.
Explain why the class Player that is defined in the previous question has an instance method named toString(), even though no definition of this method appears in the definition of the class.
If a class is not declared to extend any class, then it automatically extends the class Object, which is one of the built-in classes of Java. So in this case, Player is a direct subclass of Object. The Object class defines a toString() method, and the Player class inherits this toString() method from Object. The methods and member variables in a class include not just those defined in the class but also those inherited from its superclass.
Explain what is meant by the terms subclass and superclass.
In object oriented programming, one class can inherit all the properties and behaviors from another class. It can then add to and modify what it inherits. The class that inherits is called a subclass, and the class that it inherits from is said to be its superclass. In Java, the fact that ClassA is a subclass of ClassB is indicated in the definition of ClassA as follows: class ClassA extends ClassB {...}