0% found this document useful (0 votes)
15 views2 pages

Java Exp 1

The document outlines an experiment in a Computer Science course focused on Java programming. It details the creation of an Animal class with subclasses Dog and Cat, demonstrating inheritance, method overriding, and polymorphism. The provided code showcases how to use an Animal reference to call overridden methods at runtime, enhancing code reusability and modularity.

Uploaded by

Specsy Guy
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)
15 views2 pages

Java Exp 1

The document outlines an experiment in a Computer Science course focused on Java programming. It details the creation of an Animal class with subclasses Dog and Cat, demonstrating inheritance, method overriding, and polymorphism. The provided code showcases how to use an Animal reference to call overridden methods at runtime, enhancing code reusability and modularity.

Uploaded by

Specsy Guy
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
You are on page 1/ 2

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

EXPERIMENT 1
Student Name: Supriya Dutta UID: 23BCS13291
Branch: CSE(2nd Year) Section/Group: Krg-803A
Semester: 4th Date of Performance: 10/02/25
Subject Name: JAVA Subject Code: 23CSP-202

1. AIM:
Create a class Animal with a method makeSound() that outputs a generic sound.The class should
further contain derived classes Dog and Cat that override the makeSound() method to output
specific sounds for each animal. Demonstrate polymorphism by creating an Animal reference that
can hold objects of both Dog and Cat, and call the overridden makeSound() method at runtime.
2. OBJECTIVES:
 To understand inheritance by creating a base class Animal and deriving subclasses Dog and
Cat.
 To implement method overriding by redefining the makeSound() method in the derived
classes to produce specific sounds.
 To demonstrate polymorphism by using an Animal reference to hold objects of Dog and Cat,
ensuring that the appropriate overridden method is called at runtime.
 To explore runtime polymorphism and understand how function calls are dynamically
resolved using base class pointers or references.
 To enhance code reusability and modularity by leveraging polymorphism for flexible and
maintainable code design.

3. CODE:
class Animal {
public void makeSound() {
System.out.println("Some generic animal sound");
}
}
class Dog extends Animal {
@Override
public void makeSound() {
System.out.println("Woof! Woof!");
}
}
class Cat extends Animal {
@Override
public void makeSound() {
System.out.println("Meow! Meow!");
}
}
public class PolymorphismDemo {
public static void main(String[] args) {
Animal animal1 = new Dog();
Animal animal2 = new Cat();

System.out.println("Animal reference holding Dog object:");


animal1.makeSound();

System.out.println("\nAnimal reference holding Cat object:");


animal2.makeSound();

Animal[] animals = {new Dog(), new Cat()};


System.out.println("\nDemonstrating polymorphism in an array:");
for (Animal animal : animals) {
animal.makeSound();
}
}
}
4. OUTPUT:

You might also like