Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Objects Object-Oriented Programming Instantiation

Raihan Khan
Raihan Khan
2,168 Points

What is the purpose of using an object. I didn't understand or not clear.

Tower tower = new Tower(); Can someone explain in details what he mean by instantiation in class or the purpose of creating an object in Main. Can we do it with out, if not then why...

2 Answers

In few words an object is just like a class that will contain its atributes. For example, a Car is an object and its atributes could be: red, 170cm, 900kgs, 4 seats, nissan. In programming you could do this like:

Class car{ color: red height: 170 weight: 900kgs car_brand: nissan }

Steven Parker
Steven Parker
231,275 Points

You're taking the "Object-Oriented Programming" course. The whole point is to learn about how objects can be used to create coherent program structure. The answer to the question "Can we do it with out?" is yes, but a program written that way would have a very different structure.

Objects simplify working with multiple things of the same type. Each instance represents one of the things, but they all share the same definition that establishes fields, properties, and methods to enable what they do. The "Tower" object is a good example because you may have several of them in the game, but they all do the same thing and each will store the same kind of data and use the same methods.

And "instantiation" just means "making a new thing of this type", which explains why the operator to do it is called "new".

Raihan Khan
Raihan Khan
2,168 Points

I know i am taking oop course but what I want to know why we are using Tower tower = new Tower(); what is this line represent. I know i have to use object but why we are using this line in every class. For ex. in map class we are using Map map = new Map();... what is it doing...

Steven Parker
Steven Parker
231,275 Points

Those lines create a new instance of the class, a new "Tower", or a new "Map". The class itself is simply a blueprint, instantiation is using the blueprint to build a thing based on it.