• 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

K&B 1.4 - Overloading / Overriding - Page 302

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I typed out the example given on page 302. You can copy and paste what I've typed below. So anyway I wanted to better understand the concepts but the code gives an error on the line "Animal b = new Horse();".

Anyone can enlighten me on this? Here's the code:
___________________________________________________________________________

public class TestAnimals
{
public static void main(String[] args)
{
Animal a = new Animal();
Animal b = new Horse();

a.eat();
b.eat();
}
}

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

class Horse
{
public void eat()
{
System.out.println("Horse eating.");
}
}
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Should the declaration of Horse extend Animal?
 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shanel Jacob:
Hi all,

I typed out the example given on page 302. You can copy and paste what I've typed below. So anyway I wanted to better understand the concepts but the code gives an error on the line "Animal b = new Horse();".

Anyone can enlighten me on this? Here's the code:
___________________________________________________________________________

public class TestAnimals
{
public static void main(String[] args)
{
Animal a = new Animal();
Animal b = new Horse();

a.eat();
b.eat();
}
}

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

class Horse
{
public void eat()
{
System.out.println("Horse eating.");
}
}




I don't know what you really want to understand.

But I can tell you the logic is what you are doing in this example is You are assigning reference of Animal Class to Horse Class which are no more in any sort of relationships.
SO probably the error you might be facing is

"Type mismatch: cannot convert from Horse to Animal"



So this is the problem What I will suggest you design below given code in which Horse Class will extend the Animal Class and then try this will not give the error.




Hope it helps . If still any concern then clearly write the confusion or problem.

 
Shanel Jacob
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Keith, Yes I forgot to typed in "Horse extends Animal".

Ankur, what I wanted to understand on page 302 was how overriding depends on OBJECT type.

I just wanted to compare that with the code on page 308 which shows that when it comes to overloading, its the REFERENCE type that matters.

I found that typing in some short code and seeing how they work on screen makes it easier for me to remember that's all.

Thanks all =)
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic