Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Question about difference between object type and reference type

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is  Question ID: 908907 of Kaplan





I don't understand the difference between object type and reference type in lines

 
Rancher
Posts: 1170
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's try a more clear example:

The refference type is Car, but the object type is Mercedes.
this wil only work of course if the object type extends the refference type.
an other example:
 
Marshal
Posts: 69373
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are using the wrong names. If you look in the Java® Language Specification (=JLS), you will find out that the opposite of reference type is primitive type.

I think you meant to say declared type and runtime type.Here the declared type and runtime type are both the same: Car. I am not sure I would have a class Meredes, but you might have a class like this:-Now you can sayor even one suitable for after boozy Christmas parties‍ In all those cases, the declared type is Car, but the runtime type might be a Taxi; The last code snippet doesn't allow anybody to decide what type it is until runtime.
The javac tool tests the declared type of all references, and ensures that all objects assigned thereto are of that type (that type includes subtypes). So you can assign a Car or a Taxi to a reference declared Car. You can assign a Square, Rectangle or Rhombus to a Shape declared type. That means only methods declared in the types Car/Shape can be used. You cannot therefore writebecause there is no such method in the Car class. You can however cast the Car reference, and that will make me pay the £13.50, but I shall leave you to work out what might go wrong with this sort of code:-Look at line 23 in the code you posted.
I shall also let you work out why I have used the BigDecimal(double) constructor.
 
Campbell Ritchie
Marshal
Posts: 69373
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also work out the inheritance hierarchy in that code; it has a serious error in.
 
And inside of my fortune cookie was this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic