Shared Flashcard Set

Details

Quiz 3
Some Java concepts
41
Computer Science
Graduate
11/12/2024

Additional Computer Science Flashcards

 


 

Cards

Term
inheritance
Definition
The is-a relationship between a more general superclass and a more specialized subclass.
Term
superclass
Definition
A general class from which a more specialized class (a subclass) inherits
Term
subclass
Definition
A class that inherits variables and methods from a superclass but adds instance variables, adds methods, or redefines methods
Term
substitution principle
Definition
The principle that a subclass object can be used in place of any superclass object.
Term
overriding
Definition
redefining a method in a subclass
Term
overloading
Definition
Giving more than one meaning to a method name
Term
dynamic method lookup
Definition
Selecting a method to be invoked at run time. In Java, dynamic method lookup considers the class of the implicit parameter object to select the appropriate method.
Term
polymorphism
Definition
Selecting a method among several methods that have the same name on the basis of the actual types of the implicit parameters
Term
hash code
Definition
A value that is computed by a hash function
Term
interface
Definition
A type with no instance variables, only abstract methods and constants
Term
implementing an interface
Definition
Implementing a class that defines all methods specified in the interface
Term
inner class
Definition
A class that is defined inside another class
Term
Differences between an interface and an abstract class
Definition
1) Interface has no data except constants, while an abstract class can have data members

2) An interface has no concrete methods, while an abstract class can have concrete methods for code reuse
Term
Similarities between an interface and an abstract class
Definition
1) Define responsibilities for implementers or subclasses

2) Can't be instantiated
Term
Advantage of using an abstract class
Definition
Can provide common data members and methods that you can reuse
Term
Advantage of using an interface
Definition
A class can only extend one class, but can implement multiple interfaces
Term
How to reverse a linked list
Definition
Node current = head;

while (current != null) {
next = current.next;
current.next = previous;
previous = current;
current = next;
}
head = previous;
Term
What is a Comparator, how does it work, and what are its advantages over Comparable?
Definition
-- Comparator is an interface for comparisons between two Objects
-- public int compare (Object a, Object b)
-- class FooComparatorA implements Comparator {
int compare(f1, f2) {return f1.age - f2.age;}
}
-- More flexible to compare Objects in different ways (i.e., based on different data members)
-- Comparators are different classes. If your class doesn't implement Comparable, you can still use a class that implements Comparator.
Term
Why @Override?
Definition
-- It's not strictly necessary
-- At compile-time, it checks the function signatures with those of the parent class to verify that you are actually overriding something
-- If you're not, you get an error
-- If you don't add @Override, you could get some confusing behavior, like if you think the child is using its parent when it's really not
Term
What is composition?
Definition
The child class includes an instance of the parent as a data member
Term
Advantages of composition over inheritance
Definition
1) Can combine multiple objects within, not restricted to inheriting only one class, or otherwise using inheritance hierarchy (ancestors)

2) You are not as vulnerable to changes in the parent class. If you only use a few methods from the parent class, then it's easier for you to know which parts of your code you need to change if the parent class changes.
Term
The LinkedList class implements which interfaces?
Definition
List, Collection, and Iterable
Term
The ArrayList class implements which interfaces?
Definition
List, Collection, and Iterable
Term
The HashMap class implements which interface?
Definition
Map
Term
Example of an interface extending another interface
Definition
Collection extends Iterable
List extends Collection
Term
Comparable interface
Definition
public interface Comparable {
public int compareTo(T other);
}
Term
How to evaluate compareTo
Definition
If this > other, return positive
if this == other, return 0
if this < other, return negative
Term
Four things an interface can have
Definition
1) Abstract methods
2) Static methods
3) Default methods
4) Constant data members
Term
Constructor of a child class
Definition
public childClass (parent parameter, parent parameter, child parameter) {
super(parent parameter, parent parameter);
param = child parameter;
}
Term
Child class method
Definition
public return method () {
super.method();
new stuff;
}
Term
"this" for constructors
Definition
public Foo (int x) {
...
}

public Foo (int x, int y) {
this(x);
...
}
Term
upcasting
Definition
casting a subtype to a supertype
Animal anim = (Animal) dog

void train (Animal animal)
trainer.train(new Dog())
Term
Why use upcasting?
Definition
We want general code applied only to the superclass type
Term
downcasting
Definition
casting to a subtype
Animal doggy = new Dog()
Dog doggytoo = (Dog) doggy
Term
instanceof
Definition
Animal doggy = new Dog();
if (doggy instanceof Dog) {
Dog doggytoo = (Dog) doggy;
}
Term
Why is HashMap.get(key) O(1)?
Definition
You do a hash function to find the key's index, or bucket. Just do a few operations -- like summing the characters in a String and then doing mod number of buckets -- and then go straight to that bucket
Term
Time complexity of setting an element in a built-in array
Definition
O(1)
Term
amortized analysis
Definition
the average cost of an operation over a sequence of such operations
Term
Pros and cons of a linked list
Definition
Fast to insert, slow to get to an index
Term
recursion
Definition
When something is defined in terms of itself
Term
base case
Definition
The branch in which there is no recursive call
Supporting users have an ad free experience!