• Post Reply Bookmark Topic Watch Topic
  • New Topic

Beginner - Need help to understand how this code works  RSS feed

 
John R Watson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would help if you told us which parts you don't understand.
If you're expecting someone to explain the whole thing to you line by line, then you're probably going to be disappointed.
 
John R Watson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:It would help if you told us which parts you don't understand.
If you're expecting someone to explain the whole thing to you line by line, then you're probably going to be disappointed.


No, I'm not expecting line by line. Lets start from the beginning, and correct me if I'm wrong.

PhoneBook pb = new PhoneBook();


PhoneBook pb is a object reference variable, that holds the reference to a newly created PhoneBook object?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnn Watsonn wrote:PhoneBook pb is a object reference variable, that holds the reference to a newly created PhoneBook object?

Correct
Look at the PhoneBook constructor next. Do you understand what that's doing ?
 
John R Watson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
Johnn Watsonn wrote:PhoneBook pb is a object reference variable, that holds the reference to a newly created PhoneBook object?

Correct
Look at the PhoneBook constructor next. Do you understand what that's doing ?


A array object of size 5 is constructed, and the reference is put in phoneBook? I have a slight problem with this.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnn Watsonn wrote:A array object of size 5 is constructed, and the reference is put in phoneBook? I have a slight problem with this.

That's no different to the PhoneBook pb = new PhoneBook(); line. You create an object and assign it to a reference variable.
Are you confusing PhoneBook (the class name with a capital 'P') and phoneBook (the variable defined on line 13 which is a reference variable to an array of PhoneEntry instances) ?
A better name for the phoneBook variable would be something like phoneBookEntries.
 
John R Watson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
Johnn Watsonn wrote:A array object of size 5 is constructed, and the reference is put in phoneBook? I have a slight problem with this.

That's no different to the PhoneBook pb = new PhoneBook(); line. You create an object and assign it to a reference variable.
Are you confusing PhoneBook (the class name with a capital 'P') and phoneBook (the variable defined on line 13 which is a reference variable to an array of PhoneEntry instances) ?
A better name for the phoneBook variable would be something like phoneBookEntries.


Well actually, it's the first time to come across instance/object variables of a class, that are not primitive, as in this case:



So I've got no idea how to deal with it. From my understanding, with primitive instance variables, the constructor simply initializes them. So the object will be made up with fields of primitive type, initialized by the constructor. If there is no constructor, or the constructor doesn't do anything, then they are provided the default values of their type. Here the instance variable of the class is an array. I'm not sure what the object will be.



Not sure what exactly happens after this statement.

pb is an an object reference variable to an object of type PhoneBook?

That object will be an array of size 5? And it's reference held in pb?

And then the constructor, phoneBook(), is responsible for initialising the 5 slots in the array? Still confused.



 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnn Watsonn wrote:Well actually, it's the first time to come across instance/object variables of a class, that are not primitive, as in this case:



So I've got no idea how to deal with it. From my understanding, with primitive instance variables, the constructor simply initializes them. So the object will be made up with fields of primitive type, initialized by the constructor. If there is no constructor, or the constructor doesn't do anything, then they are provided the default values of their type. Here the instance variable of the class is an array. I'm not sure what the object will be.

And exactly the same thing happens with instance variables that are objects. They are set to a default value - the default value for object references is null (meaning it's not currently pointing at any object).

Johnn Watsonn wrote:

Not sure what exactly happens after this statement.

pb is an an object reference variable to an object of type PhoneBook?

No. It is an object reference variable to an object of type array of PhoneEntry
Sorry. Misread your post. Yes that's correct, but we dealt with it earlier and you said you understood it.
We are now dealing with the constructor where the important line is

Here we are initialising the phoneBook instance variable to point to a PhoneEntry array.

Johnn Watsonn wrote:That object will be an array of size 5? And it's reference held in pb?

And then the constructor, phoneBook(), is responsible for initialising the 5 slots in the array? Still confused.

Correct on all counts.
You said earlier that you knew primitive instance variables could be initialised in the constructor.
Exactly the same is happening here except that it's an object instance variable that is being initialised.
On line 16 you initialise the variable to point to an array. At this point the array exists but is empty. Lines 17 - 26 add PhoneEntry instances to the array.

Does that help ?
 
John R Watson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Joanne for your patience. I can be a very frustrating student to teach! I'm completely new to programming, and Java is the first language that I'm learning, and I'm quite obsessed with small details, as I believe it's important to get it right from the start, to avoid potential problems down the line.

Anyway, I had read your comment, before you crossed out that part, so I was confused. I went off and drew it all on paper, so here is what I have at the moment:

Java program starts running from this point, looking for method main(), and beginning from it:


The following code, sets up a pb object reference variable. It's just a object reference variable, it can hold a reference to any object you want it to point to. The new keyword allocates space in the memory. Sets up a PhoneBook object. The object, will contain a PhoneEntry[] phonebook instance variable/object variable, with value NULL at this moment in time, since no array object exists for it to point to, and hence it points to nothing/Null. The constructor, PhoneBook() is invoked/called. Like any constructor, it initializes the instance/object variables of an object. In this case, the instance variable of the PhoneBook object is PhoneEntry[] phonebook, which is initialized. This is done by the creation of a new array object of type PhoneEntry. Now the PhoneEntry[] phoneBook; instance variable will contain a reference to this newly created 5 slots PhoneEntry array.



The following code, a "James Barclay" String Literal object is created in memory. Likewise, a "(418)665-1223" String literal object is also created, in another location in memory. The new keyword, sets up a new PhoneEntry object, whose reference will be placed in phoneBook[0]. That object, will contain 2 things. Two String instance/object variables, called "name" and "phone". Each one will contain a reference to the String Literal objects, "James Barclay" & ""(418)665-1223"" respectively.




The same is done for phoneBook[1] to [4]. Is this correct so far?

---------------------------

Another thing is the Java books I have, and the Java online tutorials, simply teach you the Java language. But not how to think like a Programmer. How to think in an Object-orientated way, if that makes sense. Which is why I seem to be struggling. As I could understand some of the code in programs I've came across, but not how the person came up with the program, how they built it and made it all work together. I look at the code, it makes sense to me, but what doesn't make sense, is the thought-process behind it, how they managed to do it. That's what I'm finding difficult. Designing the program. What is the way to learn about that, if any?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks pretty good. The only thing I would comment on is
Johnn Watsonn wrote:The following code, sets up a pb object reference variable. It's just a object reference variable, it can hold a reference to any object you want it to point to.

All object reference variables have a type. In this case the type is PhoneBook. A reference variable can only point at objects of that type, or one of it's subclasses.
So a variable declared as

can only point at a PhoneBook object (or an object of a subclass if it had any).
If you tried to write something like

you would get a compilation error.

The only way you could have a variable point at any type of object is if you declared it as an Object type. Object is a super class of every other class and so Object references can point at any object.

Johnn Watsonn wrote:Another thing is the Java books I have, and the Java online tutorials, simply teach you the Java language. But not how to think like a Programmer. How to think in an Object-orientated way, if that makes sense. Which is why I seem to be struggling. As I could understand some of the code in programs I've came across, but not how the person came up with the program, how they built it and made it all work together. I look at the code, it makes sense to me, but what doesn't make sense, is the thought-process behind it, how they managed to do it. That's what I'm finding difficult. Designing the program. What is the way to learn about that, if any?

There are a number of books on Object Oriented Design, but really the only way to do it is practice, practice and more practice.
As you learn more, go back to your old programs and try rewriting them using the new techniques you have learnt.
Come back to this post in a couple of months and hopefully everything will make a lot more sense.
Look at other posts on this site - especially in the Beginner's forum and some of the certification forums - and try answering some of the questions that are asked. You don't have to post your answers, but do compare them with any answers that are posted.

Other people may have other suggestions.
 
John R Watson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, that makes sense.

So an object reference variable can not point at objects created from a sibling or parent/super class? Only at objects created from the same class or a child class?

So the only way to learn Java, is by doing as much online tutorials and reading as much as I can about the language? The "thinking as a programmer" will come by itself, as I write more programs?

What is your opinion of the book, "Objects First with Java - A practical introduction using BlueJ".
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't say parent or child, because computer inheritance and biological inheritance are different.
Say superclass and subclass (or what they say in another language) base class and derived class.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't like BlueJ. The book has the advantage that it starts off with objects, but you need to know a bit of programming before you can understand it. Also find out how old it is.
Have you come across Head First Java (HFJ)?
Have you come across our books pages? You will find both those books reviewed there. Most people prefer HFJ, and you can get second‑hand copies at a very reasonable price.
 
John R Watson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Don't say parent or child, because computer inheritance and biological inheritance are different.
Say superclass and subclass (or what they say in another language) base class and derived class.


Do you mean so I don't make the mistake assuming there is two parents that the subclass inherits from, when in Java, there is only one (single inheritance)?


 
John R Watson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Don't like BlueJ. The book has the advantage that it starts off with objects, but you need to know a bit of programming before you can understand it. Also find out how old it is.
Have you come across Head First Java (HFJ)?
Have you come across our books pages? You will find both those books reviewed there. Most people prefer HFJ, and you can get second‑hand copies at a very reasonable price.

Published in 2006.

I didn't like BlueJ, but someone advised me that it will help me understand a lot more about classes & objects. So I will give it a try.

I already have several Java books, which I haven't read much into:

Thinking in Java - The Definitive Introduction to Object Orientated Programming In the language of the World Wide Web - Bruce Eckel

Introduction to Programming in Java - Robert Sedgewick/Kevin Wayne

Beginning Programming with Java for Dummies - Barry Burd

Is Head First a lot better than them? If it is, I will get it. If not, I will start with them.
 
John R Watson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I struggled with that single line, line 124, on this program. Why do I get an error when I try to compile?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John R Watson wrote:


I struggled with that single line, line 124, on this program. Why do I get an error when I try to compile?


The YouBirth variable is a Card reference variable, and hence, doesn't have access to the greeting() method that takes a string.

Now, arguably, it is referring to a YouthBirthday object, that does have such a method. However, the compiler doesn't know that. To make it work, you can either change the reference type to YouthBirthday, or cast it to YouthBirthday prior to calling the greeting(String) method.

Henry
 
John R Watson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
John R Watson wrote:


I struggled with that single line, line 124, on this program. Why do I get an error when I try to compile?


The YouBirth variable is a Card reference variable, and hence, doesn't have access to the greeting() method that takes a string.

Now, arguably, it is referring to a YouthBirthday object, that does have such a method. However, the compiler doesn't know that. To make it work, you can either change the reference type to YouthBirthday, or cast it to YouthBirthday prior to calling the greeting(String) method.

Henry


The compiler doesn't know YouBirth reference variable of type Card, is referring to a YouthBirthday object, because objects are created during run-time? That's why the compiler gives an error if you attempt call a greeting method that isn't part of Card? The compiler assumes that YouBirth reference variable of type Card, is referring to a Card object, so since greeting(String) isn't part of the Card class, it won't allow you to do it? So you tell the compiler, "actually, it's referring to a object of type YouBirthday", and since YouBirthday contains a class with greeting(String), the compiler allows it? I hope I've understood it correctly!

Thanks for your response.
 
John R Watson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure, why in order to override a parents method, you must have the same method signature AND return type? Why the return type too?
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't say parent and child. Say subclass and superclass. There is very little similarity between inheritance in the computer science sense and inheritance in the genetic/biological sense.

In order for a method to override another, it must look exactly like that other method. So it has to have the same signature. And you must be able to use the subclass' (overriding) method wherever the superclass' (overridden) method can be used:-Therefore the two must return the same sort of value.

So until JDK1.4.2 overriding methods had to have exactly the same return type as their overridden counterparts. That was changed in Java5 to covariant return type. That means the subclass method can return a subtype of the superclass method. If you think about the types then whatever type the superclass method returns, whatever the subclass method returns IS‑A “same type instance”.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!