







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!








COP 3330 Exam 2 Review Chapter Sections Topics 7 7.1 – 7.8 Classes, static vs. instance, this, references vs. objects, inherited methods, overriding, scoping rules, method overloading, generic classes None None Aggregation 8 8.3 – 8.4, Iterator, Array Processing, 8.8 – 8.11 2-D Arrays, Vector, Arraylist Collection Algorithms 9 9.1 – 9.2 Inheritance, Polymorphism, Visibility Modifiers How to study:
Exam Format Here are the different types of questions I will have:
Constructors The job of a constructor is to initialize an object of the class. Unlike C where you have to specifically allocate space for the object with a malloc call, (or something similar), java takes care of finding the appropriate memory automatically. All you have to do is initialize the components of the object. All constructors are public and must be named the same thing as the class. A constructor in the Time class has the signature: public Time(int h, int m); One important thing to keep straight in a constructor is the order of the assignment statements. In particular, the instance variables need to be assigned, so these need to be on the left- hand side and the formal parameters need to be on the right. Constructors are often overloaded, meaning that there are multiple ones defined with different parameter lists. The one instantiated depends on the number and types of the actual parameters. A default constructor is one that takes in no parameters. Inheriting Methods All classes in java are part of an inheritance structure. In particular, all classes inherit from the class Object. From there, we can have a longer “chain” of inheritance. In any inheritance relationship, the class from which another class inherits is known as the base/parent class. The class that does the inheriting is known as the sub/child class. The child class retains all characteristics of the parent class, plus can have some unique characteristics of its own.
Thus, the Time class, which we defined in class, automatically inherits all of the characteristics of the Object class. For example, the Object class has a toString method. If there is no toString method declared in the Time class, then anytime the toString method is called on a Time object, it will actually run the toString method defined in the Object class. In essence, the Time class has inherited the toString method in this example. The other option would be to write a toString method in the Time class. Once we do this, if we call the toString method on a Time object, it will call the one defined in the Time class, overriding the toString method in the Object class. (The rule is that the method in the “nearest” class overrides all similar methods in previous parent classes.) It would be a good idea to take some of the examples shown in class and test out what occurs when a method is overridden and when it is not. toString The following method is defined in the Object class: public String toString(); It is frequently overridden in user-defined classes. The reason for this is as follows: Anytime someone tries to print out an Object, what java automatically does is call toString() on that Object and just prints out what that method returns. Thus, if t is a time object and you run System.out.println(t); // You are implicitly running System.out.println(t.toString());
For right now, we are only using two of them: Private: Accessible only inside of the class. Protected: Accessible inside the class, subclass and package. Public: Accessible anywhere. Usually, instance variables are private while most instance methods are public. The goal here is to hide the representation of the data from the user. The public methods are the only means by which the user can manipulate objects they create. We can restrict the types of operations the user can perform by restricting the public methods we give them access to. Aggregation When we design a set of classes for an application, one of the relationships between classes is aggregation. This relationship is when one class contains OR HAS-A object of another class. For example, a Lemonade Object is comprised of several components, one of which is a BankAccount object. In many instances it makes sense to create classes with an aggregation relationship. (If you created a Car object, it would ideally contain/have several wheel objects.) It's important to recognize and understand the difference between an aggregation relationship and an inheritance relationship. The best way to internalize these concepts is to look at examples with several classes and consider how you would extend them.
Iterator If you want to "go through" each element in a Java collection or array, you can do so as follows: for (Object o : collectionName) { // Use o in here } The first item inside of the loop is the type of item being stored in the collection. The second item is the variable name temporarily given for an arbitrary object in the collection. This is followed by a colon and then the name of the collection through which you want to iterate. Inside the loop, instead of having to index into an array, just use the given variable name in the body of the loop and those actions will be performed on each element in the collection as the loop executes. 2-Dimensional Arrays Technically, a 2 dimensional array is an array of arrays. It is possible to allocate space for the array references, without allocating space for each array. Furthermore, it is possible to allocate space for references to objects without allocating space for each object. Once the array is created, it is indexed just like a 2 dimensional array in C. Object[][] vals; // vals is a reference to a 2D array vals = new Object[10][]; // we've allocated vals to point to an //array that stores 10 array references. for (int i=0; i<vals.length; i++) vals[i] = new Object[i+1]; // allocated space for each array // individually; for (int i=0; i<vals.length; i++) for (int j=0; j<vals[i].length; j++) vals[i][j] = new Object(); // create each individual Object.
Basics of Inheritance An inherited class is defined as follows, using the key word extends: public class ThreeDimensionalPoint extends Point { … } In the example above, Point is known as the base/parent/super class, and ThreeDimensionalPoint is Point's subclass. (Alternatively, Point is known as the base class from which ThreeDimensionalPoint inherits. Also, there is an IS-A relationship here – a ThreeDimensionalPoint object IS A Point object as well.) A subclass inherits all the characteristics of the superclass. Thus, all public methods in the superclass are valid to be run on objects of the subclass. In the typical case, the subclass adds at least one instance variable, separate from those it inherited from the superclass. In the example in the text, ThreeDimensionalPoint has its own instance variable z, and inherits x and y from Point. In the typical case, a subclass just uses some of the methods in its superclass, but overrides other methods. To override a method, you must define a method with the same signature as one in the superclass in the subclass. The purpose of this is so that if you have a ThreeDimensionalPoint object, you end up running a different method on it than if you just had a Point object.
The method call super() When overriding a method in the superclass, you have the option of calling the corresponding method from the superclass. To do this, instead of using "this" just use the name "super". For all methods except for constructor, you would call, super.methodname(). Pass the parameters you need/want to pass to the corresponding method in the superclass. Constructors All constructors in the subclass will automatically call the default constructor in the base class if there is NO EXPLICIT call to the base class constructor. The other option is to make an explicit call to the superclass constructor using "super". In this case, you control which of the constructors in the superclass gets called. (Here, the method call is just super, not super.methodname()) The rationale behind this is that you MUST build the base object first before building any of the rest of it to create the subclass specialization. Reasons to use Inheritance, instead of just adding to one class Putting all the functionality into one class may make it difficult to understand, or more difficult to use for simple things. (For example, sometimes you want to use points in 2 dimensions, sometimes in three. By incorporating inheritance, you make the use of both seamless. If you didn't, when wanting a 2-D point, you'd have to always just set z=0 and deal with that extra component.)
referring to an object of the subclass, and that subclass is the parameter type, then this will NOT be considered a match. Visibility Modifier Rules Member Restriction this Subclass Package General public X X X X protected X X X default X X private X instanceof operator The instanceof operator is used in Boolean expressions as follows: reference instanceof Class This expression is true if the reference to the left of the operator is referring to an object that belongs in the class designated to the right of the operator. It doesn't have to be the exact class. Thus, if the variable pt is a Point reference, and it was referring to a ThreeDColoredPoint object, then the expressions pt instanceof Point pt instanceof ColoredPoint pt instanceof ThreeDColoredPoint would all evaluate to true. But if pt referred to a ColoredPoint object, then the first two would be true and the last one would be false.