CSCE145 F2026 06 Classes and Objects Part 01
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.
Transcript excerpt
0:00 Hey everyone, how goes it? Welcome back to computer science 145. Today we're going to talk about classes and objects. In a previous lecture, we started going over code organization. As we've started writing code, it's become more and more complex, and we need to better organize statements. That way, we can reuse parts of code, quickly, find bugs and errors, and easily add or extend functionality. Java organizes software into first inside of projects, then inside of classes, and finally inside of methods. Today we begin talking about classes. Classes are a way that we can create classifications
0:48 of objects. Instances of a class are referred to as an object. The terms classes and objects are generally interchanged, but the way to always think about it is an object is an instance of a class. Classes provide a blueprint of a class of objects. Now, these refer to the shared qualities and characteristics of a given class of objects. In programming, classes combine data, which is referred to as attributes or properties, along with methods, which are the actions. In a previous lecture, we talked about how methods are verbs. Now, classes can be thought of as nouns. And remember,
1:37 methods have to be grouped inside of classes. So, a class contains verbs. In other words, this noun contains the actions in which it can do. In order to best explore this concept, let's take a classification of objects that most people would be familiar with. Let's say that we're going to classify a cat. In terms of programming, if we create this classification called a cat, we need to think about what attributes that we're going to assign to it. In other words, what information do we want to keep track of for each one of the instances? In other words, for each one
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
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.