• Post Reply Bookmark Topic Watch Topic
  • New Topic

need help in understanding classes & objects.  RSS feed

 
akshay naidu
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i've gone through many links for this,but m still very much confused about objects and classes,
illustrate a simple program to understand if possible.
thanks in advance
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a very general question. Can you explain in more detail what exactly you don't understand about objects and classes?

A class is a blueprint for making objects. For example, suppose you have a class House. You write the source code for the class, and it describes exactly what a House object should have: data, in the form of member variables, and operations you can do on that data, in the form of methods.

At some point you'll want to use the class House to make House objects. For example, you can make a House object that represents your house, and another one that represents your brother's house. You create instances of the class House. ("Instance" is another word for "object").

For example, here is a class House, that says that a house has a street name, a house number and a city name.


You can use this class to make House objects:

 
akshay naidu
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks jesper

i am a java beginner.



in this program there are two classes
"class Movie" is a blueprint,m i right?
three "Movie" objects are created in MovieTestDrive class.

please explain me working of object and everything you can explain about this program.
thank you
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct in the above - there are two user defined classes: MovieTestDrive and Movie. There are other classes provided by the JDK that get used here, such as the String class.

akshay naidu wrote:
please explain me working of object and everything you can explain about this program.

That is again an extremely broad question. "Everything you can" is really TOO broad. Can you be a little more specific in what you'd like clarified?
 
akshay naidu
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there are many questions.very very basic ones.dont understand which one to ask,
let me start with the latest question that came in my mind
why do we create three classes when we are using only MovieTwo object.
and why writing so many things as in the output i see only "playing the muvi"
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read this and this.

Yes, a class is like a blueprint. Have you ever seen a blueprint in real life? I have, but I suspect they are no longer used.
The code you showed does instruct the JVM to create 3 Movie objects, and it also includes I think 6 String objects.
 
akshay naidu
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello campbell, i'll take care of this and this that you have mentioned.
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
akshay naidu wrote: . . .
why do we create three classes when we are using only MovieTwo object.
I can only see two classes. Are there more you haven't shown us? Is that code from Head First Java? Which page?

and why writing so many things as in the output i see only "playing the muvi"
You have only told it to output playing the muvi; nothing else that you have written is input or output.
 
akshay naidu
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes its from head first java page no. 37
there are two classes only.
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code creates three Movie objects because you write new Movie() thrice.
 
Aaron Shawlington
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Repeating what has already been said, but in different words incase it helps. If this makes no sense, then ignore it...

You could think of a class as a recipe. A recipe has a list of ingredients and a load of actions which need to be performed using those ingredients (in order to produce some output such as a cake).

In your Java class you'll find data and methods. The data is like the ingredients, and the methods are like the actions to be performed (such as beating the eggs).

The thing to remember about recipes, though, is that you can have different variations of the same recipe, right? When the recipe says you need 300g of flour to bake your cake, you could - if you wish - use double the amount if you want to make a cake twice as big. Also you could choose to use Raspberry jam instead of strawberry jam. Etc.

So a class is like a recipe that isn't too strict - it might force you to use flour, but it might not care how much. Or it might force you to have jam, but you might be free to use whatever flavour of jam you like so long as it's a type of jam.

So lets take a CakeRecipe class and create 2 instances of it (2 objects by using new CakeRecipe() twice). Lets suppose that by default the jam is Strawberry - when we invoke the bake method on each (recipe1.bake(); recipe2.bake()) we might get back 2 strawberry cakes.

Now lets suppose instead that we created our two Cake class instances:



but before we bake them we change the data in recipe2 to use raspberry jam instead of strawberry:



And then we bake then by invoking the bake method in each recipe:



cake1 will have strawberry jam while cake2 has raspberry jam.

And because the Cake class, like all classes, is a bit like a recipe - it also has data (jam flavour) and methods.
Perhaps the cake has a method calculateFatContent(). Now because cake1 and cake2 have different jam, perhaps they have different fat contents in the cake.

Perhaps cake1.calculateFatContent() returns 50g, and cake2.calculateFatContent() returns 45g.



In that sense, Objects are a grouping of methods (programming logic) and data into a cohesive unit - rather than random data being accessible by random functions for any random reason. And classes are templates (or recipes) of that grouping. When you get an object you know what class type it is, which means that you know what methods it has and what data you can manipulate. Because all objects are of a class type, when you create new objects you are forced to follow the template (or recipe). This can help to keep your code neat & tidy & maintainable.

There are downsides to object orientation, but you shouldn't worry about that yet.


 
akshay naidu
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Aaron.,
cake example was really very helpful in understanding.

i completed the chapter 2 (trip to objectville) & 3(know your variables) in head first java and moving to ch 4 how objects behave
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!