0% found this document useful (0 votes)
10 views

Code

The document defines an Animal class with a makeSound() method that prints "Animal makes a sound". It defines subclasses Dog and Cat that override makeSound() to print "Dog barks" and "Cat meows" respectively. The Main class creates Dog and Cat objects and calls makeSound() on each to demonstrate polymorphism.

Uploaded by

gogulkrishnaece
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Code

The document defines an Animal class with a makeSound() method that prints "Animal makes a sound". It defines subclasses Dog and Cat that override makeSound() to print "Dog barks" and "Cat meows" respectively. The Main class creates Dog and Cat objects and calls makeSound() on each to demonstrate polymorphism.

Uploaded by

gogulkrishnaece
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.util.

*;

class Animal {
void makeound() {
System.out.println("Animal makes a sound");
}
}

class Dog extends Animal {


void makeSound() {
System.out.println("Dog barks");
}
}

class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.makeound();
myDog.makeSound();

}
}
import java.util.*;

class Animal {
void makeound() {
System.out.println("Animal makes a sound");
}
}

class Cat extends Animal {


void Sound() {
System.out.println("Cat meow");
}
}

class Dog extends Cat {


void makeSound() {
System.out.println("Dog barks");
}
}

class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.makeSound();
myDog.Sound();
myDog.makeound();

}
}
import java.util.*;

class Animal {
void makeound() {
System.out.println("Animal makes a sound");
}
}

class Cat extends Animal {


void Sound() {
System.out.println("Cat meow");
}
}

class Dog extends Animal {


void makeSound() {
System.out.println("Dog barks");
}
}

class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.makeSound();
Cat myCat= new Cat();
myCat.Sound();

myDog.makeound();

}
}
import java.util.*;

class Animal {
String species;

Animal(String species) {
this.species = species;
}

void makeSound() {
System.out.println("Animal makes a sound");
}
}

class Dog extends Animal {


String breed;

Dog(String species, String breed) {


super(species);
this.breed = breed;
}

void makeSound() {
System.out.println("Dog barks");
}
}

class Main {
public static void main(String[] args) {
Dog myDog = new Dog("Canine", "Labrador");

System.out.println("Species: " + myDog.species);


System.out.println("Breed: " + myDog.breed);

myDog.makeSound();
}
}

import java.util.*;
class Dog
{
int w;
Dog(int dig)
{
w=dig;
}
}
class HelloWorld extends Dog {
int x;
HelloWorld(int g,int s)
{
super(s);
x=g;
}
void display()
{
System.out.println(x);
System.out.println(super.w);
}
}
class Main
{
public static void main(String[] args) {
HelloWorld h=new HelloWorld(10,100);
h.display();
}
}

You might also like