KnowledgeBoat Logo

Objects & Classes

Real World Example - Objects & Classes

ICSE Computer Applications

Let’s take a real-world example to understand classes and objects better. Let’s say I want to develop a dog adoption website. To build this website, I need to model dogs a real-world entity into my software. Let’s focus on how we can represent a dog using classes and objects for our dog adoption website.

Three dogs of different breeds

Here we have pictures of 3 different dogs. Take a moment to figure out the differences between these dogs.

Let’s list down the differences:

  • Each dog is of a different breed.
  • They have different colours.
  • Their age looks to be different.
  • Maybe some are male some female.
  • They might have different owners.

So, breed, colour, age, gender, owners these might be some things that are different for these dogs. If I say these differences are also some common characteristics shared by the dogs, wouldn’t you agree with me? If I must describe a dog, I will use these characteristics like breed, color, age, gender, owner. So, these characteristics can form the data members for dogs class.

Now lets look at the common behaviours of these dogs. Dogs sleep, eat, sit, run, bark. These common behaviours can be the functions or methods of dog class.

Data members and member functions to dog class to explain ICSE Computer Applications

Now for different values of data members you will get different dog objects. Since the data elements of an object can vary, each object of the same type may have different data but will have the same functionality as defined in the methods.

Different dogs as objects of dog class to explain ICSE Computer Applications

So, the first dog can be a female white American Eskimo who is 6 years old and is Krish’s pet. The second dog can be a male black French Bulldog who is 4 years old and is Avni’s pet. The third dog can be a brown 7 years old male Boerboel who is Alex’s pet. But all the three dogs have the same behaviours as in they all eat, sleep, sit, run, bark, etc.

Class as an Object Factory

Now that we have developed some basic understanding about classes and objects, lets try to understand why classes are considered object factories?

A class has the complete description of the data elements the object will contain, the methods the object can do, the way these data elements and methods can be accessed. How will I get an object of a class? To get an object, I tell the class that I want a new object of this type. Given the values of the characteristics, a class knows how to create an object of itself. It will check if we have specified all the characteristics required to create the object, and the specified characteristics are valid. Then it will create an object of the class setting its initial state to the value of the specified characteristics.

This is similar to what happens in a factory. Consider a car factory, there will be one design of a car based on which many cars of different colors will be manufactured. If you compare this with our previous example, based on one Dog class many dogs of different breed, color, gender,etc. can be created as objects. Thats why we say that class is an object factory. Objects of a class are also referred as instances of the class as they represent the class with a specific state.