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!
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
Rahul Gupta667 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
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! )
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
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
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
SubString(). They also all have the same properties, like
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.
- The first
Towerin 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.
- The second
toweris 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.
- The equal sign.
newkeyword. You use this to tell C# that you are going to make a instance.
- 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?
Hope this helps
Rahul Gupta667 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 ?
Rahul Gupta667 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 .