Results 1 to 5 of 5
- 12-18-2010, 11:49 AM #1
Member
- Join Date
- May 2010
- Posts
- 2
- Rep Power
- 0
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
-
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!
- 12-18-2010, 01:09 PM #3
Member
- Join Date
- May 2010
- Posts
- 2
- Rep Power
- 0
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(); }
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(); } }
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 ?
- 12-20-2010, 03:52 PM #4
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.
- 12-20-2010, 05:26 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,421
- Blog Entries
- 7
- Rep Power
- 26
Similar Threads
-
Arraylist problem
By keo in forum New To JavaReplies: 3Last Post: 05-28-2010, 10:38 AM -
Method return type problem
By McChill in forum New To JavaReplies: 7Last Post: 05-05-2009, 09:21 PM -
Hey! ArrayList problem here
By Samgetsmoney in forum New To JavaReplies: 31Last Post: 02-20-2009, 12:39 AM -
Arraylist of Type T
By blawknox04 in forum New To JavaReplies: 1Last Post: 12-11-2008, 05:55 AM -
ArrayList<type> - Generics
By Java Tip in forum Java TipReplies: 0Last Post: 11-14-2007, 03:21 PM
Bookmarks