1 2 Previous Next 16 Replies Latest reply: Nov 15, 2007 9:27 AM by 807600 RSS

    abstract classes

    807600
      I'm working with an abstract class with 2 abstract methods and a few concrete methods.

      I derived a few classes from this abstract class and wrote out the abstract methods in each class.

      I wrote the main() method in the abstract class and want to call on both of the 2 methods from each class that I derived.

      How do I call the two methods from my derived classes from the abstract class.

      I was writing something like: class.method(param,param,param);
      to call each method

      What am I forgetting because this isnt compiling....
        • 1. Re: abstract classes
          807600
          Firstly, code and the error messages would be a good idea.

          Secondly, having the main method in the abstract class would be a bad idea.
          • 2. Re: abstract classes
            807600
            This isn't making much sense, at least not to me. If you have abstract class A and have class B that derives from it, and you want to call method myMethod that is overridden in class B, then you have to instantiate an object of the B class and call the method from that object. What am I missing here? If I am totally missing the point of your question, you may do well to show us your code, using code tags of course.

            One aside: You said that your main method (I'm assuming that you're taking about the entry point for your program) is with in the abstract class. That doesn't pass my "smell" test of good program design. It would be much better to get the main out of there. The whole point of an abstract class is that it be used as a building block for a concrete class. To put a main inside of it just doesn't seem right; it seems to bastardize the whole purpose of the abstract class. It would be much cleaner (at least in my opinion, for what it's worth) to create another class, say TestProgram class, that has nothing in it other than the main method.
            • 3. Re: abstract classes
              807600
              petes1234 wrote:
              That doesn't pass my "smell" test of good program design.
              Yeah, Elvis after a week on the toilet would smell better.
              • 4. Re: abstract classes
                807600
                If the derived class dosnt support one of the methods its gonna throw an UnsupportedOperationException. Can this go under the abstract method? Or under the method from the derived class that might throw it?
                • 5. Re: abstract classes
                  807600
                  corleone84 wrote:
                  If the derived class dosnt support one of the methods its gonna throw an UnsupportedOperationException. Can this go under the abstract method? Or under the method from the derived class that might throw it?
                  If you have a concrete class that extends an abstract class, it by definition has to implement all of the abstract class's abstract methods.

                  But again we need code. Til we see it, we're all just talking in circles.
                  • 6. Re: abstract classes
                    807600
                    An abstract class with a main method that invokes methods on instances of its subclasses. And now, it seems, these subclasses don't actually implement (in any meaningful sense) the abstract methods of the class they purport to extend.

                    You will get the best answers if you take the trouble to address the points raised by flounder and petes.
                    • 7. Re: abstract classes
                      807600
                      abstract class with 2 abstract methods

                      first method is an iterative solution to a problem
                      abstract void solveIter(int[] a, int first, int last);
                      second is the recursive solution
                      abstract void solveRec(int[] a, int first, int last);
                      i have 8 derived classes representing 8 different types of problems

                      each derived class can solve it using the iterative or recursive method that i complete in each class

                      however say 2 of the classes will only support either the iterative or recursive method only

                      in this case i want to throw an UnsupportedOperationException
                      • 8. Re: abstract classes
                        807600
                        Although it makes no sense and I advise against it, what is stopping you?
                        public void solveRec() throws UnsupportedOperationException {
                            throw new UnsupportedOperationException();
                        }
                        • 9. Re: abstract classes
                          807600
                          Hi there,

                          I tried to workout the problem, below is the code for the same. Please correct me if there is any design flaw.


                          Abstract Class SolveProblem*
                          package com;
                          import com.UnsupportedOperationException;
                          
                          public abstract class SolveProblem {
                               
                               abstract void solveIter(int[] a, int first, int last)throws UnsupportedIterationOperation;
                               abstract void solveRecer(int[] a, int first, int last)throws UnsupportedRecursionOperationException;
                               
                                  public void solveSelect(int[] a, int first, int last, boolean option)throws UnsupportedOperationException{
                                    
                                    if (option) {
                                         solveIter(a, first, last);
                                    }else {
                                         solveRecer(a, first, last);
                                    }
                               }
                          
                          }
                          Extended Class Problem1* which can solve the problem only by Iteration
                          package com;
                          
                          public class Problem1 extends SolveProblem {
                          
                               @Override
                               void solveIter(int[] a, int first, int last) throws UnsupportedIterationOperation{
                                    // TODO Auto-generated method stub
                                    System.out.println("iteration code goes here");
                               }
                          
                               @Override
                               void solveRecer(int[] a, int first, int last) throws UnsupportedRecursionOperationException{
                                    throw new UnsupportedRecursionOperationException();
                          
                               }
                          
                          }
                          Extended Class Problem2* which can solve the problem only by Recursion
                          package com;
                          
                          public class Problem2 extends SolveProblem {
                          
                               @Override
                               final void solveIter(int[] a, int first, int last)throws UnsupportedIterationOperation {
                                    // TODO Auto-generated method stub
                                    throw new UnsupportedIterationOperation();
                               }
                          
                               @Override
                               final void solveRecer(int[] a, int first, int last)throws UnsupportedRecursionOperationException {
                                    System.out.println("recursion code goes here");
                          
                               }
                          
                          }
                          Exception Classes
                          package com;
                          
                          public class UnsupportedOperationException extends Exception{
                               
                          }
                          package com;
                          
                          public class UnsupportedIterationOperation extends UnsupportedOperationException{
                          
                               
                          }
                          package com;
                          
                          public class UnsupportedRecursionOperationException extends UnsupportedOperationException {
                          
                          }
                          Class ProblemTest* which includes main method
                          import com.*;
                          import com.UnsupportedOperationException;
                          
                          
                          public class ProblemTest {
                          
                               public static void main(String[] args ){
                               
                                    int[] numbers = new int[10];
                                    SolveProblem problem1 =  new Problem1();
                                    SolveProblem problem2 = new Problem2();
                                    try {
                                         problem1.solveSelect(numbers, 0, 9, true);
                                         problem2.solveSelect(numbers, 0, 9, true);
                                    } catch(UnsupportedOperationException e) {
                                         System.out.println(e);
                                         
                                    } 
                               
                               }
                               
                          }
                          OUTPUT
                          iteration code goes here
                          class com.UnsupportedIterationOperation
                          • 10. Re: abstract classes
                            807600
                            You can use this code though its not the preferred design,,,,,,,, You should use main method outside the abstract class:-

                            public abstract class AbstractTest {
                            public AbstractTest() {
                            }
                            abstract void method1();
                            abstract void method2();

                            public static void main(String args[]){
                            AbstractTest d1 = new Derived1();
                            AbstractTest d2 = new Derived2();
                            d1.method1();
                            d1.method2();
                            d2.method1();
                            d2.method2();

                            d1 = new Derived1();
                            d2 = new Derived2();
                            d1.method1();
                            d1.method2();
                            d2.method1();
                            d2.method2();
                            }
                            }


                            class Derived1 extends AbstractTest {

                            public void method1() throws UnsupportedOperationException {
                            System.out.println("In derived1 method1");
                            throw new UnsupportedOperationException();
                            }

                            public void method2() {
                            System.out.println("In derived1 method2");

                            }
                            }


                            class Derived2 extends AbstractTest {
                            public void method1() {
                            System.out.println("In derived2 method1");
                            }

                            public void method2() {
                            System.out.println("In derived2 method2");

                            }
                            }
                            • 11. Re: abstract classes
                              807600
                              You can use this code though its not the preferred design,,,,,,,, You should use main method outside the abstract class:-
                              Could you please let me know where the hell I am using main method inside the abstract class???
                              • 12. Re: abstract classes
                                807600
                                marathaWarrior wrote:
                                You can use this code though its not the preferred design,,,,,,,, You should use main method outside the abstract class:-
                                Could you please let me know where the hell I am using main method inside the abstract class???
                                Easy there, I think the replier was talking about an earlier post, not your latest.

                                Regards of the most select and luxury kind, and a tip 'o the cap,
                                j
                                • 13. Re: abstract classes
                                  807600
                                  jToohey wrote:
                                  marathaWarrior wrote:
                                  You can use this code though its not the preferred design,,,,,,,, You should use main method outside the abstract class:-
                                  Could you please let me know where the hell I am using main method inside the abstract class???
                                  Easy there, I think the replier was talking about an earlier post, not your latest.

                                  Regards of the most select and luxury kind, and a tip 'o the cap,
                                  j
                                  The newbies are getting a bit touchy it seems.
                                  • 14. Re: abstract classes
                                    807600
                                    petes1234 wrote:
                                    jToohey wrote:
                                    marathaWarrior wrote:
                                    You can use this code though its not the preferred design,,,,,,,, You should use main method outside the abstract class:-
                                    Could you please let me know where the hell I am using main method inside the abstract class???
                                    Easy there, I think the replier was talking about an earlier post, not your latest.

                                    Regards of the most select and luxury kind, and a tip 'o the cap,
                                    j
                                    The newbies are getting a bit touchy it seems.
                                    Hey Petes, Sorry for the language ( Actually I belong to Non English background ) .
                                    I really appreciate your contribution to these forums
                                    1 2 Previous Next