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

pr8 Saksham

dbatu ds o/p pr8

Uploaded by

Ritesh Doibale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

pr8 Saksham

dbatu ds o/p pr8

Uploaded by

Ritesh Doibale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Name :Saksham Cheulwar Roll No : 275

Experiment no 8

Program :
// Base class
class Animal {
// Method to be overridden
public void sound() {
System.out.println("Animal makes a sound");
}
}
// Subclass 1
class Dog extends Animal {
// Overriding the sound method
@Override
public void sound() {
System.out.println("Dog barks");
}
}

// Subclass 2
class Cat extends Animal {
// Overriding the sound method
@Override
public void sound() {
System.out.println("Cat meows");
}
}
// Subclass 3
class Cow extends Animal {
// Overriding the sound method
@Override
public void sound() {
System.out.println("Cow moos");
}
}

// Main class to demonstrate polymorphism


public class PolymorphismExample {
public static void main(String[] args) {
// Creating objects of subclasses
Animal myDog = new Dog();
Animal myCat = new Cat();
Animal myCow = new Cow();

// Calling the overridden methods


myDog.sound(); // Outputs: Dog barks
myCat.sound(); // Outputs: Cat meows
myCow.sound(); // Outputs: Cow moos
}
}
OUTPUT :
Dog barks
Cat meows
Cow moos

You might also like