Results 1 to 6 of 6

Thread: Polymorphism and objects stored in a vector

  1. #1
    Join Date
    Dec 2009
    Posts
    178

    Polymorphism and objects stored in a vector

    Hello,
    I try to implement polymorphism by creating a class ObjDraw.
    Here is my code.
    Code:
    class ObjDraw 
    {
      int tyobj;
      Polygon pobj;
      String lbl;
    
      Public ObjDraw (int tyobj, pobj Polygon, String lbl)
      {
          .....
      }
    
     Draw(Graphics g, int tx, int ty, double scale, int[] frame)
      {
        .....
      }
    }
    I try to store my objects in a Vector but when I my vector and I cast my objects according tyobj in ObjDrawRect and I call the draw method. I think I have a problem in working with polymorphism and the object stored in vector. Do you have any idea of how to handle both of them. Any help will be highly appreciated. Thanks in advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    330

    Re: Polymorphism and objects stored in a vector

    Hello,
    The ideal would be to change the draw function which is in your code, it will be two different things depending on the value of tyobj. Maybe not the best solution but at least this is the easiest. It appears that you store different type objects ObjRaw. If you have used your templates in Vector, then whatever the outcome of your cast, take your java object as ObjRaw since it is a vector of ObjRaw, and thus the method of draw ObjRaw be called. Hope you find this solution a good one for your need. If you have any other problem regarding this, then post back and we will try to find a soltuion for this.

  3. #3
    Join Date
    Nov 2008
    Posts
    240

    Re: Polymorphism and objects stored in a vector

    Hello,
    I am new to this concept and I have done a code which I think can help you. The following is the code and I think this can help you.
    Code:
    private void drawDrawObj(int tx, int ty, double sc,int zcdr[])
    { 
      for (int i = 0 ; I <drobjjVector.size() ; I + +)
      {
        CustomDrawObj drobjj = CustomDrawObj)drobjjVector.elementAt(i);
        
        System.out.System.out.println("drobjj.type" + DrawObj.type);    
    				
        switch (drobjj.type)
        {
             box Const.OBJ_RECT : System.out.System.out.println("Cast OBJ_RECT");
        drobjj = (CustomDrawPOLYG) drobjj;
        		                            break;
        	 box Const.OBJ_CERCLE : System.out.System.out.println("Cast OBJ_CustomDrawCERCLE");
        drobjj = (CustomDrawCERCLE) drobjj;
        		                               break;		
        } 
        		
        System.out.System.out.println("drawDrawObj" + DrawObj.getClass());
        
        drobjj.Draw(bufGr, selColor, tx, ty, sc, zcdr,false,false);	
      }	
    }

  4. #4
    Join Date
    Nov 2009
    Posts
    333

    Re: Polymorphism and objects stored in a vector

    Hello,
    Thew following line in your code is confusing me, have a look at this
    Code:
    drobj= (CustomDrawPOLYG)drobjj;
    I think this does absolutely nothing. I can not even explain what should you do, I do not see any solution for this, I can just tell you that x = (Foo) x is necessarily a code that is useless. If drobj.getClass returns CustomDrawPOLYG then drobj is an object of class CustomDrawPOLYG And the call to draw on that object will activate the draw method of signing the same class CustomDrawPOLYG or nearest relative or it is. Hope you are understanding the concept behind this.

  5. #5
    Join Date
    Nov 2009
    Posts
    583

    Re: Polymorphism and objects stored in a vector

    Hello,
    No need to implement a type variable to determine which body is your object. For that you have the keyword instanceOf. Here is how you can do it in your code. See, the following line below
    Code:
    if (drobj instanceOf CustomDrawPOLYG) ...;
    I recommend you to do some study on core java and the best place that you can go to is the sun's official site and there you can find more detailed explanation with easy to understand examples. First get the basics clear and then implement it in the coding.

  6. #6
    Join Date
    Nov 2009
    Posts
    347

    Re: Polymorphism and objects stored in a vector

    Hello,
    If you need to run tests to determine the type of your drobj is that you have a design problem. The polymorphism is precisely there to prevent this type of testing.

    If you want your two items are drawn differently, you just:

    - Write an abstract method draw() on CustomDrawObj
    - Actually implement this method to classes CustomDrawPOLYG, CustomDrawCERCLE

    - Call objDraw.draw () => polymorphism and the rest will automatically call the correct method depending on the actual class of your object, without any explicit casting is necessary

Similar Threads

  1. Using stored procedures and stored functions in MySQL
    By Adiran in forum Software Development
    Replies: 6
    Last Post: 14-12-2010, 01:54 AM
  2. Vector objects and methods
    By Miles Runner in forum Software Development
    Replies: 5
    Last Post: 26-02-2010, 04:14 AM
  3. Runtime polymorphism Vs Compile time polymorphism
    By Owen Fernandes in forum Software Development
    Replies: 4
    Last Post: 03-02-2010, 11:06 AM
  4. How to sort vector objects by class member variables in c++?
    By Linoo in forum Software Development
    Replies: 4
    Last Post: 29-01-2010, 07:15 PM
  5. How to pass reference to vector objects
    By Recko in forum Windows Software
    Replies: 3
    Last Post: 07-08-2009, 10:23 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,718,005,850.56414 seconds with 16 queries