• 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

TestAnimals program gives incompatibility error

 
Greenhorn
Posts: 3
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi. I'm a Newbie here. Planning to to give my SCJP. So first step is practicing the study guide.
And here I'm stuck at the TestAnimals in Chapter 2.
____________________________________________________________
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("Generic animal eating"); }}
class Horse { public void eat() { System.out.println("Horse eating"); }}
__________________________________________________________________________
java:4:error:incompatible types
Animal b = new Horse();
^
required : Animal
found : Horse
_____________________________________
Isnt this program supposed to run without errors? Please help. Thanks
 
Sreelu Yeddanapudi
Greenhorn
Posts: 3
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry! I was supposed to be overriding. And I missed saying "Horse extends Animal"
It works now
 
Marshal
Posts: 79263
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done sorting it out, and welcome to the Ranch
 
reply
    Bookmark Topic Watch Topic
  • New Topic