My Java OOP example from Lessons

Animal.java:

package com.zakilive;

public 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");
    }

}

Zoo.java

package com.zakilive;

public class Zoo {

    public static void main(String[] args) {
        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();

    }
}

Fish.java

package com.zakilive;

public class Fish extends Animal{

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

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

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 Fly() {
        System.out.println(name+" also Flying...");
    }

}

 

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

Leave a Reply

Your email address will not be published.