Results 1 to 7 of 7

Thread: Java Basic

  1. #1
    Anveshan is offline Member
    Join Date
    May 2011
    Posts
    24
    Rep Power
    0

    Question Java Basic

    Hi
    Please bear(if you feel it is silly) my question nad help in understanding the concept. I have the following code.

    class Animal
    {
    void eat()
    {
    System.out.println("Animal eat");
    }
    }

    class Dog extends Animal
    {
    void eat()
    {
    System.out.println("Dog eat");
    }
    }

    When i say
    Dog dog = (Dog) new Animal();
    I get a run time(class cast exception) exception. Why is this not caught by compiler ? After all, Animal instance doesn't pass "is A" relationship with Dog(though Dog instance passes "is A" relationship with Animal and hence Animal aniDog = new Dog() works). I am still in infancy stages in java. Guys help me.

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    Hi.
    Operations cast type implement in run time. hence compiler can not check this mistake in compile time.
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    Well, what you are getting confused with is actually called upcasting and downcasting in Java.

    Here are a couple of links that explain these concepts in really good manner. Have a look at them,

    Upcasting Downcasting

    Casting Reference Variables

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  4. #4
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    I dont see why you even have a a parent class for this, considering you're overriding its only method. You would be better off having Animal as an interface.

    That aside, change it to:

    Java Code:
    Animal dog = new Dog();
    Last edited by Solarsonic; 05-04-2011 at 01:36 PM.

  5. #5
    steven.ye is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default

    as to reference type casting, such as class casting, JAVA compiler doesn't check it at compile time, it assumes that you know the casting is safe as you did so, so even you cast the Dog object to an String type, it also will not complaint. but at runtime the casting causes an error, because their memories are different. Hope to help

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by steven.ye View Post
    as to reference type casting, such as class casting, JAVA compiler doesn't check it at compile time, it assumes that you know the casting is safe as you did so, so even you cast the Dog object to an String type, it also will not complaint. but at runtime the casting causes an error, because their memories are different. Hope to help
    The compiler checks up-casts and it leaves down-casts to the runtime.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Anveshan is offline Member
    Join Date
    May 2011
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by goldest View Post
    Well, what you are getting confused with is actually called upcasting and downcasting in Java.

    Here are a couple of links that explain these concepts in really good manner. Have a look at them,

    Upcasting Downcasting

    Casting Reference Variables

    Hope that helps,

    Goldest
    Thanka a million Goldest !!! Very helpful !

Similar Threads

  1. Basic Java help, AIM?
    By jkswebsite in forum New To Java
    Replies: 4
    Last Post: 07-11-2012, 07:17 PM
  2. Basic java help =)
    By Xycose in forum New To Java
    Replies: 1
    Last Post: 06-30-2010, 03:20 AM
  3. Basic java
    By santa in forum New To Java
    Replies: 5
    Last Post: 11-16-2009, 11:16 AM
  4. basic java
    By vijay24805 in forum New To Java
    Replies: 25
    Last Post: 04-14-2009, 03:46 AM
  5. basic java help
    By adred in forum New To Java
    Replies: 0
    Last Post: 03-08-2008, 01:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •