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

Creating a new Object

 
Greenhorn
Posts: 12
Eclipse IDE Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 will suppress my every urge. But not this shameless plug:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic