CSC203 Midterm 1 Study Guide
1. Basic Classes and Objects
Class Structure
- A class is a blueprint for objects that contains:
- Instance variables (object state)
- Methods (object behavior)
- Constructors (object initialization)
- Private variables for encapsulation
Constructors
- Special methods called when creating new objects using
new - Must have the same name as the class
- Can be overloaded (multiple constructors with different parameters)
- Default constructor is provided only if no other constructors are defined
- Use
thiskeyword to refer to instance variables
Example:
public class Cat {
private String name;
private int age;
// Constructor
public Cat(String name, int age) {
this.name = name;
this.age = age;
}
// Default constructor
public Cat() {
this.name = "Unknown";
this.age = 0;
}
}
Access Modifiers
public: Accessible from any classprivate: Only accessible within the classprotected: Accessible within package and by subclasses- Default (no modifier): Only accessible within package
2. Methods
Instance vs Static Methods
- Instance methods:
- Can access instance variables
- Called on object instances
- Have implicit
thisparameter - Example:
cat.getName()
- Static methods:
- Cannot access instance variables directly
- Called on the class itself
- No
thisparameter - Example:
Math.abs()
Getters and Setters
- Used to control access to private instance variables
- Follow naming convention:
getName(),setName() - Encapsulation best practice
Example:
public class Cat {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
3. Object Class Methods
toString()
- Returns string representation of object
- Automatically called when object is printed or concatenated
- Best practice to override for custom classes
Example:
equals()
- Defines object equality
- Must override for custom object comparison
- Important for collections (HashSet, HashMap)
- Must be consistent with hashCode()
Example:
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Cat)) return false;
Cat cat = (Cat) o;
return age == cat.age &&
Objects.equals(name, cat.name);
}
hashCode()
- Returns integer hash value for object
- Must be consistent with equals()
- Used by hash-based collections
- Common implementation uses prime numbers
Example:
@Override
public int hashCode() {
int hash = 1;
hash = 31 * hash + (name == null ? 0 : name.hashCode());
hash = 31 * hash + age;
return hash;
}
4. Inheritance
Key Concepts
- Allows classes to inherit properties and methods
- Uses
extendskeyword - Java only allows single inheritance
- All classes implicitly extend Object
- Use
superto call parent constructor/methods
Example:
public class Animal {
protected String name;
public Animal(String name) {
this.name = name;
}
}
public class Cat extends Animal {
private int lives;
public Cat(String name, int lives) {
super(name); // Call parent constructor
this.lives = lives;
}
}
Method Overriding
- Child class can provide new implementation of parent method
- Must use
@Overrideannotation - Method signature must match exactly
- Cannot reduce visibility
- Cannot override
finalmethods
5. Interfaces
Key Concepts
- Define contract for implementing classes
- Can have multiple interfaces
- All methods are public by default
- Variables are
public static finalby default - Uses
implementskeyword
Example:
public interface Animal {
String vocalize();
void move();
}
public class Cat implements Animal {
@Override
public String vocalize() {
return "Meow";
}
@Override
public void move() {
System.out.println("Cat is walking");
}
}
Default Methods in Interfaces
- Can provide default implementation
- Implementing classes can override if needed
- Helps with backward compatibility
6. Type Casting
Rules
- Upcasting (child to parent): Always safe, implicit
- Downcasting (parent to child): Requires explicit cast, may throw ClassCastException
- Interface casting follows same rules
- Use
instanceofto check before downcasting
Example:
Animal animal = new Cat(); // Upcast (implicit)
if (animal instanceof Cat) {
Cat cat = (Cat) animal; // Downcast (explicit)
}
Study Tips
- Practice writing common method overrides (equals, hashCode, toString)
- Understand difference between instance and static methods
- Draw class hierarchies to visualize inheritance relationships
- Practice implementing interfaces
- Write code examples for type casting scenarios
- Understand when to use private variables with getters/setters vs public variables
Common Pitfalls to Avoid
- Forgetting to call
super()in subclass constructors - Not making fields private (poor encapsulation)
- Inconsistent equals() and hashCode() implementations
- Using == instead of equals() for object comparison
- Trying to access non-static members from static methods
- Forgetting to handle null cases in equals() method
Key UML Concepts
- Class name in top compartment
- Fields in middle compartment
+public-
- private
#protected
- Methods in bottom compartment
- Inheritance shown with solid arrow pointing to parent
- Interface implementation shown with dashed arrow