Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Objects

 
Chris Stewart
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope this is the correct place for this question/discussion.
Can anyone help me understand the concept of creating a new object and such? Example, when you code

Are you creating a new object of ClassName that "inherits" ClassNames' variables and methods? Where is the powerfulness in doing this kind of creation with objects? Thanks in advance.
 
Col Bell
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is one of those questions that isn't easy to answer meaningfully in just a few sentences. I would suggest going through the javasoft Java Tutorial http://java.sun.com/docs/books/tutorial/getStarted/index.html
A good book is Bruce Eckels "Thinking in java". This book doesn't just try to teach you java syntax but good design techniques for java as well. You can downaload a HTML version from here http://www.mindview.net/Books/TIJ/
 
Chris Stewart
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So would that code I posted create a new object?
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ClassName c = new ClassName();
In this statement you are creating (by declaring it) a variable on the left hand side. The name of the variable is c. It has a "type" of ClassName - which means that only things which can "prove" that they provide the same variables/methods as the class ClassName has can be referenced by this variable without a compiler error.
On the right hand side you are instantiating the variable c. You are creating an object on the heap that will hold the "state" of the object (the current value of each of its varables) and putting a reference to it in the variable c. Notice that the "new" word is used to do the object creating. The "ClassName()" is a call to the constructor of the class ClassName. In this case the parenthesis do not hold any parameters so you are calling the default constructor which takes no parameters.
The variable in this statement is a different thing than the object in this statement.
Variables have a "type", objects have a "class".
You can create a variable that is the "type" of an interface, and any object of any class that implements that interface can be referenced by that variable. However you can not actually create an object which is the "class" of an interface - because of course an interface is NOT a fully defined class.
Map myMap = new HashMap();
The variable is of "type" Map. So since this line compiles, the thing that it is referencing must be either a class that implements Map, or a Sublass of Map (since it is obviously NOT the "same" type). You can't really tell which just by looking at this code. As it turns out Map is an interface, so HashMap must implement that interface.
 
Ronald Schindler
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In essence you are correct. c will becomes an object of type ClassName.
Because c is of type ClassName it has all the required variables and methods for ClassName.
As for the "powerfulness": Seems to me that the best way to understand this type of programming is to try doing it without using an object.

Are you creating a new object of ClassName that "inherits" ClassNames' variables and methods? Where is the powerfulness in doing this kind of creation with objects? Thanks in advance. [/QB]

Hope that helps.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are creating an object of class ClassName, and it will have all the fields (member variables)and methods() of ClassName.
The REASON for creating an object is to track the status (current values) of "this" instance vs "that" instance. That is kept in the values of the variables of the instance.
[ March 25, 2002: Message edited by: Cindy Glass ]
 
Chris Stewart
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic