Practice for inheritance

Practice for inheritance

  1. Create a class circle and use inheritance to create another class cylinder from it
  2. 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) 
  3. Create a method for area and volume in 1 
  4. create methods for area & volume in 2 and also create getters and setters 
  5. 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

Popular posts from this blog

Creating code for Rock Paper Scissors:

Practice Set For Basic OOPs Concept: