This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating a new Object

 
Juan Marcos
Greenhorn
Posts: 12
Eclipse IDE Flex Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy folks!

This is quite a newbie question, but still important.
Can someone please explain to me what the difference between
for example:

AND:


Also, if "MobilePhone" is a subclass of "Phone", what is the reason behind
declaring a new "MobilePhone" object by using the following code:
?
Wouldn't I be doing the same thing if I created the object by:
?

I would still have access to the superclass "Phone" if I declared the object
by using the second example, right?

Thanks!
 
Jason Koonce
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch! You will get a more official welcome soon I am sure, but in the meantime to answer your questions:

When creating a new object you have to tell the JVM what the object is of, so, by not providing a class before blackberry, it doesn't know what rules to follow for that object. As your programs become larger, this is even more important because of the multitudes of classes and objects created.

The second is about Polymorphism...a discussion better left for when you understand objects a little better and ready to learn inheritance. However you have started learning Java, take it one step at a time. You can also use the Java Ranch site here as well. They have several tutorials that will better explain these concepts.

As for having access, yes and no. It all depends on how your code is written. I am sure I will be corrected on this one, but one is an object of the MobilePhone class with an "is-a" relationship to the Phone class, whereas the second is simply an object of the MobilePhone class that may or may not happen to inherit some of the values of Phone.
 
Juan Marcos
Greenhorn
Posts: 12
Eclipse IDE Flex Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Jason, for the welcome and explanation.

I have been recently studying encapsulation, inheritance, polymorphism and also abstract classes by writing code
just to see how the flow of the application works. In regards to OOP, I haven't written anything by instruction just quite yet.

The subject can be so abstract (no pun intended) at times that it seems experience and the right metaphors have seemed to
help my understanding.

I'm sure that it just depends on what I need an application to do when creating my classes and objects.
Trial, error and experience.

You're right, and that makes sense.
 
Jason Koonce
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am normally a figure it out myself type, but I have to admit, a good book on the subject made my understanding happen leaps and bounds.

As for abstract classes, polymorphism and other inheritances, it will all depend on what it is that you want to accomplish. Interfaces, superclasses, and creating your own packages to import can all have their place for what will make your job easier to do and others to understand.

If I can ever help, I will do my best. I am still just learning, but I always find it easier to learn by helping others. Enjoy the ranch, it is a great source!!!
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Juan Marcos wrote:
AND:

The difference between these two is that in the first line, you are doing two things at the same time: you declare a new variable named blackBerry of type Phone, and you initialize it to refer to a new Phone object. In the second line, you're making an existing variable named blackBerry refer to a new Phone object. For the second line to work, you must have declared the variable somewhere else. More examples:

 
Juan Marcos
Greenhorn
Posts: 12
Eclipse IDE Flex Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The difference between these two is that in the first line, you are doing two things at the same time: you declare a new variable named blackBerry of type Phone, and you initialize it to refer to a new Phone object. In the second line, you're making an existing variable named blackBerry refer to a new Phone object. For the second line to work, you must have declared the variable somewhere else. More examples:



Thanks Jesper. That makes perfect sense.

I think where I was stuck was that I couldn't get the word "object" out of my chain of thought.
It all makes sense now. It's a variable that REFERS to an object. Just like any other primitive type.

Thanks!
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Juan Marcos wrote:It all makes sense now. It's a variable that REFERS to an object.

Exactly. It's important to understand the difference between a variable and an object - a variable isn't itself an object, it's something that refers to (or points to) an object. You can have multiple variables that are referring to the same object. I was just explaining this to someone else this morning in this topic.

Juan Marcos wrote:Just like any other primitive type.

For primitive types, it works a little differently than for non-primitive types. For primitive types, the variable directly contains the primitive value (it doesn't refer to a primitive value, like with objects).
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic