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

Types - Classes and Interfaces

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Alan Phillips
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic