Friday, March 4, 2016

Java - Abstract Class with Abstract & Functional Methods with simple example

What is an abstract ?
Let us take the word shape.
The very word shape is abstract, when we say shape, 
we can think of many objects but nothing in particular.
We can't say whether it is a circle or a triangle or some other shape.

The shapes Circle, Triangle and Rectangle are concrete objects.
But Shape is abstract. It has no concrete reality.
The word shape lacks DETAIL to define any particular shape but at the same it DECLARES what it is about.
Abstract class in Java is very similar to that.
An abstract class declares what the class is about but do not contain the implementation details.

As we can't think of an object with the word shape, 
we can't create an object instance with the abstract class in Java. 
But, from shape we can derive Circle, Triangle etc.
Similarly, 
keeping abstract class as superclass we can derive subclasses, implement details and create objects.

Coming to methods,
Abstract class may contain abstract methods (methods declared but not implemented) as well as functional methods (methods fully implemented). 
Those abstract methods must be implemented in the derived classes, otherwise derived classes will be considered as an abstract class only.

We will see two simple examples -one with abstract method and another with functional method. 

Programs:

Example -1 : Abstract class with abstract method.

/* Abstract class 'shape' with abstract 'Area' method */
abstract class Shape{
    abstract float Area();
}
/* Subclasses Circle, Rectangle, Triangle with method 'Area' implemented accordingly */
class Circle extends Shape {
   float radius=10;
    float Area(){
        return( (22/7) * radius*radius);
    }
}
class Rectangle extends Shape {
    float length =10, breath = 5;
        float Area(){
        return(length*breath);
    }
}  

class Triangle extends Shape {
     float base =10, height = 5;
        float Area(){
        return(base*height)/2;
    }
}  

/* main program */
public class AbstractClsExp1 {

    public static void main(String[] args) {
      /* Object Instant creation through subclasses Circle,Rectangle and Triangle */
        Circle CirObj = new Circle();
        Rectangle RecObj = new Rectangle();
        Triangle TriObj = new Triangle();
      /* calculating & displaying area through 'Area' method in respective objects */ 
         System.out.println( "Circle : Radius = " + CirObj.radius + ",Area = " + CirObj.Area());
         System.out.println( "Rectangle : Length = " + RecObj.length + ", Breath = " + RecObj.breath + ", Area = " +RecObj.Area());
         System.out.println( "Triangle : Base = " + TriObj.base + ", Height = " + TriObj.height + ", Area = " + TriObj.Area());
    }
    
}

Output :
run:
Circle : Radius = 10.0,Area = 300.0
Rectangle : Length = 10.0, Breath = 5.0, Area = 50.0
Triangle : Base = 10.0, Height = 5.0, Area = 25.0

Example -2 : Abstract class with functional method.
         
 /* abstract class 'shape' with functional method 'Area'
    It has 3 parameters, 's' for particular shape, 
    p1,p2 for radius,length,breath,base,height accordingly 
*/
abstract class Shape{
     float Area(String s, float p1,float p2){
         float a=0;
         if (s == "r"){ a= p1*p2;}
         if (s == "c"){ a= (22/7)*p1*p1;}
         if (s == "t"){a= ((p1*p2)/2);}
         return a;
     }
}
/* derived class 'CRT' (circle,rectangle,triangle) from 'Shape' */

class CRT extends Shape{
     
}

/* main program */

public class AbstractClssExp {
       public static void main(String[] args) {

        float radius = 10, length =10, breath=5,   base=10,  height=6;
        CRT  CRTObj= new CRT(); // object instant creation
        
        /* same method 'Area' is used to calculate area of three different shapes 
           by passing corresponding parameters 
        */
        float CirArea= CRTObj.Area("c",radius,0);
        float RecArea= CRTObj.Area("r",length,breath);
        float TriArea= CRTObj.Area("t",base,height);
              
       System.out.println("Circle - Radius = "  + radius + " ,Area = "+ CirArea);
       System.out.println("Rectangle - Length = "+length+" ,Breath = "+ breath+
                                        " ,Area = "+ RecArea);
       System.out.println("Triangle - Base = "+base+" ,Height "+ height+
                                        ", Area = " + TriArea);
    }
    
}

Output :
run:
Circle - Radius = 10.0 ,Area = 300.0
Rectangle - Length = 10.0 ,Breath = 5.0 ,Area = 50.0
Triangle - Base = 10.0 ,Height 6.0, Area = 30.0

* All abstract classes  are public by default..
* In the earlier versions ,an abstract class must have at least one abstract method,
but not so now. Now, an abstract class need not have any abstract method.
Still, one may maintain the earlier condition for other programming reasons, 
which is  subjective.




No comments:

Post a Comment