• 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

Reference type casting doubt?

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

The following is from Dan chisholm,


The question is on which line will it give a compile time error? Answer given as line 4. Help my why?

Thanks in advance.
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

my answer is:
you could call base.methods() on an obj of type Object.
This wouldn't work, as the Object class does not know about base.methods.

E.G.:
class Base implements I1 {
public void methodA(){}
}

now you want base to be obj, which in turn references to sub.
Doesn't work.
Cast it:
--> base = (Base[]) obj;
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jothi Shankar ,

I think your current problem has nothing to do with all the interfaces and every other thing...

Its as simple as this ::
This will work ->class test {
public static void main(String args[]) {
Object obj = new Object();
test t = new test();
obj = t;
}
}


But this certainly wont ->
class test {

public static void main(String args[]) {

Object obj = new Object();
test t = new test();
t = obj;
}
}


You should know the very basic principle and that spells :: "you can not assign a parent reference to a child class " and Object class is the parent of all classes ,thats it.

I hope this satisfies you...
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi jyoti,
You’ll need typecast explicitely to get your code compile.see below.

interface I1 {} interface I2 {}
class Base implements I1 {}
class Sub extends Base implements I2 {}
class Gray {
public static void main(String []args) {
Base[] base = {new Base()}; // 1
Sub sub[] = {new Sub()}; // 2
Object obj = sub; // 3
base = (Sub[]) obj; /*or base = (Base[]) obj; */ //explicit typecasting
}}

Hope this will help.

Regards
Maneesh Saxena
 
You had your fun. Now it's time to go to jail. Thanks for your help tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic