| Author |
Why need objects?
|
rachna shetty
Greenhorn
Joined: Oct 28, 2012
Posts: 2
|
|
Why should we create an object? what is a need of it? Apart from access members of a class i want what is a need of object what does it contain?? In normal class to access any member of that class we create object where as for static class we access its members using class name. what is a difference between them. iknow we can create an object for static class but no use of it. thanks in advance.
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56207
|
|
|
Think about it for a minute. One dog is named Rover and is a Bulldog. Another dog is named Fifi and is a poodle. How would these "states" be captured without separate Dog objects?
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
William P O'Sullivan
Ranch Hand
Joined: Mar 28, 2012
Posts: 860
|
|
Good analogy Bear.
I was asked in an interview once, "What is an object?"
The interview had a can of Coca Cola that he drinking from.
I said, that can is an object, it has properties, methods and you can interact with it.
I got the job.
WP
|
 |
rachna shetty
Greenhorn
Joined: Oct 28, 2012
Posts: 2
|
|
Bear Bibeault wrote:Think about it for a minute. One dog is named Rover and is a Bulldog. Another dog is named Fifi and is a poodle. How would these "states" be captured without separate Dog objects?
thank you but why cant we use static class members access methodology to access any other class members(with classname ex: classname.member)?
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56207
|
|
Think again. If the Dog class had a static member named name, and it was set to "Rover", how would we model "Fifi"? The static variable can only hold one value at a time.
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5890
|
|
rachna shetty wrote:Why should we create an object? what is a need of it?
We don't need objects. Everything we do in Java could be done in assembly language, with no notion of objects whatsoever.
However, objects allow us to model physical items and abstract concepts in the domain we're coding for. As another example of what Bear already pointed out, if we are writing software for a bank, we can define an Account class, that captures what kind of state a bank account keeps and what kind of operations it can perform, and then we can create multiple Account objects to represent the real bank accounts we're dealing with. One Account object has accountId=123456 and balance=$500.00 and another Account object has accountId=9876543 and balance=$0.01.
Now, again, we don't need these objects as such, but expressing and structuring the data this way makes it easier for us human programmers to keep track of what we're doing.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32708
|
|
People managed without objects before OO (=object‑oriented) programming was invented (about 1967 with Simula67).
In C you can use a struct to retain the data. But you cannot model behaviour along with those data; you have to implement the behaviour elsewhere.
But using Java, which is intended for OO programming, you need to use objects to utilise the full power of the language.
|
 |
 |
|
|
subject: Why need objects?
|
|
|