aspose file tools*
The moose likes Beginning Java and the fly likes what is your opinion of objects ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what is your opinion of objects ?" Watch "what is your opinion of objects ?" New topic
Author

what is your opinion of objects ?

naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

hi , i think i m confuse in understanding object in java...i m asking an question please give an simple answer to that and correct me if i m wrong ..ok

consider a class room as a class ...so all the sudents will be an object of that class because all students belong to that class ...right..or wrong
if yes , then all the fans , pc , benches should also be the object of class classroom ....because they also belong to that class ?



The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
This is really great question. Lets hear from the experts.
Ok before that... i think all students, lights & fans are attributes of the class (variables) and not objects of the class.
Say since you said ClassRoom as the Java class & hence lets have there are two different classes taken in that class room. History & Geography.
Ok... now i create two objects like below for the ClassRoom class

next comes the attributes list for the class like below

So when i access historyObj.getStudentNamesList() it should return students attending History classes & respectively Geography students for getObj.
But since the no. of lights & no. of fans will be the same for both the objects we can make that static. so they will be returned the same no.

wait... what was your question
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4544
    
    5

Lesson: Object-Oriented Programming Concepts


luck, db
There are no new questions, but there may be new answers.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

I don't think it's that great of a question. When you design a class, you decide what it's supposed to include. That decision would be based on what the application is going to do. It's up to you to choose what to include and what not to include. So just giving us a list of what is in the classroom is useless; until you have an application which is going to do something with a classroom, it's impossible to know whether you care about the students in it, or the courses which are taught in it, or the number of windows in it, or what floor it is on, or whether it has been reported to have hazardous asbestos in it, or any number of other things.
Michael Ramirez
Greenhorn

Joined: Jul 04, 2011
Posts: 2

Its up to you what is modeled as a class and what isn't.

With your example you might create a "Class" object, and it could contain "Student" objects.

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14153
    
  18

naved momin wrote:consider a class room as a class ...so all the sudents will be an object of that class because all students belong to that class ...right..or wrong
if yes , then all the fans , pc , benches should also be the object of class classroom ....because they also belong to that class ?

This doesn't sound right, but I'm not sure exactly what you mean because saying something like 'X becomes an object of class Y" could be understood in different ways.

It's important to understand the difference between a class and an object first. A class is a blueprint, a plan, that defines what objects of that class will look like.

For example, you can have a class House. This class defines what a House is, by the member variables and methods that it contains. Now, you can create objects out of the House class. These objects will be concrete things made out of the class. For example, you could have my house, or your house - those are objects made from the House class. Note that the House class is not a concrete house object itself - it is just the plan that explains what a house looks like.

If you want to model a classroom with students, benches, PCs, fans etc., then you could make a class for the classroom, but you would also make classes such as Student, PC, Bench, Fan etc. Then in the Classroom class, you would have member variables of the right types to refer to the students etc. that are in a classroom.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Unnar Björnsson
Ranch Hand

Joined: Apr 30, 2005
Posts: 164
No, the students are members of the class not objects. In object oriented programming a class is a blue print or recipe of the objects, so a classroom class would consist of 4 walls 1 floor and 1 ceiling, if the student's were objects of the classroom they would have walls, ceiling and floor which makes no sense.
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

Jesper de Jong wrote:
naved momin wrote:consider a class room as a class ...so all the sudents will be an object of that class because all students belong to that class ...right..or wrong
if yes , then all the fans , pc , benches should also be the object of class classroom ....because they also belong to that class ?

This doesn't sound right, but I'm not sure exactly what you mean because saying something like 'X becomes an object of class Y" could be understood in different ways.

It's important to understand the difference between a class and an object first. A class is a blueprint, a plan, that defines what objects of that class will look like.

For example, you can have a class House. This class defines what a House is, by the member variables and methods that it contains. Now, you can create objects out of the House class. These objects will be concrete things made out of the class. For example, you could have my house, or your house - those are objects made from the House class. Note that the House class is not a concrete house object itself - it is just the plan that explains what a house looks like.

If you want to model a classroom with students, benches, PCs, fans etc., then you could make a class for the classroom, but you would also make classes such as Student, PC, Bench, Fan etc. Then in the Classroom class, you would have member variables of the right types to refer to the students etc. that are in a classroom.


what i understand till now is that object is just a way so that any variable can communicate with that class...is this right ?
if yes , then in my question ....in a ClassRoom class ..door should be the object because it allows variables to communicate with that class....?
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
naved momin wrote:what i understand till now is that object is just a way so that any variable can communicate with that class...is this right ?

No - Object is a blueprint of the class and it is not just an access point to the class (access point here refer to a class' instance methods & variables). Or rather in your words an object is a way of communicating to the attributes of the class. So n number of objects created will have n number of different values set for those attributes.
naved momin wrote:door should be the object because it allows variables to communicate with that class....?

No - door is again an attribute of a class room.
Just ignore my reply if it confuse you still. Anybody else please?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14153
    
  18

John Jai wrote:No - Object is a blueprint of the class

The other way around: a class is a blueprint for making objects.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Thanks Jesper for correcting!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14153
    
  18

naved momin wrote:....in a ClassRoom class ..door should be the object because it allows variables to communicate with that class....?

No, there are many things wrong with this idea. Objects don't allow variables to communicate with a class. A class is not something that you can "communicate" with, variables don't "communicate" with classes in any way.

You have to carefully distinguish the difference between a class, an object and a variable.

As I explained above, a class is a "plan" (or "blueprint") for making objects. For example:

House is a class. It is not a particular, concrete house - it is just a description of what a house (in general) is. You can create objects out of the class. You use variables to refer to those objects.

In these two lines, we are creating two House objects, and we declare two variables, named jespersHouse and navedsHouse, which refer to the two objects.

When you define a class, you define member variables and methods inside it. For example:

Note the following:

1. We have two classes now: a class Door and a class House.
2. Class House has two member variables, named frontDoor and backDoor.
3. These two member variables are both of type Door.
4. There are no objects involved in this last example yet.

Let's now add a constructor to class House.

A constructor is a special block of code that is called when you create a new object out of a class. So, when we create a new House object:

then the constructor will be called to initialize the new House object. In the constructor, the two member variables of the new House object will be initialized to refer to two new Door objects. So, when you create a new House object, it will contain two member variables that refer to two Door objects.

To summarize:

A class is a plan that describes what objects of that class look like.
An object is a concrete instantiation of a class. For example: my house is a real, existing object that follows the plan of a class House.
A variable is something that refers to an object. A variable has a name in your source code, and variables are what you use to keep track of objects.

Please see Object-Oriented Programming Concepts and Classes and Objects in Oracle's Java Tutorials for more explanation.
Unnar Björnsson
Ranch Hand

Joined: Apr 30, 2005
Posts: 164
what i understand till now is that object is just a way so that any variable can communicate with that class...is this right ?


Objects don't communicate with the class itself (unless it has static member variables or functions, but lets not dwell on that for now). The class is something that is designed (hence the blueprint anology) and the objects of that class is the actual concrete objects made from the design, the objects themselves are created at runtime and they represent the class, thus if you design a class with 4 variables and 2 functions the objects have 4 variables and 2 functions.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: what is your opinion of objects ?