Java Program for Hierarchical Inheritance

Write a Java program for hierachical inheritance. Write a Java program to the following hierachical inheritance. _______________________ | abstract class shape| |_____________________| | abstract area( ) | |_____________________| ___________|________________ | | ____________|______________ ___________|__________ | class rectangle | | class triagle | |__________________________| |_____________________| | length, breadth, area( ) | | side1, side2, area( ) | |__________________________| |_____________________|.



Source Code
abstract class Shape2 {
	
	abstract void area();   
}

class Rectangle2 extends Shape2 {
	
	int length, breadth;
		Rectangle2(int l, int b) {
		
		length = l;
		breadth = b;
	}
	
	public void area() {
		
		System.out.println("Area of Rectangle : "+ (length * breadth));
	}
}

class Triangle2 extends Shape2 {
	
	int side1, side2;
	Triangle2(int s1, int s2) {
		
		side1 = s1;
		side2 = s2;
	}
	
	public void area() {
		
		System.out.println("Area of Triangle : "+(0.5 * side1 * side2));
	}
}

public class HierarchicalInh {

	public static void main(String[] args) {
		
		Rectangle2 r = new Rectangle2(23,19);
		Triangle2 t = new Triangle2(15,6);
		Shape2 s = r;
		s.area();
		s = t;
		s.area();
	}
}
Output
Area of Rectangle : 437
Area of Triangle : 45.0