Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Object Oriented Pet System

The document defines an interface 'Pet' and an abstract class 'Animal' with properties and methods for animal behavior. It implements two concrete classes, 'Dog' and 'Cat', which extend 'Animal' and implement 'Pet', providing specific behaviors for moving and speaking. The 'Main' class demonstrates the usage of these classes by creating instances of 'Dog' and 'Cat' and invoking their methods.

Uploaded by

nishalls6415
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)
11 views2 pages

Object Oriented Pet System

The document defines an interface 'Pet' and an abstract class 'Animal' with properties and methods for animal behavior. It implements two concrete classes, 'Dog' and 'Cat', which extend 'Animal' and implement 'Pet', providing specific behaviors for moving and speaking. The 'Main' class demonstrates the usage of these classes by creating instances of 'Dog' and 'Cat' and invoking their methods.

Uploaded by

nishalls6415
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

Pet.

java

public interface Pet {


void speak();
String getName();
}

Animal.java

public abstract class Animal {


private String name;
private int age;

public Animal(String name, int age) {


this.name = name;
this.age = age;
}

public abstract void move();

public String getName() {


return name;
}

public int getAge() {


return age;
}
}

Dog.java

public class Dog extends Animal implements Pet {


public Dog(String name, int age) {
super(name, age);
}

@Override
public void move() {
System.out.println(getName() + " runs on four legs.");
}

@Override
public void speak() {
System.out.println(getName() + " says: Woof!");
}
}

Cat.java

public class Cat extends Animal implements Pet {


public Cat(String name, int age) {
super(name, age);
}
@Override
public void move() {
System.out.println(getName() + " gracefully walks on four legs.");
}

@Override
public void speak() {
System.out.println(getName() + " says: Meow!");
}
}

Main.java

public class Main {


public static void main(String[] args) {
Pet dog = new Dog("Buddy", 4);
Pet cat = new Cat("Whiskers", 2);

dog.speak();
((Animal) dog).move();

cat.speak();
((Animal) cat).move();
}
}

You might also like