#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0

    Exclamation Problem with downcasting


    Hello,
    I have to make a downcasting but I always get a run-time error. The classes' hierarchy is:
    Code:
    Class A abstract 
       - Class B extends A
       - Class C extends A
    
       - Class D abstract extends A
           °Class E abstract extends D
              °Class F extends E
    A is an abstract class that implements some interfaces. D and E are abstract too, they make the gui in this program in contrast to B and C which make the logic. So the same operations are eather in B & C or D, but D makes graphic rappresentation. Using a C object I need to use a specific graphical method that is implemented in F. I tried to make an upcasting and then a downcasting but I get the run-time error. I tried this:
    C c1=something.. ;
    A a1=c1;
    D d1=(D) a1;
    d1.specificGraphicMethod();

    How can I make the downcasting?
    Thanks for helping
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    get a run-time error
    Can you post the full text of the error message and the statement that causes it?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0
    Sure, that's the all stack trace:
    Code:
    Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: Logic.characterPackage.MainCharacter cannot be cast to Logic.characterPackage.CharactersManager
    	at GUI.decoratorPackage.MainFrame$2.actionPerformed(MainFrame.java:219)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    	at javax.swing.AbstractButton.doClick(AbstractButton.java:389)
    	at com.apple.laf.ScreenMenuItem.actionPerformed(ScreenMenuItem.java:95)
    	at java.awt.MenuItem.processActionEvent(MenuItem.java:650)
    	at java.awt.MenuItem.processEvent(MenuItem.java:609)
    	at java.awt.MenuComponent.dispatchEventImpl(MenuComponent.java:343)
    	at java.awt.MenuComponent.dispatchEvent(MenuComponent.java:331)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
    	at java.awt.EventQueue.access$400(EventQueue.java:82)
    	at java.awt.EventQueue$2.run(EventQueue.java:663)
    	at java.awt.EventQueue$2.run(EventQueue.java:661)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
    	at java.awt.EventQueue$3.run(EventQueue.java:677)
    	at java.awt.EventQueue$3.run(EventQueue.java:675)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:674)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    How do the names of the classes in the error message map to those in your example?

    D d1=(D) a1
    The object pointed to by a1 is not a class D object.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0
    Logic.characterPackage.MainCharacter is C and Logic.characterPackage.CharactersManager is D. From a C type I need to use its information to call a gui method in D, this method only creates a JFrame and shows other options to call other methods. Is there a simpler way to do that? Calling a method in D from C
  10. #6
  11. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,576
    Rep Power
    1906
    There is no (inheritance) link between D and C. You will have to create an instance of C in D.

    For example
    Code:
    Class D
        function myfunc()
             C c1 = new C();
    Depend at you logic, you can pass it as parameter:
    Code:
    Class Main
        function myMain()
             C c1 = new C()
             D d1 = new D(c1)
    
    Class D
         C myCat;
         function D (C para_cat)
               myCat = para_cat
    I suggest you take a look this description about Downcasting and Upcasting
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0
    Yes I already put an instance of C in D, but if I use it, I can only call C's methods. For example, I have this situation:
    Code:
    public abstract class D extends A {
    	protected A a1;
    	
    	public D(A a1) {
    		super(a1.getID(), a1.getName(), a1.getLocation());
    		
    		this.a1=a1;
    	}
    
    	@Override
    	public abstract void method_one();
    
    	@Override
    	public abstract void method_two();
    
    	@Override
    	public abstract void method_three(int i, A a2);
    
    	@Override
    	public abstract void method_four();
    
    	@Override
    	public abstract void method_ofive();
    
    	@Override
    	public abstract void method_six();
    
    	@Override
    	public abstract void method_seven();
    
    	@Override
    	public abstract void method_eight();
    
    }
    From a sub-type, in this case from a C type, I have to call a specific method, for example method_one, which implements gui operations (using or not C information). I have the same methods of D, in C, but if I call them on C, I don't get what i want because C only implements logic operations. So i thought to cast it to D and then call the method_one, but I only got run-time error. The problem is to separate program logic from program gui, so I implemented that type of classes structure. The main class is A, the concrete objects are B and C (program logic), and from D to F are decorative classes which have the scope to implement program gui. I followed this implementation from the Decorator Pattern, but I still get this type of errors.
  14. #8
  15. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,576
    Rep Power
    1906
    I am not sure whether Decorator Pattern is what you need for this task.
    the Decorator Pattern is mostly used to extend functionality, not separate it.
    I suggest you also take a look at the Model View Controller, if you haven't look at it already.

    How is the code for A and C?
    Simple example how it can work, (pseudo-code):
    Code:
    abstract class A
        public abstract void method_one();
    
    class C extends A
       public void method_one()
          // do something within C
    
    class D extends A
       A myA
       D(A a1)
           myA = a1
    
       public void method_one()
          // do something related to D
          myA.method_one();  // this will be function from class C
          // do something related to D
          
    class main
       main start
          D d1 = new D(new C())
          d1.method_one()
    Last edited by MrFujin; January 22nd, 2013 at 02:16 PM.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0
    Thanks for the link to Model–view–controller, I had already looked at it, but I haven't studied it yet and my professor suggested that for this project, we can make it good even just using what he explained in his lessons. We studied some patterns, that's the reason I thought to use the Decorator Pattern. Anyway I think i'm on the right way to fix the problem (also after read your pseudo-code example ). I'm working on it so I can tell you more later whether I fixed or not
    Anyway A is:
    Code:
    public abstract class implements interface1, interface2, etc {
        private String id;
        private String name;
        ....
    
        public A (String id, String name, ... , ....){
            this.id=id; 
            this.name=name;
            ...
        }
    
        public abstract method_interface1();
        public abstract method_interface2();
        ....
    
    }
    and C is:
    Code:
    public class C extends A{
    
    public C (String id, String name, ... , ....){
           super(id, name, .. , ...);
    }
    
    public  method_interface1() {...........}
    public  method_interface2() {.............}
    ....
    
    }

IMN logo majestic logo threadwatch logo seochat tools logo