• 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

casting

 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could anyone explain why the below code results in an error:
File: junktest.java
class junktest {
public static void main(String args []) {
junk j1 = new junk();
junksub js;
System.out.print("inside junktest");
js = (junksub)j1;
}
}
File: junk.java
class junk {
public static void main(String args []) {
System.out.println("This is junk");
}
}
File: junksub.java
class junksub extends junk {
public static void main(String args []) {
System.out.println("This is junksub");
}
}
Thank you in advance!
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For starters, main is not a method that you put into all classes you create. I would remove it from class junk and junksub altogether.
Then, you can't make class junk a junksub. All instances of junksub are junk, but not all instances of junk are junksub. Does that make sense?
 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's right, you can upcast no problem, but not downcast (class-wise). Also, I beleive that whatever class you decide to keep your one main() method in has to be declared as public.
Someone correct me if I am wrong.
------------------
  • Ryan Burgdorfer
  • Java Acolyte in
  • Columbus, OH USA
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They can't be protected or private, but they don't need to be explicitly public either.

This works, so the default (or package) access is allowed as well as public.
 
Charlie Swanson
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still do not understand why am I getting an error when I do an explicit cast down.
Eventhough I have the main declared in each class it is not causing the casting error.
Thank you,
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Charlie:
You cannot force an object of junk()to be an object of junksub() even with an explicit cast.
For example:
Create a class Animal
Create class Dog extends Animal
You cannot forcibly make any Animal a Dog by explicit casting
But a Dog is naturally an Animal (defined in the class hierarchy)
I rewrote your code using the example



Please use code tags when you post code http://www.javaranch.com/ubb/ubbcode.html
regards,
Jyotsna
 
Charlie Swanson
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I am confused on this.
From my books, I know that an explicit cast is required when going from a superclass to a subclass. Can anyone explain how or when this can be done?
 
ryan burgdorfer
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It can be done only when the reference you are casting actually IS an object of the subclass type. No other case will work.
------------------
  • Ryan Burgdorfer
  • Java Acolyte in
  • Columbus, OH USA
 
Kathy Lynch
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suggest that you read "How my dog learned Polymorphism" in the Campfire section of the JavaRanch. It discusses these concepts really well (at least it really helped me).

[This message has been edited by Kathy Lynch (edited April 06, 2001).]
 
Charlie Swanson
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for the explanations.
 
You will always be treated with dignity. Now, strip naked, get on the probulator and hold this 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