Home > Java > Java Program to show Abstract Class – Q24

Java Program to show Abstract Class – Q24

January 12, 2009 Leave a comment Go to comments

Q24: Java Program to create four classes figure(abstract), rectangle ,triangle and circle and
define Area method in all the classes.

An abstract class is a class that is declared as “abstract”, it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed. Abstract classes are similar to interfaces. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation. However, with abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods.

abstract class figure{
	int a, b, c;
	abstract void area();
}

class rectangle extends figure{
	rectangle(int _a, int _b){
		a = _a;
		b = _b;
	}
	void area(){
		System.out.println("\n Area : " + a*b);
	}
}

class triangle extends figure{
	triangle(int _a, int _b, int _c){
		a = _a;
		b = _b;
		c = _c;
	}
	void area(){
		double s, ar;
		s = (a+b+c)/2;
		ar = Math.sqrt(s * (s-a) * (s-b) * (s-c));
		System.out.println("\n Area : " + ar);
	}
}

class circle extends figure{
	circle(int _a){
		a = _a;
	}
	void area(){
		System.out.println("\n Area : " + (22/7)*a*a);
	}
}

class fig_area{
	public static void main(String args[]){
		System.out.println("\n Area of Rectangle:-");
		rectangle Or = new rectangle(10, 5);
		Or.area();
		
		System.out.println("\n Area of Triangle:-");
		triangle Ot = new triangle(5, 10, 15);
		Ot.area();
		
		System.out.println("\n Area of Circle:-");
		circle Oc = new circle(10);
		Oc.area();
	}
}

… from College notes (BCA/MCA assignments):


Advertisement
Categories: Java Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: