Results 1 to 7 of 7
  1. #1
    ziggy is offline Member
    Join Date
    Nov 2008
    Posts
    11
    Rep Power
    0

    Default Effective polymorphism approach

    Test code

    The Interface
    Java Code:
    		package com;
    		public interface Parser{
    			public void parse(String message);
    		}
    		
    		
    		package com;
    MessageA Parser
    Java Code:
    		
    		public class MessageAParser implements Parser{
    			public void parse(String message){
    				System.out.println("Parsing A");
    			}
    		}
    MessageB Parser
    Java Code:
    		
    		package com;
    		
    		public class MessageAParser implements Parser{
    			public void parse(String message){
    				System.out.println("Parsing A");
    			}
    		}
    MessageC parser (This expects an object)
    Java Code:
    		
    		package com;
    		public class MessageCParser implements Parser{
    			public void parse(Object message){
    				MessageObject mobject = (MessageObject)message; 
    				System.out.println("Parsing C" + mobject.getMessage());
    			}
    			
    			public void parse(String m){}
    		}
    The parser manager that calls the relevant parser.
    Java Code:
    		
    		package com;
    		
    		import java.util.HashMap;
    		
    		public class ParserManager{
    			
    			public ParserManager() {
    				prepare();
    			}
    		
    			HashMap parsers	= new HashMap();
    			
    			
    			public void prepare(){
    				
    				parsers.put("A",new MessageAParser());
    				parsers.put("B",new MessageBParser());
    				parsers.put("C",new MessageCParser());
    			}
    			
    			public void parseMessage(String msgType, String message){
    				((Parser)parsers.get(msgType)).parse(message);
    			}
    		}


    The controller.
    Java Code:
    		
    		package com;
                    public class ControlClass{
    			
    			public static void main(String[] args){
    				
    				ParserManager pManager = new ParserManager();
    		
    				//Parse A
    				pManager.parseMessage("A","ATEXT TO PARSE");
    		
    				//Parse B
    				pManager.parseMessage("B","BTEXT TO PARSE");
    		
    				//Parse C
    				Object mobject = new MessageObject();
    				pManager.parseMessage("C",(String)mobject);
    			}
    		}
    When i run the above program i get the following exception

    Java Code:
    Parsing A
    Parsing B
    java.lang.ClassCastException
    	at com.ControlClass.main(ControlClass.java:17)
    Exception in thread "main"
    It looks like i cant pass an Object (casted to a String) where a String is expected. (Why is this the case since String is a subclass of Object?)
    Is there a way i can call pManager.parseMessage and pass an object to it without having to modify ParserManager.
    Last edited by ziggy; 02-27-2011 at 06:53 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your MessageObject object is not a String. Since it inherits from Object (as do all classes), it will have a valid toString() method, and this can be used instead, but if this method is not overridden, the String returned will not be terribly useful to you.

  3. #3
    ziggy is offline Member
    Join Date
    Nov 2008
    Posts
    11
    Rep Power
    0

    Default

    Hi, i think you misunderstood the question :) Here is the messageobject class which i hadnt included in the previous example.

    Java Code:
    package com;
    public class MessageObject{
    	String message = "Parsing C";
    	String namespace = "namespace";
    	String xmlVersion = "1.0";
    	
    	public String getMessage() {
    		return message;
    	}
    	public void setMessage(String message) {
    		this.message = message;
    	}
    	public String getNamespace() {
    		return namespace;
    	}
    	public void setNamespace(String namespace) {
    		this.namespace = namespace;
    	}
    	public String getXmlVersion() {
    		return xmlVersion;
    	}
    	public void setXmlVersion(String xmlVersion) {
    		this.xmlVersion = xmlVersion;
    	}
    	
    	
    }
    The question is related to passing objects of different types. I have a method that expects a String but i want to pass an Object to it.
    Last edited by ziggy; 02-27-2011 at 08:10 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ziggy View Post
    Hi, i think you misunderstood the question :)
    No, I don't think I misunderstood you at all, and in fact am sure that I understand you perfectly.


    .....

    The question is related to passing objects of different types. I have a method that expects a String but i want to pass an Object to it.


    The problem is quite simply this line of code here:
    Java Code:
    pManager.parseMessage("C", (String) mobject);

    You are casting a MessageObject object to String, and while this is valid to the compiler, since mobject is an Object variable, it fails at runtime because mobject refers to a MessageObject object.
    Last edited by Fubarable; 02-27-2011 at 09:30 PM.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default

    Yes.
    You can't just cast between Objects of any class and expect it to work.

    Your MessageObject is not a String, so trying to cast it to one causes the exception.

  6. #6
    ziggy is offline Member
    Join Date
    Nov 2008
    Posts
    11
    Rep Power
    0

    Default

    The reason i was doing that is that i wanted to be able to pass any type of object. I was hoping that i would have been able to pass it as a String (Even though it is not) and only refer to it as as string where it is expected to be a string ( hope that makes sense)

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default

    You'll need two parseMessage() methods, one taking a String and the other a MessageObject. Something along those lines anyway.

Similar Threads

  1. Reg Effective Java books
    By nn12 in forum New To Java
    Replies: 0
    Last Post: 02-22-2011, 07:07 PM
  2. most effective method?
    By java_newby in forum New To Java
    Replies: 0
    Last Post: 12-04-2010, 09:47 PM
  3. Regex approach
    By karlito in forum Lucene
    Replies: 1
    Last Post: 11-04-2009, 05:53 PM
  4. New Database Approach Advice
    By greenco in forum New To Java
    Replies: 2
    Last Post: 12-02-2008, 08:26 AM
  5. Please suggest me the correct approach!
    By rjuyal in forum Advanced Java
    Replies: 6
    Last Post: 05-05-2008, 02:54 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
  •