Programming Tutorials

Java program using Method Overriding

By: Paawan Chaudhary in Java Tutorials on 2012-09-19  

This Java program implements Method Overriding for following inheritance : (Assume suitable data )

Abstract Class : Shape
dim1, dim2, disp()
abstract area ()

Class: Rectangle
getd(), area ()

Class: Rectangle
getd(), area ()
import java.lang.*;
import java.io.*;
abstract class Shape
{
	int dim1,dim2;
	void getd()throws IOException
	{
		BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
		System.out.println ("Enter Value of 1st Dimension");
		dim1=Integer.parseInt(br.readLine());
		System.out.println ("Enter Value of 2nd Dimension");
		dim2=Integer.parseInt(br.readLine());
	}
	abstract void area();
}
class Rectangle extends Shape
{
	void getd() throws IOException
	{
		super.getd();
	}
	void area()
	{
		int a=dim1*dim2;
		System.out.println ("Area of Rectangle = "+a);
	}
}
class Triangle extends Shape
{
	void getd() throws IOException
	{
		super.getd();
	}
	void area()
	{
		double b=(1*dim1*dim2)/2;
		System.out.println ("Area of Triangle = "+b);
	}
}
class q11MethodOverriding
{
	public static void main(String args[]) throws IOException
	{
		Rectangle R = new Rectangle();
		R.getd();
		R.area();
		Triangle T = new Triangle();
		T.getd();
		T.area();
	}
}





Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in Java )

Latest Articles (in Java)