• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

what is your opinion of objects ?

 
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?


 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lesson: Object-Oriented Programming Concepts
 
Sheriff
Posts: 28332
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 2
Google Web Toolkit Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jesper for correcting!
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Hug your destiny! And hug this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic