Practice for inheritance
Practice for inheritance
- Create a class circle and use inheritance to create another class cylinder from it
- Create a class rectangle and use inheritance to create another class cuboid, try to keep it as close to the real-world scenario as possible (for your practice)
- Create a method for area and volume in 1
- create methods for area & volume in 2 and also create getters and setters
- What is the order of constructor execution for the following inheritance hierarchy
Code:
package Com.Suryawanshi;
class Circle{
int radius;
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
this.radius = radius;
}
Circle(){
System.out.println("I am Non Parametrized Constructor");
}
Circle(int r){
this.radius=r;
}
public double circleArea(){
return Math.PI*this.radius*this.radius;
}
}
class Cylender extends Circle{
int height;
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
Cylender(int r, int h){
super(r);
this.height=h;
}
public double cylenderVolume(){
return Math.PI*this.radius*this.radius*this.height;
}
}
public class Inheritance1 {
public static void main(String[] args) {
Cylender volume = new Cylender(12,5);
System.out.println("Area of Circle is "+volume.circleArea());
System.out.println("volume of Circle is "+volume.cylenderVolume());
}
}
Comments
Post a Comment