Results 1 to 4 of 4
  1. #1
    ajaygargnsit is offline Member
    Join Date
    Dec 2007
    Posts
    9
    Rep Power
    0

    Default An interface query !!

    I wish to make a code snippet, that demonstartes ue of interface. Specifically, I do the following two things ::

    1. Print out what shape it is by using sayWhat() function.
    2. Print out the area using area function. Herein I have a doubt :: Since a circle and square require different number of arguments for area calculation, can I have a single declaration in interface, with different implementations in the corresponding implementaton classes ??

    I know, the above thing is impossible using a concrete class or an abstract class, just want to know in the case of 'interfaces' ?

    The interface is shown below ::



    interface Shape
    {

    void sayWhat(); // fine here.

    // void area(int radius); // Doubt
    // void area(int length, int breadth); //Doubt

    // Can the above two lines be collapsed in a single declaration ???

    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    interface Shape {
        void sayWhat();
        void area();
    }
    
    class Rectangle implements Shape {
        int width;
        int height;
    
        Rectangle(int width, int height) {
            this.width= width;
            this.height = height;
        }
    
        public void area() {
            System.out.println("area = " + width*height);
        }
    
        public void sayWhat() {
            System.out.println("Rectangle[width: "  + width +
                               " height: " + height +
                               " area: "   + area() + "]";
        }
    }
    
    class Circle implements Shape {
        int radius;
    
        Circle(int radius) {
            this.radius = radius;
        }
    
        public void area() {
            System.out.println("area = " + Math.PI*radius*radius);
        }
    
        public void sayWhat() {
            System.out.println("Circle[radius: " + radius +
                               " area: "   + area() + "]");
        }
    }

  3. #3
    ajaygargnsit is offline Member
    Join Date
    Dec 2007
    Posts
    9
    Rep Power
    0

    Default

    Well, thanks. This version seems to be alright, but i can see the following limitations :

    1. The arguments need to be passed at the time of object instantiation.

    Thus, in case if i wish to do something like this ::

    obj.area(radius); // for circle
    obj.area(length,breadth); // for rectangle

    where obj may be a 'circle' or 'rectangle' object, WHICH HAS BEEN instantiated before; and the arguments are entered by the user at a point after the object has been created .. ?

    Is there a getaround for this ?

  4. #4
    spoon! is offline Member
    Join Date
    Dec 2007
    Posts
    12
    Rep Power
    0

    Default

    just have methods that get and set the fields; then you can set the radius / length, width of whatever
    but why does it matter? you should just create a new object when you get the input

Similar Threads

  1. Help in Query
    By geeta_ravikanti in forum JDBC
    Replies: 0
    Last Post: 03-31-2008, 01:16 PM
  2. Using sql:query tag
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-15-2008, 03:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •