Interface, Abstract Class and Polymorphism Example

Animal.java class

package com.zakilive;

public abstract class Animal {
    //defining attributes
    String name;
    int weight;
    String gender;

    public Animal(String name, int weight, String gender){   //defining constructors
        this.name=name;
        this.weight=weight;
        this.gender=gender;
    }

    //behaviours

    public void Eat(){
        System.out.println(name+" eat");
    }

    public void Sleep(){
        System.out.println(name+" Sleep");
    }

    public abstract void move(); //You cannot create instance of a abtract class


}

Fish.java

package com.zakilive;

public class Fish extends Animal {

    public Fish(String name, int weight, String gender) {
        super(name, weight, gender);
    }


    public void move() {
        System.out.println("Fish is swimming");
    }

    public void Swim() {
        System.out.println(name + " also Swimming...");
    }


    public static void moveAnimal() {

    }

}

Zoo.java

package com.zakilive;

public class Zoo {

    public static void main(String[] args) {

       Animal sparrow1=new Sparrow("Bird1",9,"Male");
       sparrow1.move();


       Animal fish1=new Fish("Hilsha",99,"Male");
       fish1.move();

        moveAnimals(fish1);
        moveAnimals(sparrow1);



//        Animal animal1 = new Animal("Parrot", 20, "Male");
//        animal1.Eat();
//
//        Bird bird1 = new Bird("Parrot", 20, "male");
//        //bird1.Fly();
//
//        Animal animal2 = new Animal("Salmon", 50, "Female");
//        animal2.Eat();
//
//        Fish fish= new Fish("Salmon", 50, "Female");
//        fish.Swim();
//
//
//
//        Chicken chick1=new Chicken("Murgi",50,"Female");
//        chick1.fly();


    }

    private static void moveAnimals(Animal animal) {
        animal.move();

    }
}

Sparrow.java

package com.zakilive;

public class Sparrow extends Bird implements Flyable {

    public Sparrow(String name, int weight, String gender) {
        super(name, weight, gender);
    }


    public void Fly(){
        System.out.println("Sparrow flying high....");
    }

}

Chicken.java

package com.zakilive;

public class Chicken extends Bird{


    public Chicken(String name, int weight, String gender) {
        super(name, weight, gender);
    }

    public void fly(){
        System.out.println("not able to fly....");
    }

}

Bird.java

package com.zakilive;

public class Bird extends Animal { //now bird is a child of Animal, Bird is a subclass and Animal is base class



    public Bird(String name, int weight, String gender)
    {
        super(name, weight, gender);
    }


    public void move() {
        System.out.println("Flapping wings");
    }


}

Flyable.java

package com.zakilive;

public interface Flyable {

    //interface only has abstract method
    public void Fly(); //interface has no body



}

 

 

 

 

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published.