How to use the methods of a subclass of a superclass in another class in which I created a ArrayList?











up vote
0
down vote

favorite












How to use the methods of a subclass of a superclass in another class in which I created a ArrayList?



I created a ArrayList of a superclass in another class. This superclass has three subclasses, each with their own methods. How do I use those methods?



And also, how can I use the instance variables of those subclasses?










share|improve this question


























    up vote
    0
    down vote

    favorite












    How to use the methods of a subclass of a superclass in another class in which I created a ArrayList?



    I created a ArrayList of a superclass in another class. This superclass has three subclasses, each with their own methods. How do I use those methods?



    And also, how can I use the instance variables of those subclasses?










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      How to use the methods of a subclass of a superclass in another class in which I created a ArrayList?



      I created a ArrayList of a superclass in another class. This superclass has three subclasses, each with their own methods. How do I use those methods?



      And also, how can I use the instance variables of those subclasses?










      share|improve this question













      How to use the methods of a subclass of a superclass in another class in which I created a ArrayList?



      I created a ArrayList of a superclass in another class. This superclass has three subclasses, each with their own methods. How do I use those methods?



      And also, how can I use the instance variables of those subclasses?







      java






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 21:45









      user10610048

      676




      676
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          Because you used the name of the superclass as your generic type, you are using the interface that the superclass provides in the ArrayList.



          You can add subclass types to the list, but you cannot safely use unique methods introduced in the subclasses without first checking exactly which subclass the object is, then making a cast to the subclass.



          Here is an example:



          public class A {

          public void methodFromA() {
          System.out.println("From class A");
          }

          }


          public class B extends A {

          public void methodFromB() {
          System.out.println("From class B");
          }

          }

          public class C extends A {

          public void methodFromC() {
          System.out.println("From class C");
          }

          }


          and here are these classes in use, using an ArrayList with the superclass type:



          public static void main(String args) {

          List<A> myList = new ArrayList<>();

          A object1 = new A();
          B object2 = new B();
          C object3 = new C();

          myList.add(object1);
          myList.add(object2);
          myList.add(object3);

          for (A object : myList) {
          object.methodFromA();

          if (object instanceof B) {
          B castedObject = (B) object;
          castedObject.methodFromB();
          }
          else if (object instanceof C) {
          C castedObject = (C) object;
          castedObject.methodFromC();
          }
          }
          }


          the resulting output:



          From class A
          From class A
          From class B
          From class A
          From class C


          As far as using instance variables, they should be accessed by public getters and setters, used after checking and casting, as demonstrated above.






          share|improve this answer






























            up vote
            0
            down vote













            First of all if you are not able to call super class method which will reflect to concrete subclass it means your design isn't good, you should thing it again.



            But, anyway you can get item from arraylist and check it using instanceof like:



            if (item instanceof FirstSubClass){
            FirstSubClass f = (FirstSubClass) item;
            f.methodInFirst();
            } else if (...)
            .....





            share|improve this answer























            • And how can I use the instance variables of that specific subclass?
              – user10610048
              Nov 11 at 22:01










            • Just cast it. I added logic in my code
              – Vasif
              Nov 11 at 22:05










            • If I created a ArrayList of superclass type how can I add objects of subclass type to that ArrayList if they have different constructors (with other parameters).
              – user10610048
              Nov 11 at 22:10






            • 1




              the constructor of an object must be called before you can add the object to the list. therefore, you can use whatever constructor you want at the time of the creation of the subclass object.
              – 4dc0
              Nov 11 at 22:12












            • It doesn't matter, feel free to create any kind of objects using various constructors, main point is all your objects which u will add to list should be subclass of Superclass
              – Vasif
              Nov 11 at 22:14











            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253558%2fhow-to-use-the-methods-of-a-subclass-of-a-superclass-in-another-class-in-which-i%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote



            accepted










            Because you used the name of the superclass as your generic type, you are using the interface that the superclass provides in the ArrayList.



            You can add subclass types to the list, but you cannot safely use unique methods introduced in the subclasses without first checking exactly which subclass the object is, then making a cast to the subclass.



            Here is an example:



            public class A {

            public void methodFromA() {
            System.out.println("From class A");
            }

            }


            public class B extends A {

            public void methodFromB() {
            System.out.println("From class B");
            }

            }

            public class C extends A {

            public void methodFromC() {
            System.out.println("From class C");
            }

            }


            and here are these classes in use, using an ArrayList with the superclass type:



            public static void main(String args) {

            List<A> myList = new ArrayList<>();

            A object1 = new A();
            B object2 = new B();
            C object3 = new C();

            myList.add(object1);
            myList.add(object2);
            myList.add(object3);

            for (A object : myList) {
            object.methodFromA();

            if (object instanceof B) {
            B castedObject = (B) object;
            castedObject.methodFromB();
            }
            else if (object instanceof C) {
            C castedObject = (C) object;
            castedObject.methodFromC();
            }
            }
            }


            the resulting output:



            From class A
            From class A
            From class B
            From class A
            From class C


            As far as using instance variables, they should be accessed by public getters and setters, used after checking and casting, as demonstrated above.






            share|improve this answer



























              up vote
              0
              down vote



              accepted










              Because you used the name of the superclass as your generic type, you are using the interface that the superclass provides in the ArrayList.



              You can add subclass types to the list, but you cannot safely use unique methods introduced in the subclasses without first checking exactly which subclass the object is, then making a cast to the subclass.



              Here is an example:



              public class A {

              public void methodFromA() {
              System.out.println("From class A");
              }

              }


              public class B extends A {

              public void methodFromB() {
              System.out.println("From class B");
              }

              }

              public class C extends A {

              public void methodFromC() {
              System.out.println("From class C");
              }

              }


              and here are these classes in use, using an ArrayList with the superclass type:



              public static void main(String args) {

              List<A> myList = new ArrayList<>();

              A object1 = new A();
              B object2 = new B();
              C object3 = new C();

              myList.add(object1);
              myList.add(object2);
              myList.add(object3);

              for (A object : myList) {
              object.methodFromA();

              if (object instanceof B) {
              B castedObject = (B) object;
              castedObject.methodFromB();
              }
              else if (object instanceof C) {
              C castedObject = (C) object;
              castedObject.methodFromC();
              }
              }
              }


              the resulting output:



              From class A
              From class A
              From class B
              From class A
              From class C


              As far as using instance variables, they should be accessed by public getters and setters, used after checking and casting, as demonstrated above.






              share|improve this answer

























                up vote
                0
                down vote



                accepted







                up vote
                0
                down vote



                accepted






                Because you used the name of the superclass as your generic type, you are using the interface that the superclass provides in the ArrayList.



                You can add subclass types to the list, but you cannot safely use unique methods introduced in the subclasses without first checking exactly which subclass the object is, then making a cast to the subclass.



                Here is an example:



                public class A {

                public void methodFromA() {
                System.out.println("From class A");
                }

                }


                public class B extends A {

                public void methodFromB() {
                System.out.println("From class B");
                }

                }

                public class C extends A {

                public void methodFromC() {
                System.out.println("From class C");
                }

                }


                and here are these classes in use, using an ArrayList with the superclass type:



                public static void main(String args) {

                List<A> myList = new ArrayList<>();

                A object1 = new A();
                B object2 = new B();
                C object3 = new C();

                myList.add(object1);
                myList.add(object2);
                myList.add(object3);

                for (A object : myList) {
                object.methodFromA();

                if (object instanceof B) {
                B castedObject = (B) object;
                castedObject.methodFromB();
                }
                else if (object instanceof C) {
                C castedObject = (C) object;
                castedObject.methodFromC();
                }
                }
                }


                the resulting output:



                From class A
                From class A
                From class B
                From class A
                From class C


                As far as using instance variables, they should be accessed by public getters and setters, used after checking and casting, as demonstrated above.






                share|improve this answer














                Because you used the name of the superclass as your generic type, you are using the interface that the superclass provides in the ArrayList.



                You can add subclass types to the list, but you cannot safely use unique methods introduced in the subclasses without first checking exactly which subclass the object is, then making a cast to the subclass.



                Here is an example:



                public class A {

                public void methodFromA() {
                System.out.println("From class A");
                }

                }


                public class B extends A {

                public void methodFromB() {
                System.out.println("From class B");
                }

                }

                public class C extends A {

                public void methodFromC() {
                System.out.println("From class C");
                }

                }


                and here are these classes in use, using an ArrayList with the superclass type:



                public static void main(String args) {

                List<A> myList = new ArrayList<>();

                A object1 = new A();
                B object2 = new B();
                C object3 = new C();

                myList.add(object1);
                myList.add(object2);
                myList.add(object3);

                for (A object : myList) {
                object.methodFromA();

                if (object instanceof B) {
                B castedObject = (B) object;
                castedObject.methodFromB();
                }
                else if (object instanceof C) {
                C castedObject = (C) object;
                castedObject.methodFromC();
                }
                }
                }


                the resulting output:



                From class A
                From class A
                From class B
                From class A
                From class C


                As far as using instance variables, they should be accessed by public getters and setters, used after checking and casting, as demonstrated above.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 11 at 22:08

























                answered Nov 11 at 21:50









                4dc0

                42539




                42539
























                    up vote
                    0
                    down vote













                    First of all if you are not able to call super class method which will reflect to concrete subclass it means your design isn't good, you should thing it again.



                    But, anyway you can get item from arraylist and check it using instanceof like:



                    if (item instanceof FirstSubClass){
                    FirstSubClass f = (FirstSubClass) item;
                    f.methodInFirst();
                    } else if (...)
                    .....





                    share|improve this answer























                    • And how can I use the instance variables of that specific subclass?
                      – user10610048
                      Nov 11 at 22:01










                    • Just cast it. I added logic in my code
                      – Vasif
                      Nov 11 at 22:05










                    • If I created a ArrayList of superclass type how can I add objects of subclass type to that ArrayList if they have different constructors (with other parameters).
                      – user10610048
                      Nov 11 at 22:10






                    • 1




                      the constructor of an object must be called before you can add the object to the list. therefore, you can use whatever constructor you want at the time of the creation of the subclass object.
                      – 4dc0
                      Nov 11 at 22:12












                    • It doesn't matter, feel free to create any kind of objects using various constructors, main point is all your objects which u will add to list should be subclass of Superclass
                      – Vasif
                      Nov 11 at 22:14















                    up vote
                    0
                    down vote













                    First of all if you are not able to call super class method which will reflect to concrete subclass it means your design isn't good, you should thing it again.



                    But, anyway you can get item from arraylist and check it using instanceof like:



                    if (item instanceof FirstSubClass){
                    FirstSubClass f = (FirstSubClass) item;
                    f.methodInFirst();
                    } else if (...)
                    .....





                    share|improve this answer























                    • And how can I use the instance variables of that specific subclass?
                      – user10610048
                      Nov 11 at 22:01










                    • Just cast it. I added logic in my code
                      – Vasif
                      Nov 11 at 22:05










                    • If I created a ArrayList of superclass type how can I add objects of subclass type to that ArrayList if they have different constructors (with other parameters).
                      – user10610048
                      Nov 11 at 22:10






                    • 1




                      the constructor of an object must be called before you can add the object to the list. therefore, you can use whatever constructor you want at the time of the creation of the subclass object.
                      – 4dc0
                      Nov 11 at 22:12












                    • It doesn't matter, feel free to create any kind of objects using various constructors, main point is all your objects which u will add to list should be subclass of Superclass
                      – Vasif
                      Nov 11 at 22:14













                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    First of all if you are not able to call super class method which will reflect to concrete subclass it means your design isn't good, you should thing it again.



                    But, anyway you can get item from arraylist and check it using instanceof like:



                    if (item instanceof FirstSubClass){
                    FirstSubClass f = (FirstSubClass) item;
                    f.methodInFirst();
                    } else if (...)
                    .....





                    share|improve this answer














                    First of all if you are not able to call super class method which will reflect to concrete subclass it means your design isn't good, you should thing it again.



                    But, anyway you can get item from arraylist and check it using instanceof like:



                    if (item instanceof FirstSubClass){
                    FirstSubClass f = (FirstSubClass) item;
                    f.methodInFirst();
                    } else if (...)
                    .....






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 11 at 22:04

























                    answered Nov 11 at 21:57









                    Vasif

                    21815




                    21815












                    • And how can I use the instance variables of that specific subclass?
                      – user10610048
                      Nov 11 at 22:01










                    • Just cast it. I added logic in my code
                      – Vasif
                      Nov 11 at 22:05










                    • If I created a ArrayList of superclass type how can I add objects of subclass type to that ArrayList if they have different constructors (with other parameters).
                      – user10610048
                      Nov 11 at 22:10






                    • 1




                      the constructor of an object must be called before you can add the object to the list. therefore, you can use whatever constructor you want at the time of the creation of the subclass object.
                      – 4dc0
                      Nov 11 at 22:12












                    • It doesn't matter, feel free to create any kind of objects using various constructors, main point is all your objects which u will add to list should be subclass of Superclass
                      – Vasif
                      Nov 11 at 22:14


















                    • And how can I use the instance variables of that specific subclass?
                      – user10610048
                      Nov 11 at 22:01










                    • Just cast it. I added logic in my code
                      – Vasif
                      Nov 11 at 22:05










                    • If I created a ArrayList of superclass type how can I add objects of subclass type to that ArrayList if they have different constructors (with other parameters).
                      – user10610048
                      Nov 11 at 22:10






                    • 1




                      the constructor of an object must be called before you can add the object to the list. therefore, you can use whatever constructor you want at the time of the creation of the subclass object.
                      – 4dc0
                      Nov 11 at 22:12












                    • It doesn't matter, feel free to create any kind of objects using various constructors, main point is all your objects which u will add to list should be subclass of Superclass
                      – Vasif
                      Nov 11 at 22:14
















                    And how can I use the instance variables of that specific subclass?
                    – user10610048
                    Nov 11 at 22:01




                    And how can I use the instance variables of that specific subclass?
                    – user10610048
                    Nov 11 at 22:01












                    Just cast it. I added logic in my code
                    – Vasif
                    Nov 11 at 22:05




                    Just cast it. I added logic in my code
                    – Vasif
                    Nov 11 at 22:05












                    If I created a ArrayList of superclass type how can I add objects of subclass type to that ArrayList if they have different constructors (with other parameters).
                    – user10610048
                    Nov 11 at 22:10




                    If I created a ArrayList of superclass type how can I add objects of subclass type to that ArrayList if they have different constructors (with other parameters).
                    – user10610048
                    Nov 11 at 22:10




                    1




                    1




                    the constructor of an object must be called before you can add the object to the list. therefore, you can use whatever constructor you want at the time of the creation of the subclass object.
                    – 4dc0
                    Nov 11 at 22:12






                    the constructor of an object must be called before you can add the object to the list. therefore, you can use whatever constructor you want at the time of the creation of the subclass object.
                    – 4dc0
                    Nov 11 at 22:12














                    It doesn't matter, feel free to create any kind of objects using various constructors, main point is all your objects which u will add to list should be subclass of Superclass
                    – Vasif
                    Nov 11 at 22:14




                    It doesn't matter, feel free to create any kind of objects using various constructors, main point is all your objects which u will add to list should be subclass of Superclass
                    – Vasif
                    Nov 11 at 22:14


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253558%2fhow-to-use-the-methods-of-a-subclass-of-a-superclass-in-another-class-in-which-i%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Guess what letter conforming each word

                    Port of Spain

                    Run scheduled task as local user group (not BUILTIN)