Exam 2 Review Flashcards

1
Q

What is a class and how is it different from an object?

A

A class is like a blueprint for creating objects where as an object is an actual instance create from a class.

Example:
class Car { // Class (blueprint)
String model;
int year;
}

public class Main {
public static void main(String[] args) {
Car myCar = new Car(); // Object (instance of Car)
myCar.model = “Toyota”;
myCar.year = 2022;
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Describe the difference between instance methods and static methods

A

Instance methods belong to an object (require an object to be called) where as static methods belong to the class (can be called without creating an object)

Example:
class Example {
public void instanceMethod() { System.out.println(“Instance Method”); }
public static void staticMethod() { System.out.println(“Static Method”); }
}

public class Main {
public static void main(String[] args) {
Example.staticMethod(); // ✅ No object needed

    Example obj = new Example();
    obj.instanceMethod(); // ✅ Requires object
} }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Describe the difference between instance fields and static fields

A

Instance fields each object gets its own copy where as static fields data is shared across all objects of the class

Example:
class Counter {
public int instanceCount = 0;
public static int staticCount = 0;
}

public class Main {
public static void main(String[] args) {
Counter c1 = new Counter();
Counter c2 = new Counter();

    c1.instanceCount++; // Only changes c1's field
    Counter.staticCount++; // Changes shared field
} }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Describe constructors

A

Constructors initialize new objects

Example:
class Employee {
String name;
Employee(String name) { this.name = name; }
}

class Manager extends Employee {
Manager(String name) { super(name); } // Calls Employee’s constructor
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does the new operator do?

A

Allocates memory and calls the constructor

Example:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does super() do?

A

When you create an object of a subclass, the constructor of the superclass (parent class) is automatically called before the subclass constructor. By default, the Java compiler inserts a call to the no-argument constructor of the superclass (super()). However, if you want to explicitly call a superclass constructor with arguments, you can use super() with the appropriate parameters.

Example:
class Animal {
// Superclass constructor
Animal(String name) {
System.out.println(“Animal name: “ + name);
}
}

class Dog extends Animal {
// Subclass constructor
Dog(String name) {
// Call to the superclass constructor
super(name); // This calls Animal(String name)
System.out.println(“Dog name: “ + name);
}
}

public class Test {
public static void main(String[] args) {
Dog dog = new Dog(“Buddy”);
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Syntax, Employee, Manager, and ManagerTest

A

class Employee {
String name;
double salary;

public Employee(String name, double salary) {
    this.name = name;
    this.salary = salary;
} }

class Manager extends Employee {
String department;

public Manager(String name, double salary, String department) {
    super(name, salary); // Calls Employee constructor
    this.department = department;
} }

public class ManagerTest {
public static void main(String[] args) {
Manager m = new Manager(“Alice”, 90000, “IT”);
System.out.println(m.name + “ manages “ + m.department);
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Label all four modifiers and what the scope of each one is

A

Modifier Scope
public: Accessible everywhere
protected: Accessible within the same package + subclasses
No modifier (default): Accessible within the same package
private: Accessible only within the class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Can a final class be subclassed?

A

NO, if a final class provides mutators, its state can change, but it cannot be extended

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How does a class hide data?

A

Encapsulation, using private fields and utilizing getters and setters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are static factory methods?

A

Provide controlled object creation

Example:
class User {
private String name;
private User(String name) { this.name = name; }

public static User createUser(String name) {
    return new User(name); // Controlled instantiation
} }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is method overloading?

A

Same method name, different parameters

Example:
class MathUtils {
public int add(int a, int b) { return a + b; }
public double add(double a, double b) { return a + b; }
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is call by value?

A

Java passes object references by value, meaning the reference is copied, not the object itself

Example:
class Example {
int value = 10;
void change(Example obj) { obj.value = 20; }
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are facts about packages in java?

A

A package groups related classes together.

Example:
java.lang, java.io

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Inheritance

A

Extend keywords allows for inheritance. Super() calls parent constructor

Example:
class Parent { }
class Child extends Parent { }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Declared Type vs Actual Type

A

Employee e = new Manager(); // Declared: Employee, Actual: Manager

17
Q

Method overriding and polymorphism

A

A subclass redefines a method from the parent class.

Example:
class Animal { void makeSound() { System.out.println(“Sound”); } }
class Dog extends Animal { void makeSound() { System.out.println(“Bark”); } }

18
Q

Type Safety in ArrayList is ensured when?

A

When generics are used

Example:
In this example the generic is <String></String>

ArrayList<String> list = new ArrayList<>();</String>

19
Q

Diamond Syntax is ArrayList

A

ArrayList<Integer> nums = new ArrayList<>();</Integer>

20
Q

Wrapper Class, need to fill this in based off slide

21
Q

Explicit Casting

A

Process of manually converting an object from one type to another, can be danger because it may lead to ClassCastException

Example:
Animal animal = new Dog(); // Animal is a parent class, Dog is a subclass
Dog dog = (Dog) animal; // Explicit casting

22
Q

Instanceof operator

A

The instanceof operator is used to check the type of an object before performing an explicit cast. This ensures type safety and prevents ClassCastException.

Example:
Animal animal = new Dog(); // Animal is a parent class, Dog is a subclass

if (animal instanceof Dog) { // Check if animal is an instance of Dog
Dog dog = (Dog) animal; // Safe cast
}

23
Q

Upcasting

A

Upcasting is the process of casting a child class object to a parent class reference. It’s safe and happens automatically (implicit casting) because every child is a type of the parent class. There’s no risk of losing information during this process. Checked at compile time

Example:
Dog dog = new Dog();
Animal animal = dog; // Upcasting, safe and automatic

24
Q

Downcasting

A

Downcasting is the process of casting a parent class object to a child class reference. This can be unsafe if the object is not actually an instance of the child class, leading to a ClassCastException. Checked at runtime

Example:
if (animal instanceof Dog) {
Dog dog = (Dog) animal; // Safe downcasting
}

25
Abstract Class
An abstract class is a class that cannot be instantiated directly. It’s meant to be extended by subclasses. Example: abstract class Animal { abstract void makeSound(); // Abstract method, no implementation public void sleep() { // Concrete method, can be used by subclasses System.out.println("Sleeping..."); } } class Dog extends Animal { @Override void makeSound() { System.out.println("Bark"); } }
26
All enumerated types are subclasses of the class Enum. T/F
TRUE