• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Object creation

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have recently started studying java programming language and have these few questions.
What does it mean when you say java garbage collects any unused objects? I have compiled a java programme( .java is created) and then ran it using the class name. I presume it is at this point that an object is created. Once the program is run the object created should go as far as I understood. Is that right? So any object that is creatd should be garbage collected once it is run, right? Why is there a big deal on "when to garbage collect an object"? I know my understanding is not very clear, what am I missing? If some other porgram is running which uses this program then it can't be garbaged. But if there is nothing else that is referencing this program, it will be garbage collected, right?
What is the difference betweeen creating an object on stack and creating them on heap? If performance is such a big issue, why not create the objects on stack itself( is stack a limited resource?)?
This is on inheritance ->
I have a class which extends another. If I create an object of the class B (extending A) do I have access to all the private methods of the class A?

A a = new B().
Can I access a.somePrivateMethodOfA ? If yes how is private? If not how exactly "is" B A?
I know, I have to read a little more. But I am not moving forward, it is too confusing and I thought I could use some help.

Thanks.
Shails
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Welcome to JavaRanch!

Originally posted by shailu kommineni:

What does it mean when you say java garbage collects any unused objects?


A single program will create many objects (hundreds or thousands or millions, even.) Some of these objects will be used he whole time the program is running; others are needed only for a few lines of code and then never used again. In some languages (C++), you have to explicitly call a function to say you were through with an object so that the memory it uses can be reclaimed. In Java, this is automatic: an object that can no longer be reached via any variable is automatically reclaimed.


What is the difference betweeen creating an object on stack and creating them on heap? If performance is such a big issue, why not create the objects on stack itself( is stack a limited resource?)?


Slightly oversimplifying: Stack layout is determined at compile time. If you need to create N objects, or an N element array, but you don't know what N is until the program is running (because N comes from user input, for example) then you can't allocate the objects on the stack -- they have to go on the heap.

This is on inheritance ->


I have a class which extends another. If I create an object of the class B (extending A) do I have access to all the private methods of the class A?


Nope.



If not how exactly "is" B A?


Because anything that some other class C can do to an A object, it can also do to a B object; for example, if C has a method twiddleA() which accepts an A as a parameter, you can pass a B when you call it, and everything works fine.
[ January 26, 2004: Message edited by: Ernest Friedman-Hill ]
 
shailu kommineni
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. If C expects an object of A but you are passing an object of B. If in the method twidleA C is creating an object of A and on the instance of A calling some private methods of A, then can it do the same when you pass an object of B?
2. Also I pass B to twiddleA method of C which has a serveNoodles method. It will use a method of A say, cookNoodles which boils noodles without frying them but same method of B will fry them and then boils. C expects some behaviour but a different behaviour results when you pass a different ( subclass) object of A (B). Then how can you say that everything works fine?
3. What is the best book to study java if I want to know what exactly happens behind the scenes as well?

Shials
(I am really thrilled to receive response from an author!! Is your book on java? Then you will obviously say it is the best book? )

Originally posted by Ernest Friedman-Hill:
Hi,
Welcome to JavaRanch!

Because anything that some other class C can do to an A object, it can also do to a B object; for example, if C has a method twiddleA() which accepts an A as a parameter, you can pass a B when you call it, and everything works fine.
[ January 26, 2004: Message edited by: Ernest Friedman-Hill ]

 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by shailu kommineni:
1. If C expects an object of A but you are passing an object of B. If in the method twidleA C is creating an object of A and on the instance of A calling some private methods of A, then can it do the same when you pass an object of B?


By definition, C can't call any private methods of A. That's what "private" means -- not accessible outside the defining class. I'm not sure what you're driving at. The point is that anything you can legally do to an A object, you can legally do to a B, and it is in that sense that a B is-a A.


2. Also I pass B to twiddleA method of C which has a serveNoodles method. It will use a method of A say, cookNoodles which boils noodles without frying them but same method of B will fry them and then boils. C expects some behaviour but a different behaviour results when you pass a different ( subclass) object of A (B). Then how can you say that everything works fine?


I had a good laugh reading this
Anyway, that's a good question. I think you'll come to appreciate the answer after you work with the language for a while. Imagine that A is actually called simply "Noodles," while B is called "FriedNoodles". C is called "Restaurant." Now, let's say we pass a Noodles object to Restaurant.serveNoodles(), which calls cookNoodles(). The customer gets a dish of boiled noodles. Now, instead, say we pass a FriedNoodles to Restaurant.serveNoodles(). In this case, the customer gets fried noodles.
You made it sound like this is bad, but actually, it's a great thing. The serveNoodles() method knows to call cookNoodles(), and then to put the noodles on a plate and bring them to the customer. It works equally well with many different kinds of noodles. We don't have to rewrite serveNoodles() for each different kind of noodles. This is a good thing! serveNoodles() shouldn't know anything about cooking, or what kind of noodles. This lets the code for serving and the code for cooking vary independently, and it eliminates duplicate code. This is the whole point of polymorphism -- a fancy word which just means that different classes respond to the same method call by doing different things.


3. What is the best book to study java if I want to know what exactly happens behind the scenes as well?


It's hard to pick a "best book." The JavaRanch Bunkhouse is filled with honest reviews of hundreds of books. Go have a look and choose for yourself!


(I am really thrilled to receive response from an author!! Is your book on java? Then you will obviously say it is the best book? )


I did write a general Java text some years ago, but I couldn't in good conscience call it the best of anything. My current book is a bit more specialized; check out the link in my signature if you're interested.
[ January 26, 2004: Message edited by: Ernest Friedman-Hill ]
 
shailu kommineni
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I have read about polymorphysm. I am really embarassed now.
Thanks for your answers.

Shials
 
I once met a man from Nantucket. He had a tiny ad
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic