Results 1 to 5 of 5
  1. #1
    30mar is offline Member
    Join Date
    May 2010
    Posts
    2
    Rep Power
    0

    Default ArrayList of type superclass problem

    Hello

    I`m new to java and actually this is my 1st real program.

    now my problem is i have an abstract super-class "Component" and contains one abstract method run() and two Attributes and their concrete setAttribute functions
    inherited from this class sub classes that contain their own attributes and concrete methods
    I made an ArrayList of type Suber-class <Component> and let it refer to instances of the sub-class
    now if i try to call a concrete method of any sub class instance the compiler tells me that the given method is not defined for the component class !!!
    should i define every sub class specific concrete method in the sub class !?

    sorry for the long topic and I hope you help me the new java programmer

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

    Default

    How will you know which subclass you're dealing with though? One kludge is to use the instanceof operation, but that smells of a design that needs changing. If you give us more information though, we may be able to give you better help.

    Best of Luck and welcome to Java-Forums!

  3. #3
    30mar is offline Member
    Join Date
    May 2010
    Posts
    2
    Rep Power
    0

    Default

    thanks for your fast reply

    this is the code

    the super-class
    Java Code:
    package com.logicsim.component;
    
    import com.logicsim.Node;
    
    abstract public class Component {
    	
    	protected Node in1 = null;
    	protected Node out1 = null;
    	
    	
    	public void setIn1(Node i1)
    	{
    		in1=i1;
    	}
    	
    	public Node getIn1()
    	{
    		return in1;
    	}
    	
    	
    	
    	public void setOut1(Node o1)
    	{
    		out1=o1;
    	}
    	
    	public Node getOut1()
    	{
    		return out1;
    	}
    	
    
            /* methods that must not be found in any other class except HalfAdder class shown below */
    
    	public void setIn2(Node i2)
    	{}
    	
    	public void setCout(Node cout) 
    	{}
    	
    	/* abstract method */
    
    	abstract public void run();            
    	
    }
    this is one of the sub classes which contains functions like setIn2() which is specific for this class only and i dont want any other class to inherit this function from the super-class

    Java Code:
    package com.logicsim.component.arithmatic;
    
    import com.logicsim.Node;
    import com.logicsim.component.*;
    import com.logicsim.component.gate.*;
    
    public class HalfAdder extends Component {
    
    	protected Node in2 = null;
    	protected Node cOut = null;
    	
    	protected XorGate xor1 = new XorGate();
    	protected AndGate and1 = new AndGate();
    	
    	
    	public void setIn2(Node i2)
    	{
    		in2=i2;
    	}
    	
    	public Node getIn2()
    	{
    		return in2;
    	}
    	
    	
    	public void setCout(Node cout) 
    	{	
    		cOut=cout;
    	}
    	
    	public Node getCout()
    	{
    		return cOut;
    	}
    	
    	
    	public void run() {
    		
    		xor1.setIn1(in1);
    		xor1.setIn2(in2);
    		xor1.setOut1(out1);
    		
    		and1.setIn1(in1);
    		and1.setIn2(in2);
    		and1.setOut1(cOut);
    		
    		xor1.run();
    		and1.run();
    	}
    }
    I made ArrayList of type superclass Component

    ArrayList <Component> Comp = new ArrayList <Component> ();

    which refers to object of type HalfAdder (sub-class)

    when calling setIn2() for that HalfAdder object it tells me this function isn't defined in Component class

    isn't it the function of the dynamic binding to determine that this object in the ArrayList of type Component is one of the sub classes and assign this function call to it ?

  4. #4
    ajaay's Avatar
    ajaay is offline Member
    Join Date
    Dec 2010
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by 30mar View Post
    I made ArrayList of type superclass Component

    ArrayList <Component> Comp = new ArrayList <Component> ();

    which refers to object of type HalfAdder (sub-class)

    when calling setIn2() for that HalfAdder object it tells me this function isn't defined in Component class

    isn't it the function of the dynamic binding to determine that this object in the ArrayList of type Component is one of the sub classes and assign this function call to it ?
    ArrayList does not work in the same way as arrays. Since you used Component class for element of the array list, all elements of variable Comp are of type Component. It is possible to add a class that inherits from the class given in the element field.

    In this case, since Halfadder extends Component, you can add a HalfAdder instance to the list. But the elements are taken as type Component, so it checks for the method that is defined in the Component class.

    Hope this clarifies your doubt.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,656
    Blog Entries
    7
    Rep Power
    21

    Default

    A little while ago this exact same problem showed up here in the forums and I suggested the Visitor pattern (or 'Double Dispatch' pattern). Maybe that little discussion can be of help to you.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Arraylist problem
    By keo in forum New To Java
    Replies: 3
    Last Post: 05-28-2010, 10:38 AM
  2. Method return type problem
    By McChill in forum New To Java
    Replies: 7
    Last Post: 05-05-2009, 09:21 PM
  3. Hey! ArrayList problem here
    By Samgetsmoney in forum New To Java
    Replies: 31
    Last Post: 02-20-2009, 12:39 AM
  4. Arraylist of Type T
    By blawknox04 in forum New To Java
    Replies: 1
    Last Post: 12-11-2008, 05:55 AM
  5. ArrayList<type> - Generics
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-14-2007, 03:21 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
  •