Exam 2 Review Flashcards
What is a class and how is it different from an object?
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;
}
}
Describe the difference between instance methods and static methods
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 } }
Describe the difference between instance fields and static fields
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 } }
Describe constructors
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
}
What does the new operator do?
Allocates memory and calls the constructor
Example:
What does super() do?
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”);
}
}
Syntax, Employee, Manager, and ManagerTest
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);
}
}
Label all four modifiers and what the scope of each one is
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
Can a final class be subclassed?
NO, if a final class provides mutators, its state can change, but it cannot be extended
How does a class hide data?
Encapsulation, using private fields and utilizing getters and setters
What are static factory methods?
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 } }
What is method overloading?
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; }
}
What is call by value?
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; }
}
What are facts about packages in java?
A package groups related classes together.
Example:
java.lang, java.io
Inheritance
Extend keywords allows for inheritance. Super() calls parent constructor
Example:
class Parent { }
class Child extends Parent { }
Declared Type vs Actual Type
Employee e = new Manager(); // Declared: Employee, Actual: Manager
Method overriding and polymorphism
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”); } }
Type Safety in ArrayList is ensured when?
When generics are used
Example:
In this example the generic is <String></String>
ArrayList<String> list = new ArrayList<>();</String>
Diamond Syntax is ArrayList
ArrayList<Integer> nums = new ArrayList<>();</Integer>
Wrapper Class, need to fill this in based off slide
Explicit Casting
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
Instanceof operator
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
}
Upcasting
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
Downcasting
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
}