Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Doubt regarding LIST

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Doubt regarding LIST

    Hi i have doubt regarding list .
    I have a List<Operator> type and a List<SQLExpr> type and I want to use them interchangeably like i want to add a List<SQLExpr> element to List<Operator> so i how can i do it ,explicitly casting in not working.So what should i do.

    Let me frame my question properly

    How to cast List<A> type element into List<B> type element

    somebody plz help

    Thanks


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Doubt regarding LIST

    Unless one is a sub-class of the other, you can't cast (and even then, you can only cast up, exceptions being objects viewed as a poly-morphed super object). What you can do is declare your list to hold elements that both objects initially inherited from (this is the basis for polymorphism and OOP code re-use):

    public class A
    {}
     
    public class B extends A
    {}
     
    public class C extends A
    {}
     
    public class Main
    {
         public static void main(String[] args)
         {
              List<A> list = new ArrayList<A>();
              list.add(new B());
              list.add(new C());
              list.add(new A());
         }
    }

    If you don't want to create your own super class, you can use the Object class since everything inherits from Object.

Similar Threads

  1. doubt in hibernate
    By jyothishey in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 10th, 2010, 11:43 PM
  2. My doubt may be stupid
    By kalees in forum Object Oriented Programming
    Replies: 2
    Last Post: September 22nd, 2009, 02:38 AM
  3. JComboBox doubt
    By rajaramesh.tadi in forum AWT / Java Swing
    Replies: 0
    Last Post: August 24th, 2009, 08:18 AM
  4. Which javadoc tag is used to denote a comment for a method parameter?
    By TARUN JORA in forum Java Theory & Questions
    Replies: 2
    Last Post: May 15th, 2009, 04:30 PM
  5. Replies: 2
    Last Post: October 7th, 2008, 11:03 PM