OOP Final

0.0(0)
studied byStudied by 1 person
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/42

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

43 Terms

1
New cards

Static variables

belongs to the class itself. They CANNOT use instance variables because they run without an instance

2
New cards

Instance variables

belongs to each object. They CAN use static variables because static data always exists.

3
New cards

Enumerated types (Java)

Full classes; they can have fields, constructors, methods, etc.

4
New cards

Enumerated types (C++ and others)

Just named integer constants. Are a type, not a class.

5
New cards

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

6
New cards

How are 2D arrays implemented?

They are made of another 1D array.

7
New cards

Inheritance

"is a" relationship. Allows classes to inherit properties or behaviors from other classes.

8
New cards

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

9
New cards

Properties of Java inheritance

Java forbids multiple inheritance of classes, but simulates it with interfaces.

10
New cards

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

11
New cards

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

12
New cards

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.

13
New cards

What are the two ways you can use Polymorphism in Java?

Inheritance & interfaces

14
New cards

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.

15
New cards

How do you deal with exceptions?

Try/ Catch block

Throw

If unhandled, the exception becomes an error that triggers a stack trace.

16
New cards

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

17
New cards

What are the two types of unchecked exceptions?

Runtime exceptions (e.g., null pointer, out of bounds)

Errors (system-level failures)

18
New cards

Recursion

A programming technique where a method calls itself to solve a problem.

19
New cards

Basic properties of recursion?

An elegant way to write code, but inefficient

Necessary for certain problems (Tower of Hanoi)

20
New cards

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

}

21
New cards

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;

}

22
New cards

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

23
New cards

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)

24
New cards

How could you set up class Student's default constructor?

public Student() {

this.name = " ";

this.age = 0;

}

25
New cards

How could you set up a class Student's parameterized constructor?

public Student(String name, int age) {

this.name = name;

this.age = age;

}

26
New cards

How could you set up a class Student's getters?

public String getName() {

return name;

}

public int getAge() {

return age;

}

27
New cards

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;

}

28
New cards

How could you set up a class Student's toString method?

@Override

public String toString() {

return name + " (" + age + ")";

}

Output: name (age)

29
New cards

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

30
New cards

Be able to create an array using the "new" keyword

Student[ ] roster = new Student[20]

31
New cards

Be able to implement a child class of Student using the "extends" keyword

Used to demonstrate overriding and knowledge on it's properties

32
New cards

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

33
New cards

How could you make the default constructor for the child class?

public CollegeStudent() {

super();

this.major = " " ;

}

34
New cards

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()

35
New cards

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;

}

36
New cards

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; }

37
New cards

How could you use method overriding in the child class?

@Override

public string toString() {

return super.toString() + " major= " + major + " ";

}

Output: name (age) major= major

38
New cards

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

39
New cards

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.

40
New cards

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!

41
New cards

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.

42
New cards

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)

43
New cards

How do you access child specific methods?

Casting.

Student s = new CollegeStudent("Lia", 20, "History");

((CollegeStudent) s).getmajor(); ← LEGAL!!!