public class Animal {
   public void eat() { 
      System.out.println("I eat like a generic Animal."); 
   }
 
   public static void main(String[] args) {
   }
}
 
class Fish extends Animal {
   @Override
   public void eat() { 
      System.out.println("I eat like a fish!"); 
   }
}
 
class Goldfish extends Fish {
   @Override
   public void eat() { 
      System.out.println("I eat like a goldfish!"); 
   }
}

In this example how to call the 'eat' method of the Animal class using an object of class GoldFish?

GoldFish gfobj = new GoldFish();
   gfobj.eat();//i'm expecting this line to call the eat method in the Animal class

however i'm aware of

Animal aobj = new Fish();
   aobj.eat();//this will print "I eat like a fish!"

Edit: Sorry I didn't look the code and what I wrote was not very accurate. This is the code I suggest:

public class Animal {
   public void eat() { 
      System.out.println("I eat like a generic Animal."); 
   }
 
   public static void main(String[] args) {
   }
}
 
class Fish extends Animal {
   @Override
   public void eat() { 
      System.out.println("I eat like a fish!"); 
   }

  public void eat2() { 
      super.eat();
   }
}
 
class Goldfish extends Fish {
   @Override
   public void eat() { 
      System.out.println("I eat like a goldfish!"); 
   }

   public void eat2() { 
      super.eat2();
   }
}

When GoldFish will call eat2, it will call the super eat2: Fish.eat2. Then Fish.eat2 will call the super eat: Animal.eat.

Try to copy the code and check it out. I didn't test it, but it is up to you to experiment. That is one way to learn.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.