Understanding classes and objects

There are two major steps in programming.

  1. Coding
  2. Execution

Coding is the time when you are writing the code for your program. Execution is when you run your code. Classes are a part of the coding step and objects are a part of the execution step. When you code you write a class and when you run your program the object of your class is created. Objects cannot be created during coding and classes cannot be created during the execution.

In this tutorial you will learn about classes and objects. Please remember that the concepts you learn here apply to all object oriented programming languages. Once you understand these concepts you will be able to apply them in all your projects.

What is a class?

A class is a blueprint. Or in other words a class is a template. It’s a blueprint of the object. Which object? Any object you want to create!

Then what is an object?

An object is an instance of a class.

Let’s take an example. Below is the description of a small project which you need to do

You need to create a software project to manage all the books in the library. The user should be able to enter the books in the database subject wise. The user should be able enter the name, price, author and subject for every book. After entering the books in the database you should be able to see the list of all the books in the database and also you should be able to search books by name and also by author.

The above description of the project has a lot of information inside it. Let’s try to understand how to understand it from object oriented programming perspective.

First step – Identify the objects in the problem

If you read the above description properly you will find out that it talks about just one object – Book. We are talking about adding books to the database, reading them or searching them.

Second step - Find out the properties of the object

For every object you identify from the problem definition you should try to learn about its properties. From the above definition try to find out the properties for the book object. You will see that the problem statement talks about the name, price, author and subject for the book. These are what we call the properties or attributes or data members of the book object. Keep in mind that you should not assume anything by yourself. If you are not able to understand the properties for the object from the problem statement you should ask for more details.

Third step - Find out the functions which objects can perform

Remember that objects contain data and functions. Now that you know the properties of the book object you should try to find out what functions the book object can perform. The above problem statement does not mention any functions which the book can perform. Also you can well imagine that the book cannot perform any functions (do any work) by itself.

Fourth step - Create the blueprints for your objects

There is only one object in the problem statement – Book. The book has name, price, author and subject as its properties. There are no functions which the book can perform.

Now next you will have to create the blueprint of the book object. Why? To tell the computer how a book object looks like as per your current problem. Somebody else doing another project which involves books will have a different blueprint for books. So you need to find out objects, their properties and functions for every software project you work on. You cannot expect the same blueprints to work for all projects you do.

Finally, lets create our class

From all our understanding till now the book object blueprint will be created by writing the Book class as shown below. Please keep in mind that we have just written pseudo code here. We are not following the syntax of any particular programming language. Its just to explain the concept to you.

Class Book
    String Name
    Float price
    String Author
    String Subject

In this code we have created the Book class with four properties. When you create your class you have to give it a name. Also when you create the properties of your class you have to tell the data type for those properties. This will help the computer know how much space to allocate when you will be creating the object of the Book class.

With every class you write you create a new datatype. We also call it a user defined datatype. You can create variables of this new datatype just like you create variables of the type int, float, char etc.

How to create objects of your class ?

We will not be talking about how to create the object of your class in this tutorial. This is because different programming languages have different syntaxes to create objects. You can learn how to create objects seperately from other EClassrooms where we cover different programming languages.

Related Questions

Quick Links



Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..


Login to Open ESchool OR Create your account    Login   SignUp