Exp 5 Java
Exp 5 Java
Experiment:.2.1
Objective:
Using inheritance, one class can acquire the properties of others. Consider the
classAnimal{
voidwalk(){
System.out.println("I am walking");
}
}
This class has only one method, walk. Next, we want to create a Bird class that also has
classBirdextendsAnimal{
voidfly(){
System.out.println("I am flying");
}
}
Finally, we can create a Bird object that can both fly and walk.
publicclassSolution{
publicstaticvoidmain(String[]args){
Birdbird=newBird();
bird.walk();
bird.fly();
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
}
}
The above code will print:
I am walking
I am flying
This means that a Bird object has all the properties that an Animal object has, as well as
The code above is provided for you in your editor. You must add a sing method to
the Bird class, then modify the main method accordingly so that the code prints the
following lines:
I am walking
I am flying
I am singing
ProgramCode:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
class Animal{
void walk(){
System.out.println("I am walking");
}
}
}
}
Output:
3.I learned how to call a function by creating the object of its class.