1/42
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
Static variables
belongs to the class itself. They CANNOT use instance variables because they run without an instance
Instance variables
belongs to each object. They CAN use static variables because static data always exists.
Enumerated types (Java)
Full classes; they can have fields, constructors, methods, etc.
Enumerated types (C++ and others)
Just named integer constants. Are a type, not a class.
Properties of Java arrays
automatically check bounds
They are immutable in length, not in contents ("resizing" an array is actually creating a new one and deleting the old one)
Dynamically allocated in the shared memory and have properties and behaviors like other objects (allocated on the heap), unlike other languages
How are 2D arrays implemented?
They are made of another 1D array.
Inheritance
"is a" relationship. Allows classes to inherit properties or behaviors from other classes.
Method overriding
Allows a subclass (child class from inheritance), to provide specific implementations to a method already defined in the superclass (class that child inherited from)
When using method overriding, you should annotate it with @Override
Properties of Java inheritance
Java forbids multiple inheritance of classes, but simulates it with interfaces.
What is an abstract class?
An abstract class is a class that cannot be instantiated on it's own, and is used as a blueprint for other subclasses to inherit from
Properties of abstract classes
cannot directly create an object using the "new" keyword
used to extend classes using the "extends" keyword
Can have zero or more abstract methods
Polymorphism
A reference of a parent type can refer to an object of a child type, and the method that is executed is determined at runtime.
What are the two ways you can use Polymorphism in Java?
Inheritance & interfaces
How does the compiler take an object at "face value" in polymorphism?
A reference typed as the parent can only call parent methods, unless you cast it.
How do you deal with exceptions?
Try/ Catch block
Throw
If unhandled, the exception becomes an error that triggers a stack trace.
What are two types of exceptions and what do they mean?
Checked - must be checked. Handled or declared using throws.
Unchecked - has the option to be checked or unchecked
What are the two types of unchecked exceptions?
Runtime exceptions (e.g., null pointer, out of bounds)
Errors (system-level failures)
Recursion
A programming technique where a method calls itself to solve a problem.
Basic properties of recursion?
An elegant way to write code, but inefficient
Necessary for certain problems (Tower of Hanoi)
Know how to make a recursive factorial function
int factorial(int n) {
if (n == 0) {
return 1; // base case
}
return n * factorial(n - 1); // recursive step
}
Know how to make an iterative Fibonacci sequence
int fib (int n) {
int a = 0;
int b=1;
for (int i = 0; i < n; i++) {
int temp = a + b;
a = b;
b = temp;
}
return a;
}
Be able to create a class "Student" that include instance variables, constructors (remember default), getters/setters, toString method, and perhaps be able to ovveride a method in a subclass
Next flashcards
How could you set initially set up class Student and it's instance variables?
public class Student {
private String name;
private int age;
} ← second bracket will be at the end of the class declaration (after all methods, constructors, etc. get initialized)
How could you set up class Student's default constructor?
public Student() {
this.name = " ";
this.age = 0;
}
How could you set up a class Student's parameterized constructor?
public Student(String name, int age) {
this.name = name;
this.age = age;
}
How could you set up a class Student's getters?
public String getName() {
return name;
}
public int getAge() {
return age;
}
How could you set up a class Student's setters?
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
How could you set up a class Student's toString method?
@Override
public String toString() {
return name + " (" + age + ")";
}
Output: name (age)
Why do you need to use @Override when doing the toString method?
Because every object comes with a toString method. If you don't override it, the toString method used will print the class name + memory hash
Be able to create an array using the "new" keyword
Student[ ] roster = new Student[20]
Be able to implement a child class of Student using the "extends" keyword
Used to demonstrate overriding and knowledge on it's properties
How would you initialize the child class and it's variable?
public class CollegeStudent extends Student{
private String major;
} ← again, this bracket shows up at the end of the class initialization
How could you make the default constructor for the child class?
public CollegeStudent() {
super();
this.major = " " ;
}
What is the "super" keyword?
super is used to reference the parent class. Example would be super(), this calls the Student class's default constructor Student()
How could you make the parameterized constructor for the child class?
public CollegeStudent(String name, int age, String major) {
super(name, age); ← calls Student(name,age)
this.major = major;
}
How could you make a getter and setter function for the child class?
public String getMajor() { return major; }
public void setMajor(String major) { this.major = major; }
How could you use method overriding in the child class?
@Override
public string toString() {
return super.toString() + " major= " + major + " ";
}
Output: name (age) major= major
What is an example of how polymorphism can apply to our Student and CollegeStudent classes? Why is it legal?
Student s = new CollegeStudent();
It's legal because of how polymorphism works on a "is-a" relationship. A CollegeStudent IS a Student
In a broader example, how could this be useful?
Once the reference is typed as the parent, you can store different child objects in the same variable type or the same array/list.
Student a = new student("Amy", 19);
Student b = new CollegeStudent("Mark", 21, "CS");
Student c = new HighSchoolStudent("Jen", 17);
all three variables have the type Student, but they can point to child objects of different classes.
What types of methods could you call in the example?
Only methods that the reference type knows about.
Student x = new CollegeStudent("Ken", 20, "Math");
x.getMajor(); ← ILLEGAL!
Why doesn't this work?
Even though the object is a CollegeStudent, the reference is a Student, and Student does NOT have a getMajor() method.
Java always checks the reference type first for method availability.
on runtime behavior, if a child class called the overriden toString method, will the reference type toString be executed, or the child class's?
The child class's (CollegeStudents in our example)
How do you access child specific methods?
Casting.
Student s = new CollegeStudent("Lia", 20, "History");
((CollegeStudent) s).getmajor(); ← LEGAL!!!