Generics and Inheritance
// Generics and Inheritance
class Rectangle<T extends Number>
{
T l, b;
public Rectangle(T x, T y)
{
l = x;
b = y;
}
void fnArea()
{
System.out.println("Area = "+(l.doubleValue()*b.doubleValue()));
}
}
class Box<T extends Number> extends Rectangle<T>
{
T h;
public Box(T x, T y, T z)
{
super(x, y);
h = z;
}
void fnVolume()
{
System.out.println("Volume = "+(l.doubleValue()*b.doubleValue()*h.doubleValue()));
}
}
class Generics_Demo
{
public static void main(String as[])
{
Box<Integer> obj1 = new Box<Integer>(5,6,2);
obj1.fnArea();
obj1.fnVolume();
Box<Double> obj2 = new Box<Double>(2.1,4.3,6.5);
obj2.fnArea();
obj2.fnVolume();
}
}
// Sample Output
>javac Generics_Demo.java
>java Generics_Demo
Area = 30.0
Volume = 60.0
Area = 9.03
Volume = 58.7
>
No comments:
Post a Comment
Don't be a silent reader...
Leave your comments...
Anu