Is Java really Object-Oriented?
Is Java really Object-Oriented?
It is my understanding that object-oriented programming advocates objects interacting with each other. But as far as I have seen (I learned a little Java) the programs have clear paths of doing things. Like main
routine or some other functions force objects to use their methods.
I have also read that OOP programs are supposed to resemble the real world. In a real world example a cat and a dog would interact without outside intervention. No one would force the dog to bark at a cat but it is in the "dog" object's nature.
But in Java, Objects don't really interact with each other. There is a main
function directing the interaction of the objects. Wouldn't it be nicer if all the objects could identify near objects (without the help of other functions) and run their actions simultaneously? I think it would resemble real world more appropriately.
EDIT:- @Makoto Yes my point is that objects don't interact with each other automatically.
if objects can identify other objects in the same vicinity and run at the same time ( like threads) we can resemble the real world more closely at the same time coding it to do our work.
example : -
class Dog { if you see a cat object in the same place bark at it. if you see the owner object wag your tail. }
If like this we can create a bunch of objects and have them automatically interact , the program would not be linear at all. And yes it is different from the normal OOP or functional programming. But we can achieve the same results or so i hope like in the real world.
Answer by drewmoore for Is Java really Object-Oriented?
class Dog { void barkAtCat(Cat c){ c.gotBarkedAt(this); } void gotHissedAt(Cat c){ barkAtCat(c); } } class Cat { void gotBarkedAt(Dog d){ hissAtDog(d); } void hissAtDog(Dog d){ d.gotHissedAt(this); } }
Here is a very simple example of two Object
s "interacting with one another": Yes, you'll have to initiate the interaction from "outside" - because, after all, this is not the "real world", it's a simulation of it - see @The111, @Sujan and @PelitMamani's answers for valuable thoughts about that.
That said, the OO principle at issue here is that you can model the nature and behavior of the objects in your domain (dogs and cats) in the Object
s in your program (Dog
and Cat
), and this can include interactions between these objects that occur "automatically", as functions of the nature of the objects you've codified. In the example here, a single call to either barkAtDog()
or hissAtCat()
will cause an infinite loop of interactions between these two objects. This is obviously a trivial example, but if you imagine modeling more complex behaviors and relationships between objects, you can begin to imagine simulations of more complex, emergent natural phenomena, and what I hope to have conveyed in this answer is the usefulness of OO in doing so.
In response to your edit - your intuition that multi-threading is essential to any such simulation is correct. In fact, in many intro to Java classes, the project at the end of the multi-threading unit is a simulation very much like the one you describe.
Answer by The111 for Is Java really Object-Oriented?
Any type of programming is implicitly going to include a program to follow and a programmer who writes that program. Programming as it exists today does not involve creating animals who play with each other, unless of course the programmer defines exactly the types of play and the conditions required for each, and includes them in his written program. And quite possibly he would use OOP to write that program.
Your question isn't really about OOP or Java, but programming in general.
Answer by ??? for Is Java really Object-Oriented?
Your question is not about Object Orientated Programming, but programming and algorithms in general. A program does only what it is instructed to do, otherwise it is not a program. We can imagine that real-world like interactions between cats and dogs can be programmed, just like anything else. Programming is about translating real world algorithms and properties into lines of codes.
However, we can agree for now, that a real and physical world cannot be created just by writing lines of codes. Maybe in the future, with breakthrough in holographic and quantum computing, we will be able to create real cats and dogs with some lines of code..
Answer by Junaid for Is Java really Object-Oriented?
Well I would like extend the answer by drewmoore answer little more.
Extension: There is ONE point that need to be cleared. Dog doesnot bark-at cat himself. There is also a medium through which dog knows that cat is in front of him, and the medium is Mind or brain. So I can relate each's brain with the each's actionListener()
. In real world brain triggers the dog to bark and in computer / program world actionListener()
triggers the dog to bark..
Answer by Pelit Mamani for Is Java really Object-Oriented?
Interesting philosophical question. Just my humble opinion:
You aren't necessarily forced to create an accurate copy of the world. You just need a Model that's good enough for your business (and I won't even get into the question of "what's an accurate copy of the world", some people might feel cats & dogs are controlled by some higher power...)
Regarding 'independence': with some real-world scenarios, objects make independent decisions (e.g. your cats & dogs example); with other scenarios, objects are controlled (e.g. if the cat & dog were puppets). Regarding their knowledge of each other - if java made them all know each other by default, that would also fail to reflect the real world: what if there's a cat & dog with a hill between them? Their ability to locate each other would depend on the direction of the wind (=smell) or whatever. So programming languages had to choose some simple defaults, knowing that whatever defaults they chose - they can't presume to reflect all real-world scenarios. Extra coding would be required.
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment