CSCE145 F2026 06 Classes and Objects Part 01
Watch on YouTube →
Overview
UofSC_CSCE145_CSCE146 introduces Java classes and objects as reusable blueprints that combine private attributes with methods, using a Cat class with name, weight, and numberOfLegs properties. The lecture demonstrates constructors, encapsulation, getters, setters with validation, equals(), toString(), reference variables, null, aliasing, and garbage collection through Cat objects such as Mr. Fluffkins, Triscuit, and Dr. Boots.
Key takeaways
- A Java class combines encapsulated state and behavior: Cat stores name, weight, and numberOfLegs while exposing actions such as eat(), sleep(), and destroyYourStuff().
- Constructors are the only mechanism demonstrated for creating new Cat instances; new Cat() applies defaults, while new Cat("Triscuit", 9.2, 4) initializes supplied values.
- Private fields require public getters and setters, allowing Cat to enforce rules such as weight > 0, numberOfLegs between 0 and 4, and non-null names.
- equals() compares object contents, whereas == compares reference addresses; two Cats with identical properties can therefore be equal through equals() but false through ==.
- Assigning one object reference to another creates an alias rather than a clone, so cat02 and cat03 can point to the same Cat and observe each other's mutations.
- Objects with no remaining references become eligible for Java garbage collection, which automatically reclaims their unreachable memory.
Chapters
- Java organizes software into projects, classes, and methods; classes act as noun-like containers for data and verb-like actions.
- A class is a blueprint, while each object is an instance of that class with its own attribute values.
- The Cat example tracks name, weight, and numberOfLegs while sharing eat(), sleep(), and destroyYourStuff() behaviors across objects.
- Mr. Fluffkins, Triscuit, and Dr. Boots illustrate that separate Cat instances can have different data while using the same methods.
- A user-defined class becomes a Java type, and its public class identifier must match the filename; Java generally uses one external class per file.
- Declaring Cat cat01 creates a reference variable, but cat01 initially contains null rather than a constructed object.
- The new keyword invokes a constructor and dynamically creates an instance in heap memory, as with Random r = new Random().
- Calling a method through a null reference causes the runtime error NullPointerException, so references should be checked before use.
- Cat defines private instance variables String name, double weight, and int numberOfLegs.
- Private scope implements encapsulation, or information hiding, by preventing outside code from directly changing object state.
- Encapsulation lets the Cat class validate values before accepting them, preserving constraints such as positive weight and a realistic leg count.
- A separate HouseOfCats class contains main() and uses Cat objects without directly accessing their private fields.
- Constructors have no return type and must use the exact class name, Cat; they create and initialize Cat instances.
- The default constructor takes no parameters and assigns name = "none", weight = 1, and numberOfLegs = 4.
- The parameterized constructor accepts a String name, double weight, and int numberOfLegs to initialize all three properties during construction.
- The this keyword refers to the current Cat instance, such as this.name and this.numberOfLegs.
- Public accessors such as getName(), getWeight(), and getNumberOfLegs() return private Cat properties without parameters.
- Public mutators such as setName(), setWeight(), and setNumberOfLegs() modify private properties through validated parameters.
- setName() replaces null input with "none", setWeight() accepts only values greater than 0, and setNumberOfLegs() accepts values from 0 through 4.
- The parameterized Cat constructor calls the setters so constructor input receives the same validation as later updates.
- cat01 is first created with new Cat() and later changed to Mr. Fluffkins with weight 8 through mutators.
- cat02 is created with new Cat("Triscuit", 9.2, 4), setting all properties in one statement.
- Each call to new Cat() creates a separate object instance while reusing the Cat class definition and methods.
- The example demonstrates why constructors improve convenience and consistency when creating many objects from one class.
- Cat.equals(Cat other) first checks other != null, then compares name with String.equals(), weight with primitive equality, and numberOfLegs with integer equality.
- Cat.toString() concatenates name, weight, and numberOfLegs into a readable string for debugging and printing.
- Passing cat02 directly to System.out.println() automatically invokes its toString() method.
- Behavior methods include eat(), which increases weight by 1, sleep(), which prints a cat-snoring message, and destroyYourStuff(), which prints a playful destruction message.
- cat01.equals(cat03) returns true when both Cats have identical properties, while cat01 == cat03 returns false because their memory addresses differ.
- For class types, == and != compare references, making cat == null the standard null check; equals() compares object contents.
- Assigning cat03 = cat02 does not clone a Cat: both identifiers reference the same object, so changing cat02's name to Dr. Boots also changes cat03's observed name.
- The original object loses its reference after reassignment and becomes eligible for Java's garbage collector to remove from memory.
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, UofSC_CSCE145_CSCE146.