To illustrate the difference between a class and an instance let’s take a look at applying the concept to dogs.
Class
A class is a definition for a collection of objects. For example, if we define a class for Dogs it would include all the characteristics common to all dogs.
The class is a template: it defines only the parameters these objects share, functions they can perform, and logic for how an object can be constructed. A class becomes an object only when you make an instance of it.
Let’s try to make a Dog Class
All Dogs have:
- name
- breed
- hair
- legs (usually 4)
- head
- tail (most of them)
All Dogs can:
- bark
- run
- eat
Now let’s make a few instances of dogs
Instance 1: Pluto
- name = Pluto
- breed = Bloodhound
- hair = short and light brown
- legs = 4
- head = large
- tail = yes
- bark = a lot
- run = fast
- eat = cheeseburgers
Leave a Reply