IT 206 Quiz 2 Study Guide & Final Review
What is an association relationship?
"Uses" relationship Represents a conceptual relationship, where there is NO owner Both classes can exist independently Classes can be accessed directly Student uses a library
What is a composition relationship?
"has a" "death" relationship Special form of association (strong-type) where two class are connected together conceptually with ownership Represents a "death" relationship, with one class owning another. If parent class dies so does the child. Classes are existent-dependent Child class can only be accessed via parent clas Employee has a job course has an instructor
What is an aggregation relationship?
"has a" relationship Special form of association (weak-type) Represents a conceptual relationship, where there is one owner Both classes can exist independently Child class can be accessed directly course has an instructor
What is a Class?
A blueprint or template that defines the attributes & behaviors that an object of that class can have. Attributes in a class can be of mix data types. (both primitive and reference)
What is a concrete class?
A class that can be instantiated
What is an abstract class?
A class that is used when it is desirable to force a class to be extended because it is too broad or "abstract" to be instantiated on its own. Cannot instantiate objects from an abstract class Classes should have at least one abstract method
What is an abstract method?
A method that must be overridden at some point Has no method body
What happens when you call and override a method by a subclass object?
A new method is invoked with the same method name
How to prevent from violation information hiding with arrays of instance variables?
All constructors that accept an array as a parameter for an array as an instance variable should use a copy of the array and not a reference value to the array All accessor methods that access an array as an instance variable should return a copy of the array and not a reference value to the array Similar to accessors, all mutator methods that accept an array as a parameter for an array as an instance variable should use a copy of the array to store values and not the original array
Method Overriding
Allows you to reuse methods in the subclass from the superclass. These methods can be enhanced (more specialized). They must have the same method declaration (same name & formal parameters) in the subclass that is in the superclass. Ex// toString()
What is an Object?
An object represents a specific instance of a class. Each object has the same attributes but with unique values. All objects contain the same behaviors
What is a stream?
An object that manages data flow between the program and I/O devieces or files
How do owner (parent) classes incorporate child classes?
As instance variables
How many constructors will be invoked when a subclass object is instantiated (created)?
At least two constructors are invoked, the subclass constructor that invokes the superclass constructor. A superclass constructor must have a default constructor if there are no specific constructors which in turn must be called when a sublcass object is created.
Where in the class is the space in memory allocated for the Array of Instance Variables?
At object creation time, so within the constructor
How will can you make sure a text file is saved?
By making sure you explicitly close the output stream If you don't explicitly close it, Java will close it for you when program ends Best Practice: Make sure you explicitly close the file
You ______ instantiate an object of an abstract class
Cannot
Instance variables in inheritance
Cannot be overridden, only non-private variables are inherited.
What is downcasting?
Casting an object of a subclass that has a superclass reference value to a subclass reference value
What is upcasting?
Casting an object of subclass that has subclass reference value to a superclass reference value such as for storing objects into an array Cast operator is NOT needed because of substitution principle (JAVA will do this for you)
FileNotFound is a _____ exception
Checked
What is an interface?
Collection of abstract methods and constants These abstract methods are common methods that may be implemented by several disparate classes. Interface is like a fake inheritance Provides for consistency of activity across disparate classes Behaviors are done differently in each class. Ex// getMeasure()
Text files are...
Commonly used to store data Both numbers and words can be stored as text
When down casting, a ______ results in cast attempts that are not possible - where objects are not related
Compiler Error
What are the steps of adding a value to an array as an instance variable?
Create a mutator method that accepts a value Check if there is room in the instance array Check if the parameter value is valid Set the parameter value to the array Increment the current size
What are the steps of accessing a specific element in an array as an instance variable?
Create an accessor method that accepts an index parameter and has return type Check to make sure that the index is valid. Return the element at the specified index
What are the steps when writing to a file?
Create an object of the PrintWriter class that takes in an object of the FileOutputStream class which takes in an object of the File class 1: Pass the name of the file in code 2. Ask the user to specify the file name 3. Allow the user to select a file PrintWriter may throw a checked FileNotFoundException In this case, FileNotFound does not mean the file was not found. It means the file could not be created. As a result, the method must "catch or declare" the exception
What are the 2 types of levels in the inheritance hierarchy?
Direct superclass/subclass: one level ex// car is a vehicle Indirect superclass/subclass: two or more levels ex// sports is a car, car is a vehicle
PrinterWriter
Does the writing
When using string literals to specify file paths, what should be used?
Double backslashes, since on backslash is an escape character
What is it called when the JVM determines which method to select?
Dynamic binding
What is single inheritance?
Each subclass has only one superclass Java supports only single inheritance
What are the advantages of OO?
Easier to reuse code/easier maintenance Easier to increase an application's functionality Information Hiding - protect the data Data Encapsulation (Requirement of OO)
How are interfaces different from class?
Every method in an interface is abstract Every method in an interface is public There are no instance variables There are no static methods in interfaces
What are checked exceptions?
Exceptions represent invalid conditions outside immediate control of program Exceptions of the Exceptions class are checked Methods are obliged to establish a policy for handling them (catch or declare) IOexception, FileNotFound
What are unchecked exceptions?
Exceptions that are defects in the program (programmer error in logic) RunTimeExceptions are unchecked Not obligated to establish a policy for handling them DivisionByZero, runtime errors
What is a common error when implementing an interface?
Forgetting to declare the overridden method public in the class that is doing the implementing.
How should exceptions be ordered?
From most specific to least specific NumberFormatException IllegalArgumentException Exception
JFileChooser
GUI presented to user to select a file
The inheritance hierarchy becomes more____as you go up the chart
Generalized
When to use an abstract class instead of an interface?
Generally if the classes have an inheritance relationship, use an abstract class.
Association multiplicity
Goes both ways
Substitution Principle
In inheritance, a sublcass object reference value may be used in any location that expects a superclass object reference value
What are the 4 types of relationships?
Inheritance Association Aggregation Composition
Data flow IN
Input stream From JOP.showInputDialog and/or taken from file
What is the issue with information hiding and Array of Instance Variables?
Instance variable are meant to be private should be accessed and manipulated from the data definition class. Instance variables also store the reference value that points to the contents of an array. Within that array are the set of instance variables. The issues lies when it comes time to sharing the contents. Since it's an array, the contents aren't passed, only the reference value. This creates a backdoor for an external class (implementation) to be able to read/update the contents of the array directly instead of using methods in the data definition class.
How is an Array of Instance Variables created?
Instantiated using the "new" keyword
What is Inheritance?
It is conceptualized as a hierarchy where a subclass inherits properties (attributes) and behaviors from a superclass "is-a" relationship Provides easier maintenance and less duplication than maintaining individual classes Some type of Generalization/specialization hierarchy
What is instanceof?
It is used to perform safe downcasting to prevent syntax errors such as ClassCastException if (v instanceof Car) { Car c = (Car)v; c.getIsSport(); } OR if (v instanceof Car) { ((Car)v).getIsSport(); }
What is said about when a class implements an interface?
It must override all the methods of that interface since all interface methods are abstract
What is dynamic binding?
Just means that calls to overridden methods are determined at execution time (based on object type not reference) need superlass reference and overridden methods
Instances of Association are called
Links
Java is a strongly typed language
Meaning variables must be declared with data types
______ is when two methods share the same name but different parameters
Method overloading
______ is when two methods share the same name and parameters, but have different implementation
Method overriding
What are the disadvantages of OO?
More time consuming than procedural
How many catch blocks can be used for one try?
Multiple
What is OO programming?
Object-Oriented programming is modeled on the concept of real world "things" (objects) and their interactions (relationships) with other "things". Objects are based off real world "things", with attributes and behaviors OO still relates to the procedural approach like breaking tasks into sub tasks and reusing methods to handle those tasks.
When to use inheritance?
Only when the classes differ in both multiple attributes and behaviors
Data flow OUT
Output String From JOP.showMessageDialog and/or provided from a file
We are able to have an superclass object reference assigned to a sublcass object that is derived from that superclass because of
Polymorphism
What is polymorphism?
Polymorphism refers to the fact that you can have different subclass with the reference value of a single superclass and be able to select proper methods (that are in both the super and subclass) based on the subclass object type
3 types of methods that cannot be overridden
Private methods - meant to be private and not inherited (password validation) Static methods - apply for all instances Final methods - cannot be changed
What are exceptions?
Rare events outside normal behavior of code
Where in the class is the Array of Instance Variables declared?
Reference value for the array is declared at the top of the class
Overridden methods in a subclass cannot be more ______ than the method in the super class
Restrictive
FileOutputStream
Sends the data
What is the power of inheritance?
Software reusability New classes created (extended) from existing class use existing class' properties and behaviors (no need to code again) Subclasses: Are enhanced with additional capabilities (more specialized) Inherited behaviors can be customized New properties and behaviors can be added
The inheritance hierarchy becomes more______as you go down the chart
Specialized
What are not inherited?
Static methods & Constructors Constructors are not members of a class
Give an example of a "is a" relationship
Student is a Person
Most "portable" type of data file
Text file
Text files vs Binary files
Text files are Sequence of characters when view by text editor or read by program Designed to be read by humans Binary files' contents are handled as sequences of binary digits Designed to be read only by programs
What must be abstract in order for a method to be abstract?
The class must be abstract as well
What is the equals method?
The equals method is a method from the object class that is used to compare two objects to each other. public boolean equals(Object o) { if (o == null || this.getClass() != o.getClass()) { return false; } //downcast Student s = (Student)o; return this.getName().equals(s.getName()) && this.getGPA() == s.getGPA();
What is the Object Class?
The highest class in the OO hierarchy. All classes extend the Object class. Common overridden methods include toString() and equals(object o)
How are copy constructors used?
They are used when accessing or mutating the object instance variable within the parent class. Like arrays, the object reference value is stored into the instance variable, so to avoid violating information hiding , a reference value of a copied instance object is used. Accessor - return a copy of the object Mutator - set a copy of the object
What does throw early/catch late mean
Throw early means that a method detects a problem it can't solve, it is better to throw (address/state/inform) an exception rather than to come up with a work aorund Catch late means that a method should only catch an exception if it can remedy the error, if it cannot, it should propagate (send) it to the calling method until it has reached a method suitable to address the error.
Why would you need to downcast?
To call methods that only exist in the subclass
All methods in interfaces are public and abstract
True
An abstract class must be extended in order to be instantiated
True
If a class has an abstract method, then the class is also abstract
True
The "public abstract" keywords do not need to be provided in interfaces
True
How can you append to a file?
Use println
How do you use an interface with a class?
Use the "implements" keyword
How to call an overridden method from the super class?
Use the "super" keyword
What is a copy constructor?
Used to create a copy of an object and its attributes
Scanner Cass
Used to read text from a file
How to access instance variables?
Via accessors and mutators
What is static binding?
When the compile knows which method to use
When to use an interface instead of an abstract class?
When you need to force the creation of similar behaviors across disparate classes
When importing classes to read/write a file you can import all the classes or just the following specific ones
Writing import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import javax.swing.JFileChooser; Read import java.util.Scanner; import javax.swing.JFileChooser; A shortcut can be used to reference all files in the package, such as: import java.io.*;
Can a class implement more than one interface?
Yes
Are interfaces abstract?
Yes (since it has abstract methods, the class must be abstract as well)
What will happen if the file name doesnt exist?
a new empty file with a name will be created
How do you create a custom checked exception?
create a class that extends the Exception class
How do you create a custom unchecked exception?
create a class that extends the RuntimeException class
How are constants written in interfaces?
double speed = 45.5; String message = "Hello"; int distance = 10;
What are the methods to write to a file?
print - Prints out a message println -Prints out a message followed by a new line printf -Prints out a formatted message. Some examples: %d - Print the next item as an integer (int) %f - Print the next item as a real number (double)
What will happen if the file name already exists?
the old contents of the file will be lost (overwritten)