• Post Reply Bookmark Topic Watch Topic
  • New Topic

TestAnimals program gives incompatibility error  RSS feed

 
Sreelu Yeddanapudi
Greenhorn
Posts: 3
Java
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Sorry! I was supposed to be overriding. And I missed saying "Horse extends Animal"
It works now
 
Campbell Ritchie
Marshal
Posts: 56522
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done sorting it out, and welcome to the Ranch
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!