File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Types - Classes and Interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Types - Classes and Interfaces" Watch "Types - Classes and Interfaces" New topic
Author

Types - Classes and Interfaces

Alan Phillips
Ranch Hand

Joined: Aug 13, 2002
Posts: 73
Hi everyone,
I'm really appreciating all the help that I'm getting here at JavaRanch but, I have a question. I'm having some difficulty understanding types and how they relate to interfaces and classes. An example:
define x as an interface type y
define z as a class c
define q as a class d
assign x to class c
assign q to class z
assuming their all classed and accessed properly. Now at the end, what are the types x and q. They are still of the type y and d, but seem to now contain c(x) and q(z). Am I correct in thinking that they are still their original type but now 'contain' all the information of the new types? It seems that casting creates new types, but does x=c and q=z as well or use the original types. Now, Java is the first OO language, so maybe it's just the way I'm thinking about it that is confused!

Thanks alot!
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi Allan:
I'm really appreciating all the help that I'm getting here at JavaRanch but, I have a question. I'm having some difficulty understanding types and how they relate to interfaces and classes.

Sometimes, different resources use same terms in different meaning but I guess it is universal that TYPES and CLASSES are one the same thing. I found that JLS uses term TYPE more often than CLASS.
Regarding your second question: Do you have code for all those definition? It will be much more easier with code to understand different relationships...
Thanks
Barkat
Alan Phillips
Ranch Hand

Joined: Aug 13, 2002
Posts: 73
I think that might be part of my problem too.
Here is a better way to show it:
y x = new y();//interface y defined in the package
c z = new c();//class c defined in the package
d q = new q();//class d defined in the package
x = c;//what type is x, it was an interface y?
q = z;//what type is q, it was a class c?
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
x = c;//what type is x, it was an interface y?
q = z;//what type is q, it was a class c?

You do not have c (variable) defined yet. So:
x = c;
should not compile.
q is going to point to same object as being pointed by z. So q and z are pointing to same object of type c.
Hope this helps.
Barkat
Alan Phillips
Ranch Hand

Joined: Aug 13, 2002
Posts: 73
Sorry, made an error:
y x = new y();//interface y defined in the package
c z = new c();//class c defined in the package
d q = new d();//class d defined in the package
x=z;//what type is x, it was an interface y?
q = z;//what type is q, it was a class c?
Perhaps I'm not understanding the relationship between what the jvm is pointing to and what constitutes an Object. eg: x was an interface type y, now it is a class type c what does the jvm think it is an intertace y or a class type c. q was a class d but now it is a class c?
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi:
I am not sure I am following your questions. But I think following points may help a bit:
1. An interface can not be instantiated. Because all methods in interface are abstract. The interface is a template and a number of classes can implement them. And you can instantiate those classes.
2. The objects created from these classes are refered to by variables such as a, c, z in your example. You can switch the objects refered to by a variable. The variable only remembers the last object it is refereing to. So to answer you question "so what is such and such variable is refereing to now?". All you have to do is look for last assignment to that variable.
Hope this helps.
Barkat
Bishal P
Ranch Hand

Joined: Sep 06, 2002
Posts: 43
Hi Alan,
Quite a confusion here.. so let me try to add some more...
The TYPE of a variable is a property cannot be changed. Once i say that the variable a is dog it can't change itself to cat.
But, since we have hierarchy ( extends, implements and all those complexities) , the variable can point to some more entities too. It can point to any object of the same class (or type), it can refer to any subclass of its type or it can refer to null. Let me not confuse you further and give you what the JLS says
A variable (�4.5) is a storage location. A variable of a primitive type always
holds a value of that exact type. A variable of a class type T can hold a null refer-ence
or a reference to an instance of class T or of any class that is a subclass of T.
A variable of an interface type can hold a null reference or a reference to any
instance of any class that implements the interface.

Remember in Java its all in the reference. The type never changes (until u cast it?), references might change.


_ _____ _ <br />Used to be a Java Programmer but now I work on Microsoft Technologies - Word, Excel and Outlook!
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
If you could follow the Java coding conventions and use Capital letters at the beginning of class and interface names, people will have an easier time reading your examples. Thanks.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Alan Phillips
Ranch Hand

Joined: Aug 13, 2002
Posts: 73
Thanks guys. I read the section in the JLS and it really helped clarify how variables may be of one "type" but reference a different type altogether.
Ron - where do you get the coding convention information? And do you have any suggestions for getting the guys at work to follow one!
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Originally posted by Alan Phillips:
Ron - where do you get the coding convention information? And do you have any suggestions for getting the guys at work to follow one!

You can visit the Chicken Coop...
There is also the Sun website.
[ September 11, 2002: Message edited by: Anthony Villanueva ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Types - Classes and Interfaces
 
Similar Threads
IBM Mock Exam
IBM Mock test
Casting
Qualitiy of OO
Ibm mock questions