JAVA object-oriented inheritance and polymorphism simple understanding and examples

Program requirements

1. Write a Cat class and implement a shout() method, which can output the kitten's meow

2. Modify the above program to declare a field name and a constructor with parameters for Cat class, so that the name of the object can be set when instantiating the object; It also enables the shot () method to output the name of the object according to the value of name, such as "my name is... Meow", and implements the default constructor.

3. Modify the above program, design a parameterized constructor and call it.

class Cat {
    private final String name;

    public Cat(String name) {
        this.name = name;
        cnt++;
    }

    public void shout() {
        System.out.println("My name is" + name + " Meow Meow Meow");
    }

}

4. Modify the above program, use the private field and get/set method to realize the function of controlling the cat's call, make it call several times, and output the call, but call 10 times at most.

5. Modify the above program, design a static variable and static method, count and display the number of generated instances.

class Cat {
    static int cnt=0;
    private int n;
    private final String name;

    public Cat(String name) {
        this.name = name;
        cnt++;
    }
    public void shout() {//Cat barking method
        System.out.println("My name is" + name);
        for (int i = 0; i < Math.min(n,10); i++) {

            System.out.print("Meow ");
        }
    }

    public void setN(int n) {//Set the number of cat calls
        this.n = n;
    }

    public int getCnt() {//Returns the number of instances
        return cnt;
    }
}

6. On the basis of the program implemented in the last class, a Dog class is added following the Cat class, and there is also a shot () method, which can output "my name is... Wang! Wang! " And other functions.

class Dog {
    static int cnt=0;
    private int n;
    private final String name;

    public Dog(String name) {
        this.name = name;
        cnt++;
    }

    public void shout() {//Dog barking method
        System.out.println("My name is" + name);
        for (int i = 0; i < Math.min(n,10); i++) {

            System.out.print("Woof ");
        }
    }

    public void setN(int n) {//Set the number of dogs barking
        this.n = n;
    }

    public int getCnt() {//Returns the number of instances
        return cnt;
    }
}

7. By analyzing Cat and Dog programs, we can find that they have very similar codes. Please create a parent Animal class and try to put the same code of Cat and Dog into the Animal class.

8. By analyzing the above procedures, it can be found that Cat and Dog classes have shout() methods, but the sound is different. Please modify the above program to create a shot () method for the Animal class, and then let Cat and Dog rewrite the shot () method to realize polymorphism.

9. On the basis of the above procedures, add two animals, cattle and sheep, and call their own voices.

class Animal {
    static int cnt = 0;
    public int n;
    public final String name;

    public Animal(String name) {
        this.name = name;
        cnt++;
    }
    public void shout(){
        
    }

    public void setN(int n) {//Set the number of dogs barking
        this.n = n;
    }

    public int getCnt() {//Returns the number of instances
        return cnt;
    }
}

class Cat extends Animal {
    public Cat(String name) {
        super(name);
    }

    public void shout() {
        System.out.println("My name is" + name);
        for (int i = 0; i < Math.min(n, 10); i++) {

            System.out.print("Meow ");
        }
    }
}

class Dog extends Animal {
    public Dog(String name) {
        super(name);
    }

    public void shout() {
        System.out.println("My name is" + name);
        for (int i = 0; i < Math.min(n, 10); i++) {

            System.out.print("Woof ");
        }
    }
}

class Cow extends Animal {
    public Cow(String name) {
        super(name);
    }

    public void shout() {
        System.out.println("My name is" + name);
        for (int i = 0; i < Math.min(n, 10); i++) {

            System.out.print("Mou ");
        }
    }
}

class Goat extends Animal {
    public Goat(String name) {
        super(name);
    }

    public void shout() {
        System.out.println("My name is" + name);
        for (int i = 0; i < Math.min(n, 10); i++) {

            System.out.print("Mie");
        }
    }
}

10. By analyzing the above procedures, it can be found that the shout() methods of the four animals are almost the same except that the sounds of animals are different. Please implement this method in the Animal class, but due to the different sounds of various animals, you need to add a method getSound() to obtain the sounds of various animals in the subclass. After refactoring, all repetitions are placed in the parent class, and the child class becomes very concise and easy to modify.

11. Since the Animal class cannot be instantiated at all and does not need to be instantiated, you can change the parent class without any meaning of instantiation into an abstract class. On the basis of the above program, change the Animal class to an abstract class, and getSound() should also be changed to an abstract method.

abstract class Animal {
    static int cnt = 0;
    public int n;
    public final String name;
    public String Sound;

    public Animal(String name) {
        this.name = name;
        cnt++;
    }
     public void shout(){
        for (int i = 0; i < Math.min(n, 10); i++) {

            System.out.print(Sound+' ');
        }
    }
    abstract public void getSound();

    public void setN(int n) {//Set the number of animal calls
        this.n = n;
    }
    public int getCnt() {//Returns the number of instances
        return cnt;
    }
}

class Cat extends Animal {
    public Cat(String name) {
        super(name);
    }
    public void getSound(){
        Sound="Meow";
    }

}

class Dog extends Animal {
    public Dog(String name) {
        super(name);
    }
    public void getSound(){
        Sound="Woof";
    }

}

class Cow extends Animal {
    public Cow(String name) {
        super(name);
    }
    public void getSound(){
        Sound="Mou";
    }

}

class Goat extends Animal {
    public Goat(String name) {
        super(name);
    }
    public void getSound(){
        Sound="Mie";
    }
}

11. Among the four animals, Cat and Dog can catch mice. Please design this function as an interface, which is inherited and realized by Cat and Dog respectively.

interface CatchMouse{
    public void Catch();
}

abstract class Animal{
    static int cnt = 0;
    public int n;
    public final String name;
    public String Sound;

    public Animal(String name) {
        this.name = name;
        cnt++;
    }
     public void shout(){
        for (int i = 0; i < Math.min(n, 10); i++) {

            System.out.print(Sound+' ');
        }
    }
    abstract public void getSound();

    public void setN(int n) {//Set the number of dogs barking
        this.n = n;
    }
    public int getCnt() {//Returns the number of instances
        return cnt;
    }
}

class Cat extends Animal implements CatchMouse{
    public Cat(String name) {
        super(name);
    }
    public void getSound(){
        Sound="Meow";
    }
    public void Catch(){
        System.out.println(name+"as a cat caught a mouse");
    }

}

class Dog extends Animal implements CatchMouse{
    public Dog(String name) {
        super(name);
    }
    public void getSound(){
        Sound="Woof";
    }
    public void Catch(){
        System.out.println(name+"as a dog caught a mouse");
    }

}

class Cow extends Animal {
    public Cow(String name) {
        super(name);
    }
    public void getSound(){
        Sound="Mou";
    }
}

class Goat extends Animal {
    public Goat(String name) {
        super(name);
    }
    public void getSound(){
        Sound="Mie";
    }
}


UML class diagram

Keywords: Java OOP Polymorphism inheritance

Added by FarhanKhalaf on Tue, 07 Sep 2021 08:56:19 +0300