Java Program to Create a class named ‘Animal’ which includes methods like eat() and sleep().

Create a class named ‘Animal’ which includes methods like eat() and sleep().

Create a child class of Animal named ‘Bird’ and override the parent class methods. Add a new method named fly().

Create an instance of Animal class and invoke the eat and sleep methods using this object.

Create an instance of Bird class and invoke the eat, sleep and fly methods using this object.




Source Code:

1:  package Inheritance.Assignment01;  
2:  class Animal {  
3:       void eat() {  
4:            System.out.println("Animal is eating");  
5:       }  
6:       void sleep() {  
7:            System.out.println("Animal is sleeping");  
8:       }  
9:  }  
10:  class Bird extends Animal {  
11:       @Override  
12:       void eat() {  
13:            System.out.println("Bird is eating");  
14:       }  
15:       @Override  
16:       void sleep() {  
17:            System.out.println("Bird is sleeping");  
18:       }  
19:       void fly() {  
20:            System.out.println("Bird is flying");  
21:       }  
22:  }  
23:  public class Solution {  
24:       public static void main(String[] args) {  
25:            // TODO Auto-generated method stub  
26:            Animal animal = new Animal();  
27:            animal.eat();  
28:            animal.sleep();  
29:            Bird bird = new Bird();  
30:            bird.eat();  
31:            bird.sleep();  
32:            bird.fly();  
33:       }  
34:  }