Bryan Incognito

Greenhorn
+ Follow
since May 16, 2006
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Bryan Incognito

Max -

I agree with you. I wish that the Sharpen Pencil exercises had answers for later chapters as well!!

So, to your question:
In Chapter 4 (Page 84 in the second edition) of the HeadFirst book, it says that references that you don't initialize in your code by yourself are given a default initialization of null.

So, in your example from the book, Integer i is given a value of Null when you create the TestBox object. When you try to do "j=i;", autoboxing comes into play, which will automatically try to get the int value from Integer i and assign it to int j.

It automatically does something similar to:

j = i.intValue();


But since Integer i is null, you get a null pointer exception.


Java does not have pointers, but only references; if a method is called on a null reference, then you'll get that exception.

I hope that this clears things up.


Bryan
17 years ago
Rajarsi-

I would like to add to what Wise Owen said; perhaps it would help you to see a situation.

An abstract class cannot be instantiated.
An abstract method MUST be overridden by subclasses.


This implies the following:
(I'm keeping it simple, and making the inheritance tree only 2 levels deep)

-- If you have an abstract class, it has to be subclassed (that is, extended) in order to be instantiated.
-- If your abstract class has abstract methods, then those methods have to be implemented in your subclass
-- If your abstract class has all of its methods defined, then it means that writing your subclass is really easy, because all those methods in the superclass are already implemented for you.


Can you see how this works?


A note about my simplification: you can subclass an abstract class, and make your subclass also abstract, and thus it would not have to define the abstract methods. But the first non-abstract class down the tree will have to do so; the methods in the first non-abstract class will be inherited in turn, by its subclasses.
17 years ago
Stephan, just to add to the previous reply...
Your last 3 questions about traversing the inheritance tree:

Your example, where Wolf is a subclass of animals:

animals s=new Wolf();
s.roam();

The JVM traverses the inheritance tree. If Wolf overrides the roam method, then that's the method that will be used. If Wolf does not override the method, then the JVM will walk up the inheritance tree to find the first superclass with that method in it.

The compiler makes sure that Wolf is a subclass of animals, and the compiler lets you call s.roam() because roam() is a method in class animals. It doesn't know/care that s is really of type Wolf, or whether class Wolf overrides the roam() method. (This is because s might be different subclasses, depending on how you write your code and what happens at runtime). The compiler lets you call roam() on s because it knows that roam() is at least defined in class animals, then every subclass of animals has to either a) inherit the method, or b) override it. Thus, Wolf has a roam() method.

Does this give you a better idea of the mechanism?


Bryan
17 years ago
Uh oh, Tony. Is what I said about memory allocation and freeing in C inaccurate?

I'm not at all challenging you if so; actually, I'd like to know so that I can learn something if I've made a mistake.

Thanks.


Bryan
17 years ago
Thanks, Jeff!

That's kind of what I was expecting, that there was some other thread, or maybe some sort of other reference in some JVM static something or other...

Thanks for your clear explanation.


Bryan
17 years ago
Hi, Jim.

It sounds to me like your question is kind of security oriented. Is this correct?

If you program in C, memory that's been allocated and then freed will still hold its data until it's used again.

Here's a scenario where this could be useful...
some program is running on your machine with sensitive information. You want to find out some of this information, so you allocate a big chunk of memory and then scan the memory for strings. You *might* be able to get some of the data in the memory that was freed by the other program, if the OS gives you a chunk of memory that was previously used by that program.

Is this what you are asking about, but in Java?
17 years ago
I have a question that's been bugging me for a while...

It occurred to me that I don't know why my main method does not return until *after* I close my GUI window.

Typical code is like this:

public static void main(String[] args){
JFrame frame = new JFrame();
// configure frame size and exit on close
// make and register a bunch of components, like buttons, etc.

frame.setVisible(true);
// some other program logic here
}

So, after the frame is made visible, my program window appears. Then other stuff in main happens.

So, what's going on? Why doesn't my main method reach its end, and then the program end? How come the JFrame variable doesn't go out of scope? What's holding the main method in execution state?

Thanks, I appreciate explanations that you can give.


Bryan
17 years ago