Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Java
  4. Different object types (references)

Different object types (references)

Scheduled Pinned Locked Moved Java
c++javadata-structuresjson
9 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kujtim Hyseni
    wrote on last edited by
    #1

    Hello, I heave an array of objects which each object has some common parameters, but also is to be characterized by adequate specific object. For each object I use 'objectType' parammeter to differentiate, then the rest, I tried as follows (using 'Object' type) but it is not working. Here is the complete simplified code:

    class DiffObjectClasses{
    public static void main(String [] args)
    {
    SerialObject so;
    IPObject iob;

           ObjectType ot\[\] = new ObjectType\[3\]; 
            
           ot\[0\].objectID=1; 
           ot\[0\].objectType=1; 
           so=new SerialObject(); 
           so.serialPortNo=21; 
           ot\[0\].objectContent=so; 
            
           ot\[1\].objectID=2; 
           ot\[1\].objectType=1; 
           so=new SerialObject(); 
           so.serialPortNo=21; 
           ot\[1\].objectContent=so; 
            
           ot\[2\].objectID=3; 
           ot\[2\].objectType=2; 
           iob=new IPObject(); 
           iob.host="localhost"; 
           iob.port=2200; 
           ot\[2\].objectContent=iob; 
            
           System.out.println("----------------------------"); 
           for(int indx=0; indx<ot.length;indx++) 
           { 
                 System.out.println("ObjID:"+ot\[indx\].objectID); 
                 System.out.println("ObjType:"+ot\[indx\].objectType); 
                 switch(ot\[indx\].objectType) 
                 { 
                       case 1: 
                                   so=(SerialObject)ot\[indx\].objectContent; 
                                   System.out.println("Serial port no:"+so.serialPortNo); 
                                   break; 
                       case 2: 
                                   iob=(IPObject)ot\[indx\].objectContent; 
                                   System.out.println("IP host:"+iob.host); 
                                   System.out.println("IP port:"+iob.port); 
                                   break;             
                 } 
           } 
     } 
    

    }

    class ObjectType{
    int objectID;
    int objectType;
    Object objectContent;
    }

    class SerialObject{
    int serialPortNo;
    }

    class IPObject{
    String host="";
    int port;
    }

    I've worked in C/C++, it was very simple by declaring 'void' member and then using implicit conversion as here but it sin't working in Java. Kujtim

    L 1 Reply Last reply
    0
    • K Kujtim Hyseni

      Hello, I heave an array of objects which each object has some common parameters, but also is to be characterized by adequate specific object. For each object I use 'objectType' parammeter to differentiate, then the rest, I tried as follows (using 'Object' type) but it is not working. Here is the complete simplified code:

      class DiffObjectClasses{
      public static void main(String [] args)
      {
      SerialObject so;
      IPObject iob;

             ObjectType ot\[\] = new ObjectType\[3\]; 
              
             ot\[0\].objectID=1; 
             ot\[0\].objectType=1; 
             so=new SerialObject(); 
             so.serialPortNo=21; 
             ot\[0\].objectContent=so; 
              
             ot\[1\].objectID=2; 
             ot\[1\].objectType=1; 
             so=new SerialObject(); 
             so.serialPortNo=21; 
             ot\[1\].objectContent=so; 
              
             ot\[2\].objectID=3; 
             ot\[2\].objectType=2; 
             iob=new IPObject(); 
             iob.host="localhost"; 
             iob.port=2200; 
             ot\[2\].objectContent=iob; 
              
             System.out.println("----------------------------"); 
             for(int indx=0; indx<ot.length;indx++) 
             { 
                   System.out.println("ObjID:"+ot\[indx\].objectID); 
                   System.out.println("ObjType:"+ot\[indx\].objectType); 
                   switch(ot\[indx\].objectType) 
                   { 
                         case 1: 
                                     so=(SerialObject)ot\[indx\].objectContent; 
                                     System.out.println("Serial port no:"+so.serialPortNo); 
                                     break; 
                         case 2: 
                                     iob=(IPObject)ot\[indx\].objectContent; 
                                     System.out.println("IP host:"+iob.host); 
                                     System.out.println("IP port:"+iob.port); 
                                     break;             
                   } 
             } 
       } 
      

      }

      class ObjectType{
      int objectID;
      int objectType;
      Object objectContent;
      }

      class SerialObject{
      int serialPortNo;
      }

      class IPObject{
      String host="";
      int port;
      }

      I've worked in C/C++, it was very simple by declaring 'void' member and then using implicit conversion as here but it sin't working in Java. Kujtim

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      If you declare those last two classes like so:

      class SerialObject extends ObjectType{
      int serialPortNo;
      }

      class IPObject extends ObjectType{
      String host="";
      int port;
      }

      It may work. You generally can't cast types to each other unless they somehow derive from each other in some way (unless it's a primitive type)

      K 1 Reply Last reply
      0
      • L Lost User

        If you declare those last two classes like so:

        class SerialObject extends ObjectType{
        int serialPortNo;
        }

        class IPObject extends ObjectType{
        String host="";
        int port;
        }

        It may work. You generally can't cast types to each other unless they somehow derive from each other in some way (unless it's a primitive type)

        K Offline
        K Offline
        Kujtim Hyseni
        wrote on last edited by
        #3

        Hello, I just tried your suggestion but it doesn't works. Even tried extends 'Object' since is more logical. This is the error: Exception in thread "main" java.lang.NullPointerException at DiffObjectClasses.main(DiffObjectClasses.java:9) Thanks anyway, Kujtim

        modified on Friday, July 10, 2009 2:24 PM

        L 1 Reply Last reply
        0
        • K Kujtim Hyseni

          Hello, I just tried your suggestion but it doesn't works. Even tried extends 'Object' since is more logical. This is the error: Exception in thread "main" java.lang.NullPointerException at DiffObjectClasses.main(DiffObjectClasses.java:9) Thanks anyway, Kujtim

          modified on Friday, July 10, 2009 2:24 PM

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Oh. I don't know then. But if I may ask, why are you doing it the C way? If you're just going to store and print them, maybe I would do something like:

          interface IPrintable
          {
          void Print();
          }

          class SerialObject implements IPrintable{
          int serialPortNo;
          public SerialObject(int portno)
          {
          serialPortNo = portno;
          }
          public void Print()
          {
          System.out.println("Serial port no:" + serialPortNo);
          }
          }

          class IPObject implements IPrintable{
          String host;
          int port;
          public IPObject(String host, int port)
          {
          this.host = host;
          this.port = port;
          }
          public void Print()
          {
          System.out.println("IP host:" + host);
          System.out.println("IP port:" + port);
          }
          }

          And then make an array of IPrintable's, of course.

          K 1 Reply Last reply
          0
          • L Lost User

            Oh. I don't know then. But if I may ask, why are you doing it the C way? If you're just going to store and print them, maybe I would do something like:

            interface IPrintable
            {
            void Print();
            }

            class SerialObject implements IPrintable{
            int serialPortNo;
            public SerialObject(int portno)
            {
            serialPortNo = portno;
            }
            public void Print()
            {
            System.out.println("Serial port no:" + serialPortNo);
            }
            }

            class IPObject implements IPrintable{
            String host;
            int port;
            public IPObject(String host, int port)
            {
            this.host = host;
            this.port = port;
            }
            public void Print()
            {
            System.out.println("IP host:" + host);
            System.out.println("IP port:" + port);
            }
            }

            And then make an array of IPrintable's, of course.

            K Offline
            K Offline
            Kujtim Hyseni
            wrote on last edited by
            #5

            Hello and thanks for responding, there is a list of objects (class instances) which heave few common properties (at the moment two) but they belong to certain type regarding to access they provide. These specific properties seems more logical to be organized in a class, but the objects must be encapsulated together. More simply, let these objects be servers, so the particular object Server[i], has the properties: id (Server[i].id), type and accessobject(which can be SerialObject or IPObject). Good and similar example is ArrayList which enables storing objects, I then 'create' instances from these objects using implicit conversion. There also figures Object type as an argument, but here that is not working. Kujtim

            L 1 Reply Last reply
            0
            • K Kujtim Hyseni

              Hello and thanks for responding, there is a list of objects (class instances) which heave few common properties (at the moment two) but they belong to certain type regarding to access they provide. These specific properties seems more logical to be organized in a class, but the objects must be encapsulated together. More simply, let these objects be servers, so the particular object Server[i], has the properties: id (Server[i].id), type and accessobject(which can be SerialObject or IPObject). Good and similar example is ArrayList which enables storing objects, I then 'create' instances from these objects using implicit conversion. There also figures Object type as an argument, but here that is not working. Kujtim

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Ok I'm not disagreeing or something like that, but, well. Why do you use a custom "type" thing when Java already provides the means to see which class an object is an instance of?

              K 1 Reply Last reply
              0
              • L Lost User

                Ok I'm not disagreeing or something like that, but, well. Why do you use a custom "type" thing when Java already provides the means to see which class an object is an instance of?

                K Offline
                K Offline
                Kujtim Hyseni
                wrote on last edited by
                #7

                Thanks again, I am experienced C/C# programmer but will try that in Java. Kujtim

                L K 2 Replies Last reply
                0
                • K Kujtim Hyseni

                  Thanks again, I am experienced C/C# programmer but will try that in Java. Kujtim

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  The operator is in C# is called instanceof in Java (saves you some googling)

                  1 Reply Last reply
                  0
                  • K Kujtim Hyseni

                    Thanks again, I am experienced C/C# programmer but will try that in Java. Kujtim

                    K Offline
                    K Offline
                    Kujtim Hyseni
                    wrote on last edited by
                    #9

                    Hello, I just solved the problem. It was because the instance was not created with ObjectType ot[] = new ObjectType[3]; so I had to add also the lines ot[0]=new ObjectType(); , ot[1]=new ObjectType(); and ot[2]=new ObjectType(); after that. The working code looks as follows:

                    class DiffObjectClasses{
                    public static void main(String [] args)
                    {
                    SerialObject so;
                    IPObject iob;

                    	ObjectType ot\[\] = new ObjectType\[3\];
                    	
                    	ot\[0\]=new ObjectType();	// This was added!!!	
                    	ot\[0\].objectID=1;
                    	ot\[0\].objectType=1;
                    	so=new SerialObject();
                    	so.serialPortNo=21;
                    	ot\[0\].objectContent=so;
                    	
                    	ot\[1\]=new ObjectType();	// This was added!!!
                    	ot\[1\].objectID=2;
                    	ot\[1\].objectType=1;
                    	so=new SerialObject();
                    	so.serialPortNo=21;
                    	ot\[1\].objectContent=so;
                    	
                    	ot\[2\]=new ObjectType();	// This was added!!!
                    	ot\[2\].objectID=3;
                    	ot\[2\].objectType=2;
                    	iob=new IPObject();
                    	iob.host="localhost";
                    	iob.port=2200;
                    	ot\[2\].objectContent=iob;
                    	
                    	System.out.println("----------------------------");
                    	for(int indx=0; indx<ot.length;indx++)
                    	{
                    		System.out.println("ObjID:"+ot\[indx\].objectID);
                    		System.out.println("ObjType:"+ot\[indx\].objectType);
                    		switch(ot\[indx\].objectType)
                    		{
                    			case 1:
                    					so=(SerialObject)ot\[indx\].objectContent;
                    					System.out.println("Serial port no:"+so.serialPortNo);
                    					break;
                    			case 2:
                    					iob=(IPObject)ot\[indx\].objectContent;
                    					System.out.println("IP host:"+iob.host);
                    					System.out.println("IP port:"+iob.port);
                    					break;		
                    		}
                    	}
                    }
                    

                    }

                    class ObjectType extends Object{
                    int objectID;
                    int objectType;
                    Object objectContent;
                    }

                    class SerialObject extends Object{
                    int serialPortNo;
                    }

                    class IPObject extends Object{
                    String host="";
                    int port;
                    }

                    Thanks for other suggestions!

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups