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 trialAmal Karim
5,329 PointsConfusing definition of class and object
I'm confused with this definition of class and object. I still can't see how they related each other. The "FunFactsActivity" in our file FunFactsActivity.java inside our project folder is declared as a class that extends the "Activity" class. It's a class extends other class, right? But at the same time, teacher Ben said that this "FunFactsActivity" is an object (please refer to minute 01:52).
In other programming language like PHP, I know that a class is created as a blueprint. To use a class, we must create an instant of that class by write code like:
MainActivity = new FunFactsActivity;
Then we can start using this "MainActivity" object.
But in this video, we are actually using that class instead of creating an instant of that class.
If I get it wrong, please explain it to me how it exactly means. Thanks!
3 Answers
Seth Kroger
56,414 PointsWhat's happening is the Activity (or FunFactsActivity) object is created and managed by the Android OS. So the OS will call the class methods like onCreate(), etc., when appropriate. We don't need to create new FunFactsActivity objects ourselves, just provide the right methods for the OS to call.
Nelson Ramos
9,338 PointsFrom video transcript 1:29, "Think of a Java class as the blueprint from which objects are created."
This may be an oversimplification, but it helped me: A Java 'class' is a recipe for pie (it can be a recipe for any dish, but I love pie); an 'object' is the pie itself, made from the recipe.
From video transcript 1:04, "... an object has its own properties and abilities..."
The pie (object) can be Key Lime, Cherry, Apple... you get the idea.
George Pirchalaishvili
3,747 PointsClass is an object with default values (like a definition) . So FunFactsActivity is an object with some default values.
When you make Activity newActivity = FunFactsActivity, you create new object of FunFactsActivity class (so this object is defined by methods and values FunFactsActivity has).
Makes more sense ?
Amal Karim
5,329 PointsThank you for your reply, George. Yes, I understand the point you wrote. I just can't see the process of creating new object in the code.
Seth Kroger
56,414 PointsNot exactly. A class is a definition for an object, but not an object itself. You can define static members that don't need an object to be called, but to call/refer to non-static members you need to actually create on object.
As I said in my answer, the trick is that the object is created somewhere else.
Amal Karim
5,329 PointsAmal Karim
5,329 PointsHi, Seth. Your answer makes sense. So, it is basically the system that creates the object for us. We don't need to create it ourselves, right? Thank you for the answer!
David Palczewski
2,557 PointsDavid Palczewski
2,557 PointsThanks for clarifying this, when the Instructor mentions that "FunsFactActivity" was an object, I was a bit confused and I was already pretty familiar with java.