Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C#

Rahul Gupta
Rahul Gupta
667 Points

OOP confusion - Tower tower = new Tower ()

I am a little confused about the Tower tower = new Tower ()

Is it correct that we are :

->Using "Tower " because thats the Class we created and now we have to "call "this Class to make an object with the same properties and behaviors created in that "Tower " Class?

--> We are using "tower" because of the naming convention and , simply because it would help us recognize which class we are working with at the back end and also to give us clear vision of What object is related to which Class?

--> We are using "new " because thats how we create a new object .

--> We are using "Tower()" because thats the ................ I am confused about this one but is it because we are creating an object out of the "Tower " Class and using "()" to have the user input the parameters as we defined in the actual Class?

I am sorry if my question is confusing , i am a new learner and dont want to proceed without understanding what is at hand right now . I have been watching videos and reading about it and any help from here would be highly appreciated to clarify the doubts .

Thanks a lot

3 Answers

OOP is a very confusing part of programming.

I will try to explain it my best. Reply if you don't understand a particular part.

OOP is the style of writing classes in programming. Classes can be sometimes referred as objects because they can model real-world objects. In fact, the word "class" is short for "classify".

Think of a class as a cookie cutter. You can make instances—or copies—of the cookie cutter by stamping out a cookie. All of the cookies have things in common, like shape. However, the cookies don't have to be exactly the same. For example, one cookie can be green, and another red. (Yay, Christmas cookies! :laughing:)

Here's another example. Think of a Car class. It might have a couple methods, or actions it can do. If I made that class, I'd probably put Drive() and Stop() methods, because most cars can drive and stop. Of course, you can put as many methods as you want. Maybe you'll put a Beep() method. Classes also have what is called properties. They aren't actions. They are more of like variables, because they can hold values. Our Car class may have color and topSpeed properties. Just like methods, you can have as many properties as you want.

In fact, all of the built-in things you've been using, like strings, are classes behind the hood. You make a new copy (instance) of the string class by wrapping text in quotes. They are unique because they can have different content, but they all have the same methods, like Split() and SubString(). They also all have the same properties, like Length.

Now that you're sort of familiar with classes, let's look at this line of code:

Tower tower = new Tower();

I will break it up step-by-step.

  1. The first Tower in the line of code is the type of the variable. With strings, you would write string. With integers, you would write int, as so on. We still have to specify the type, which is the name of our class. The name of our class is Tower, so that's why that is there.
  2. The second tower is the name of the variable. You can call this whatever you want, but it is conventional to have the name of the variable to be the name of the class lowercased.
  3. The equal sign.
  4. The new keyword. You use this to tell C# that you are going to make a instance.
  5. The final Tower() on the line. This is where the magic is. This is the class that you are going to instantiate, and you may put data between the () if the class's constructor excepts parameters.

Does this help? :smiley:

Happy coding! :tada: :confetti_ball: :tada:

Hope this helps :grin:

:dizzy: ~Alex :dizzy:

Rahul Gupta
Rahul Gupta
667 Points

That helps a lot Alex , Thanks for the example and helping out with this confusion . I do have a question :

1)Am i correct in saying that "tower " is the name of the object "Tower()" of class "Tower"? 2)and the () is for any constructor method's that go in ? or 3)and the () is for any constructor Attributes that go in ?

Thank you

These may answer your questions, but I may not understand your questions fully. Your questions look like gibberish to me. (Sorry if this sounds mean, but it's true. I'm not sure what you are trying to say.)

1) The new Tower() code creates an instance of Tower, and we are simply storing that instance to a variable called tower.

2) The Tower class's constructor method is called every time you instantiate a new instance. new Tower() in your code snippet activates this method.

3) No. If the constructor takes in parameters, you can pass in the values, but if the constructor doesn't take in parameters, you can't pass in parameters. Remember that you can use the values from the parameters in the method.

Rahul Gupta
Rahul Gupta
667 Points

Thanks, Alexander, I know that I am not even able to construct the right question because of the confusion. I do appreciate the time you take to explain the matter. It def is getting clearer as i read more bout it .