Cat and Dog examples

Revision as of 12:37, 25 November 2026 by Cadrgtsecond (talk | contribs) (Created page with "A Cat and Dog example is an artificial example often inspired by real life objects used to teach programming, particularly Software Architecture, and particularly Object Oriented Programming <pre> // Parent class class Animal{ void sound(){ System.out.println("Animal makes a sound"); } } // Child class class Dog extends Animal{ @Override void sound(){ System.out.println("Dog barks"); } } // Child class class Cat extends Animal{...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

A Cat and Dog example is an artificial example often inspired by real life objects used to teach programming, particularly Software Architecture, and particularly Object Oriented Programming

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

// Child class
class Dog extends Animal{
    @Override
    void sound(){
        System.out.println("Dog barks");
    }
}

// Child class
class Cat extends Animal{
    @Override
    void sound(){
        System.out.println("Cat meows");
    }
}

// Driver class
public class Driver {
    public static void main(String[] args){
        Animal a = new Dog();
        a.sound();
        a = new Cat();
        a.sound();
    }
}

Other common examples include

  • Various kinds of shapes
  • Cars and Vehicles

Of course, while these appear to be "real life examples", they are completely silly and nothing like what we actually do in programming. And even in these examples, slight examination will lead one to discover members of the hierarchy such as LandVehicleWithTwoWeels, ShapeWithCorners and AnimalsWithFurAndWarmBloodButWhichDoNotGiveLiveBirth. Complex inheritance hierarchies do not survive the complexities of real life.