Abstract class:
Syntax:
AccessSpecifier abstract class ClassName {
//data members, abstract methods and non-abstract methods
}
Example: Create an abstract class Shape which have two abstract methods area() and perimeter() and one non-abstract method display() then create 6 classes Circle, Triangle, Rectangle, Square, Cuboid and Cube all will extends Shape abstract class?
Shape.java
Circle.java
Triangle.java
Rectangle.java
Square.java
Cuboid.java
Cube.java
Test.java
Output:
Display method of abstract class Shape- Any class that have at least one or more methods are abstract then that class must be declare as abstract class.
- A class with abstract keyword is called abstract class.
- It can have both abstract and non-abstract methods
- It can not be instantiated means object of abstract class can not be created
- It can have Constructor
- Implementation of abstract methods should be provided by sub-class that inherited the abstract class
- Create abstract class using abstract keyword
- Make abstract class where you want to provide the implementation of abstract methods in the sub-class
- To restrict the class to have certain methods
Syntax:
AccessSpecifier abstract class ClassName {
//data members, abstract methods and non-abstract methods
}
Example: Create an abstract class Shape which have two abstract methods area() and perimeter() and one non-abstract method display() then create 6 classes Circle, Triangle, Rectangle, Square, Cuboid and Cube all will extends Shape abstract class?
Shape.java
package com.java.abst;
public abstract class Shape {
abstract double area();
abstract double perimeter();
public void display(){
System.out.println("Display method of abstract class Shape");
}
}
public abstract class Shape {
abstract double area();
abstract double perimeter();
public void display(){
System.out.println("Display method of abstract class Shape");
}
}
Circle.java
package com.java.abst;
public class Circle extends Shape{
double radius;
public Circle(){
radius = 1.0;
}
public Circle(double r){
radius = r;
}
double area() {
return 22.0 / 7.0 * radius * radius;
}
double perimeter() {
return 2.0 * 22.0 / 7.0 * radius ;
}
}
public class Circle extends Shape{
double radius;
public Circle(){
radius = 1.0;
}
public Circle(double r){
radius = r;
}
double area() {
return 22.0 / 7.0 * radius * radius;
}
double perimeter() {
return 2.0 * 22.0 / 7.0 * radius ;
}
}
Triangle.java
package com.java.abst;
public class Triangle extends Shape {
double a, b, c, s;
public Triangle(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
s = perimeter()/2;
}
double area() {
return Math.sqrt(s*(s-a)*(s-b)*(s-c));
}
double perimeter() {
return a + b + c;
}
}
public class Triangle extends Shape {
double a, b, c, s;
public Triangle(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
s = perimeter()/2;
}
double area() {
return Math.sqrt(s*(s-a)*(s-b)*(s-c));
}
double perimeter() {
return a + b + c;
}
}
Rectangle.java
package com.java.abst;
public class Rectangle extends Shape {
double length, breadth;
public Rectangle(double length, double breadth) {
this.length = length;
this.breadth = breadth;
}
double area() {
return length * breadth;
}
double perimeter() {
return 2 * (length * breadth);
}
}
public class Rectangle extends Shape {
double length, breadth;
public Rectangle(double length, double breadth) {
this.length = length;
this.breadth = breadth;
}
double area() {
return length * breadth;
}
double perimeter() {
return 2 * (length * breadth);
}
}
Square.java
package com.java.abst;
public class Square extends Shape {
double side;
public Square(double side) {
this.side = side;
}
double area() {
return side * side;
}
double perimeter() {
return 4 * side;
}
}
public class Square extends Shape {
double side;
public Square(double side) {
this.side = side;
}
double area() {
return side * side;
}
double perimeter() {
return 4 * side;
}
}
Cuboid.java
package com.java.abst;
public class Cuboid extends Shape {
double l, b, h;
public Cuboid(double l, double b, double h) {
this.l = l;
this.b = b;
this.h = h;
}
double area() {
return 2*(l*b + b*h + h*l);
}
double perimeter() {
return 2*(l*b + b*h + h*l);
}
double volume() {
return l*b*h;
}
}
public class Cuboid extends Shape {
double l, b, h;
public Cuboid(double l, double b, double h) {
this.l = l;
this.b = b;
this.h = h;
}
double area() {
return 2*(l*b + b*h + h*l);
}
double perimeter() {
return 2*(l*b + b*h + h*l);
}
double volume() {
return l*b*h;
}
}
Cube.java
package com.java.abst;
public class Cube extends Shape {
double side;
public Cube(double side) {
this.side = side;
}
double area() {
return 6 * side * side;
}
double perimeter() {
return 6 * side * side;
}
double volume() {
return side * side * side;
}
}
public class Cube extends Shape {
double side;
public Cube(double side) {
this.side = side;
}
double area() {
return 6 * side * side;
}
double perimeter() {
return 6 * side * side;
}
double volume() {
return side * side * side;
}
}
Test.java
package com.java.abst;
public class Test {
public static void main(String[] args) {
//Shape s = new Shape(); //Compiler error because abstract class cannot be Instantiated
Circle c1 = new Circle();
c1.display();
System.out.println("-------------------------------Circle-----------------------------------");
System.out.println("Area of Circle of " + "radius " + c1.radius + " is: " + c1.area());
System.out.println("Perimeter of Circle of " + "radius " + c1.radius + " is: " + c1.perimeter());
Circle c2 = new Circle(10.0);
System.out.println("Area of Circle of " + "radius " + c2.radius + " is: " + c2.area());
System.out.println("Perimeter of Circle of " + "radius " + c2.radius + " is: " + c2.perimeter());
System.out.println("-------------------------------Triangle-----------------------------------");
Triangle t = new Triangle(3, 4, 5);
System.out.printf("Area of Triangle of sides %f, %f and %f is: %f \n",t.a, t.b, t.c, t.area());
System.out.printf("Perimeter of Triangle of sides %f, %f and %f is: %f \n",t.a, t.b, t.c, t.perimeter());
System.out.println("-------------------------------Rectangle-----------------------------------");
Rectangle r = new Rectangle(5, 6);
System.out.printf("Area of Square of length %f and breadth %f is %f \n ", r.length, r.breadth, r.area());
System.out.printf("Perimeter of length %f and breadth %f is %f \n ", r.length, r.breadth, r.perimeter());
System.out.println("-------------------------------Square-----------------------------------");
Square s = new Square(6);
System.out.println("Area of Square of side " + s.side + " is " + s.area());
System.out.println("Perimeter of Square of side " + s.side + " is " + s.perimeter());
System.out.println("-------------------------------Cuboid-----------------------------------");
Cuboid cb = new Cuboid(4, 5, 6);
System.out.printf("Area of Cuboid of dimensions %f, %f and %f is: %f \n",cb.l, cb.b, cb.h, cb.area());
System.out.printf("Perimeter of Cuboid of dimensions %f, %f and %f is: %f \n",cb.l, cb.b, cb.h, cb.perimeter());
System.out.printf("Volume of Cuboid of dimensions %f, %f and %f is: %f \n",cb.l, cb.b, cb.h, cb.volume());
System.out.println("-------------------------------Cube-----------------------------------");
Cube c = new Cube(5);
System.out.printf("Area of Cube of side %f is: %f \n",c.side, c.area());
System.out.printf("Perimeter of Cube of side %f is: %f \n",c.side, c.perimeter());
System.out.printf("Volume of Cube of side %f is: %f \n",c.side, c.volume());
}
}
public class Test {
public static void main(String[] args) {
//Shape s = new Shape(); //Compiler error because abstract class cannot be Instantiated
Circle c1 = new Circle();
c1.display();
System.out.println("-------------------------------Circle-----------------------------------");
System.out.println("Area of Circle of " + "radius " + c1.radius + " is: " + c1.area());
System.out.println("Perimeter of Circle of " + "radius " + c1.radius + " is: " + c1.perimeter());
Circle c2 = new Circle(10.0);
System.out.println("Area of Circle of " + "radius " + c2.radius + " is: " + c2.area());
System.out.println("Perimeter of Circle of " + "radius " + c2.radius + " is: " + c2.perimeter());
System.out.println("-------------------------------Triangle-----------------------------------");
Triangle t = new Triangle(3, 4, 5);
System.out.printf("Area of Triangle of sides %f, %f and %f is: %f \n",t.a, t.b, t.c, t.area());
System.out.printf("Perimeter of Triangle of sides %f, %f and %f is: %f \n",t.a, t.b, t.c, t.perimeter());
System.out.println("-------------------------------Rectangle-----------------------------------");
Rectangle r = new Rectangle(5, 6);
System.out.printf("Area of Square of length %f and breadth %f is %f \n ", r.length, r.breadth, r.area());
System.out.printf("Perimeter of length %f and breadth %f is %f \n ", r.length, r.breadth, r.perimeter());
System.out.println("-------------------------------Square-----------------------------------");
Square s = new Square(6);
System.out.println("Area of Square of side " + s.side + " is " + s.area());
System.out.println("Perimeter of Square of side " + s.side + " is " + s.perimeter());
System.out.println("-------------------------------Cuboid-----------------------------------");
Cuboid cb = new Cuboid(4, 5, 6);
System.out.printf("Area of Cuboid of dimensions %f, %f and %f is: %f \n",cb.l, cb.b, cb.h, cb.area());
System.out.printf("Perimeter of Cuboid of dimensions %f, %f and %f is: %f \n",cb.l, cb.b, cb.h, cb.perimeter());
System.out.printf("Volume of Cuboid of dimensions %f, %f and %f is: %f \n",cb.l, cb.b, cb.h, cb.volume());
System.out.println("-------------------------------Cube-----------------------------------");
Cube c = new Cube(5);
System.out.printf("Area of Cube of side %f is: %f \n",c.side, c.area());
System.out.printf("Perimeter of Cube of side %f is: %f \n",c.side, c.perimeter());
System.out.printf("Volume of Cube of side %f is: %f \n",c.side, c.volume());
}
}
Output:
-------------------------------Circle-----------------------------------
Area of Circle of radius 1.0 is: 3.142857142857143
Perimeter of Circle of radius 1.0 is: 6.285714285714286
Area of Circle of radius 10.0 is: 314.2857142857143
Perimeter of Circle of radius 10.0 is: 62.857142857142854
-------------------------------Triangle-----------------------------------
Area of Triangle of sides 3.000000, 4.000000 and 5.000000 is: 6.000000
Perimeter of Triangle of sides 3.000000, 4.000000 and 5.000000 is: 12.000000
-------------------------------Rectangle-----------------------------------
Area of Square of length 5.000000 and breadth 6.000000 is 30.000000
Perimeter of length 5.000000 and breadth 6.000000 is 22.000000
-------------------------------Square-----------------------------------
Area of Square of side 6.0 is 36.0
Perimeter of Square of side 6.0 is 24.0
-------------------------------Cuboid-----------------------------------
Area of Cuboid of dimensions 4.000000, 5.000000 and 6.000000 is: 148.000000
Perimeter of Cuboid of dimensions 4.000000, 5.000000 and 6.000000 is: 148.000000
Volume of Cuboid of dimensions 4.000000, 5.000000 and 6.000000 is: 120.000000
-------------------------------Cube-----------------------------------
Area of Cube of side 5.000000 is: 150.000000
Perimeter of Cube of side 5.000000 is: 150.000000
Volume of Cube of side 5.000000 is: 125.000000
No comments:
Post a Comment