Understanding classes
Let's say you want to store the following:
- Individual employee information for a company
- Items for sale on an e-commerce site
- Items you have in your house for insurance purposes
You would use a class for this.
A class groups together variables or constants used to represent an object. Variables or constants associated with a class are called properties.
A class can also contain functions that perform specific tasks. Functions associated with a class are called methods.
Once you have declared and defined a class, you can create instances of that class. Imagine you are creating an app for a zoo. If you have an Animal
class, you can use instances of that class to represent different animal types at the zoo. Each of these instances will have different values for their properties.
Here's what a class declaration and definition looks like:
class ClassName { property1 ...