Classes & OOP
What Are Classes?
Imagine you're running a pet shop. Each pet has a name, a type, an age, and can do things like eat or sleep. You could write a separate object for each pet by hand, but that gets old fast. A class is like a cookie cutter — you define the shape once, then stamp out as many cookies (objects) as you want.
Each object created from a class is called an instance. The class defines what an instance looks like and what it can do. The constructor method runs automatically when you create a new instance with new.
Creating a Class
Getters, Setters & Static Methods
Getters and setters let you define properties that look like regular data but actually run code behind the scenes. A getter computes a value when you read a property. A setter runs validation or logic when you set a property.
Static methods belong to the class itself, not to instances. You call them on the class name directly (like Math.random()). They're useful for utility functions related to the class.
Getters, Setters & Static
Inheritance — extends & super
Inheritance lets you create a specialized version of an existing class. It's like saying "a Dog is a Pet, but with extra tricks." The child class inherits all properties and methods from the parent, and can add new ones or override existing ones.
Use extends to create a child class and super to call the parent's constructor or methods.