0% found this document useful (0 votes)
3 views1 page

Overloading and Overriding in Java

The document contains Java code defining a class hierarchy with Animal, Dog, and Puppy classes. The Puppy class overrides the eat method and includes an overloaded eat method that takes a string parameter. The main method demonstrates polymorphism by creating a Puppy object referenced as an Animal and calls its methods.

Uploaded by

Sebin Mathew
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views1 page

Overloading and Overriding in Java

The document contains Java code defining a class hierarchy with Animal, Dog, and Puppy classes. The Puppy class overrides the eat method and includes an overloaded eat method that takes a string parameter. The main method demonstrates polymorphism by creating a Puppy object referenced as an Animal and calls its methods.

Uploaded by

Sebin Mathew
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

class Animal {

void eat() { [Link]("The animal eats food"); }

class Dog extends Animal {

void bark() { [Link]("Dog barks"); }

class Puppy extends Dog {

void eat() { [Link]("I am a puppy"); }

void eat(String food) { [Link]("Puppy eats " + food); }

public class classlass {

public static void main(String[] args) {

Animal a = new Puppy();

[Link]();

Puppy p = new Puppy();

[Link]();

[Link]("biscuits");

[Link]();

You might also like