True or False: If your class doesn’t directly extend something, it will implicitly extend Object?
True
64
New cards
What is always at the root of an inheritance tree?
Object
65
New cards
How do you diagram an interface in a UML diagram?
A dashed line with italics
66
New cards
Explain these 3 statements:
1) The type of variable
2) The class
3) The type of object
type of variable is exactly what it is (1)
the class of a variable is exactly what it is (1)/it is an instance of exactly 1 class
the type of object is where “is a” comes into play (an object may satisfy many types)
67
New cards
What is overload?
Two different methods have the same name with different parameters and are in no way related
68
New cards
What is override?
A subclass “replaces” inherited methods. They have the same parameter types. The return types do not have to be the same but it must satisfy the type (is a)
69
New cards
True or False: you can change the privacy type in an override as long as it is more public and not less public?
True
70
New cards
What is early(static) binding?
It will run whatever type it is declared as. The compiler makes the decision immediately.
ex. Person p = new Student s;
will run person version
71
New cards
What is late (dynamic) binding?
It will run the version it literally is, decision is made at runtime.
ex. Person p = new Student s;
will run student version
72
New cards
True or False: You can override more than once?
False
73
New cards
When will a cast run successfully?
When you cast either up or down the tree (must be reached by moving exclusively up or down)
74
New cards
Which type of casting will always run? Why?
An upcast will always run because the lower levels “is a” higher level
75
New cards
What cast do you have to be careful with? Why?
Downcasting because it is not necessarily that type
76
New cards
When can you implicitly cast? When can you not?
You can always implicitly upcast, you can never implicitly downcast
77
New cards
What type of error would you get if you downcast incorrectly?
ClassCastException
78
New cards
When will a cast compile?
As long as it is above or below the type on the tree
79
New cards
What is the safe practice in downcasting so that it doesn’t throw exceptions?
if (p instanceof Student){
Student s = (Student) p;\`
}
80
New cards
Is multiple inheritance allowed in Java?
No
81
New cards
What is inheritance, what is composition?
Because you can only have 1 superclass, it is important to make sure that you really want make sure that is it is an absolutely necessary thing while in composition you can place an object of what you need inside the class and therefore you can still access all the functionalities
82
New cards
True or false: composition allows for polymorphism?
False
83
New cards
If given the choice, what do you usually favor, composition or inheritance?
Composition
84
New cards
What does it mean to have a final primitive variable?
It cannot change, the value must remain the same
85
New cards
What does it mean to have a final object?
The object itself can change (if it is mutable) it just cannot be assigned to a new object
86
New cards
What does having a final class mean?
No one can extend the class
87
New cards
What doe having a final method mean?
You cannot override the method
88
New cards
What is protected visibility restricted to?
The class, package, and its subclasses
89
New cards
What is shadowing?
Overloading a variable that is inherited
90
New cards
What is the purpose of an abstract class?
To be extended in various ways (like an interface)
91
New cards
True or false: Abstract classes can be instantiated?
False
92
New cards
True or false: Abstract classes can have state, instance variables, and constructors?
True
93
New cards
What is better about Abstract classes?
You can include instance variables and constructors
94
New cards
What is better about interfaces?
A class can implement more than 1 interface
95
New cards
When given the option, do you favor abstract classes or interfaces?
interfaces
96
New cards
How do you write an abstract class?
public abstract class Shape{
private int xCoord, yCoord;
private Color color;
private int width;
public Shape(int x, int y, Color color, int width){
xCoord=x;
yCoord=y;
this.color=color;
this.width=width;
}
}
97
New cards
How do you write an abstract method in an abstract class?
public abstract void drawShape();
98
New cards
How do you write a try catch block?
try {
//code
} catch (NullPointerException e) {
//handler
} catch (RuntimeException (e) {
//handler
} finally {
…
}
99
New cards
What is the difference between checked and unchecked exceptions?
Unchecked exceptions means that there is something wrong with your code, fix it. Checked exceptions means that at runtime something goes wrong, it is beyond your control.
100
New cards
For what type of exceptions do you use a try catch?