1 2 Previous Next 16 Replies Latest reply: Sep 29, 2009 10:45 AM by 807580 RSS

    Constructor of abstract class

    807580
      I was reading:

      [http://java.sun.com/javaee/5/docs/api/javax/servlet/http/HttpServlet.html|http://java.sun.com/javaee/5/docs/api/javax/servlet/http/HttpServlet.html]

      And under Constructor summary, it says:

      "Does nothing, because this is an abstract class."

      Is that really a valid statement? I accept that the constructor does nothing and I accept that it's an abstract class, but I have a hard time accepting that the constructor does nothing because it is an abstract class. Why couldn't a constructor of an abstract class do something to initialize the object, like getting a context?

      Small example:
      public abstract class Abstract
      {
          public int i = 0;
          Abstract()
          {
              i = 7;
          }
          public int getI()
          {
              return i;
          }
      }
      
      public class Concrete extends Abstract
      {
          public static void main(String[] args)
          {
              Concrete c = new Concrete();
              System.out.println(c.getI());
          }
      }
      This prints out "7", as expected, since the constructor of the abstract class "does something".
        • 1. Re: Constructor of abstract class
          JoachimSauer
          itupjalu wrote:
          Is that really a valid statement? I accept that the constructor does nothing and I accept that it's an abstract class, but I have a hard time accepting that the constructor does nothing because it is an abstract class. Why couldn't a constructor of an abstract class do something to initialize the object, like getting a context?
          You are right. The reason that constructor does nothing is not that it is on an abstract class.

          The author probably meant to convey the notion that all initialization needs to happen in the concrete super class and that authors of those concrete super classes must not depend on any initialization being done by this constructor.

          But strictly speaking the sentence "Does nothing, because this is an abstract class" is wrong.
          • 2. Re: Constructor of abstract class
            dcminter
            It could but it doesn't.

            "Does A because B" can mean "A is an inevitable consequence of B" but it can also mean, as here, that the justification for A is the fact B.

            I don't bet on the lottery because it's a mug's game (the expected return on an investment is less than the investment itself). The fact that it is a mug's game, however, does not imply the logical consequence that I cannot bet on the lottery.
            • 3. Re: Constructor of abstract class
              800580
              Hi,

              According to [ http://java.sun.com/javaee/5/docs/api/javax/servlet/http/HttpServlet.html]

              The constructor of class HttpServlet is an abstract class that does nothing. It nowhere relates to application that you have sent.

              Abstract class is only a partially implemented class thats it.

              Thanks and Regards,
              Vikash Anand
              • 4. Re: Constructor of abstract class
                dcminter
                Vikash, your answer adds nothing to this discussion. Please consider not replying when you don't fully understand what the OP is asking. The OP was fully aware of what you're pointing out.

                Edited by: dcminter on 29-Sep-2009 14:13
                • 5. Re: Constructor of abstract class
                  807580
                  itupjalu wrote:
                  I was reading:

                  [http://java.sun.com/javaee/5/docs/api/javax/servlet/http/HttpServlet.html|http://java.sun.com/javaee/5/docs/api/javax/servlet/http/HttpServlet.html]

                  And under Constructor summary, it says:

                  "Does nothing, because this is an abstract class."
                  Is a correct statement, but a bit misleading, as you point out. The designer of the class wrote the constructor such that it did nothing, because the class is abstract and (s)he decided it need not do anything. But it's not to say that constructors of abstract classes should always do nothing. Definitely a misleading statement. Kudos to you, for correctly challenging something that many people would just accept as gospel
                  • 6. Re: Constructor of abstract class
                    807580
                    Vikash.SunJava wrote:
                    Abstract class is only a partially implemented class thats it.
                    Rubbish.
                    • 7. Re: Constructor of abstract class
                      807580
                      Vikash.SunJava wrote:
                      Hi,

                      According to [ http://java.sun.com/javaee/5/docs/api/javax/servlet/http/HttpServlet.html]

                      The constructor of class HttpServlet is an abstract class that does nothing. It nowhere relates to application that you have sent.

                      Abstract class is only a partially implemented class thats it.

                      Thanks and Regards,
                      Vikash Anand
                      Vikash, this is not the first time, please go to coderanch.com. There are enough others who keeps posting incorrect answers.

                      Or if you want to stay here, please learn Java properly first and refrain you from answering unless you understand the code and the problem.
                      • 8. Re: Constructor of abstract class
                        800580
                        Hi georgemc,

                        The question is:
                        "Is that really a valid statement? I accept that the constructor does nothing and I accept that it's an abstract class,
                        but I have a hard time accepting that the constructor does nothing because it is an abstract class.
                        Why couldn't a constructor of an abstract class do something to initialize the object, like getting a context?"

                        The answer is:
                        According to the link it means only that the constructor of HttpServlet does nothing. It does not mean that if we write any constructor in an abstract class it does nothing.
                        Yes we can initialize the object in a constructor or can initialise a variable.

                        and yes the description of constructor of HttpServlet is correct.

                        The originator of the question has imported concepts from the link he has provided and created a program and has a confusion.

                        I request all to please clarify his doubts

                        Thanks,
                        Vikash Anand.
                        • 9. Re: Constructor of abstract class
                          800580
                          Please read:

                          and yes the description of constructor of HttpServlet is correct.

                          as

                          and yes the description of constructor of HttpServlet is incorrect.
                          • 10. Re: Constructor of abstract class
                            dcminter
                            Vikash,

                            Your first answer failed to address the question that the OP asked. Your update answers the question less clearly than the other answers given. As such your answer detracts from the overall value in this thread.

                            In other threads you have given answers that are wrong. Please think before posting; if you are at all uncertain of your facts, or of what the OP is asking, please refrain from posting an answer - or ask questions to clarify the situation.
                            • 11. Re: Constructor of abstract class
                              807580
                              Thank you for answering!

                              I think I get the general idea that you are presenting, but does:

                              "Does nothing, because this is an abstract class."

                              add anything more than

                              "Does nothing."

                              in terms of justification? (I humbly realize english is not my first language, so I hope I use the correct terminology here.)
                              • 12. Re: Constructor of abstract class
                                dcminter
                                does: "Does nothing, because this is an abstract class." add anything more than "Does nothing." in terms of justification?
                                Not really.
                                • 13. Re: Constructor of abstract class
                                  807580
                                  Vikash.SunJava wrote:
                                  Hi georgemc,

                                  The question is:
                                  "Is that really a valid statement? I accept that the constructor does nothing and I accept that it's an abstract class,
                                  but I have a hard time accepting that the constructor does nothing because it is an abstract class.
                                  Why couldn't a constructor of an abstract class do something to initialize the object, like getting a context?"

                                  The answer is:
                                  According to the link it means only that the constructor of HttpServlet does nothing. It does not mean that if we write any constructor in an abstract class it does nothing.
                                  Yes we can initialize the object in a constructor or can initialise a variable.

                                  and yes the description of constructor of HttpServlet is correct.

                                  The originator of the question has imported concepts from the link he has provided and created a program and has a confusion.

                                  I request all to please clarify his doubts

                                  Thanks,
                                  Vikash Anand.
                                  No. You have addressed specifically the constructor of HttpServlet, not the overall misunderstanding the OP is addressing. In all fairness, the OP has a better grasp of both the question and the answer than you do. It's good that you wish to contribute here, but it is not fair on those asking questions to be given unhelpful, misleading and flat-out wrong answers. Please refrain from answering questions simply for the sake of it, and only do so when you have something of value to offer

                                  In response to your request, the OP is challenging the somewhat ambiguous language of the documentation, which seems to imply that constructors of abstract classes necessarily do nothing, because of their abstract nature. That's wrong, and the OP is right to say so. He was asking for a second opinion, not a regurgitation of the exact documentation he found ambiguous
                                  • 14. Re: Constructor of abstract class
                                    807580
                                    dcminter wrote:
                                    does: "Does nothing, because this is an abstract class." add anything more than "Does nothing." in terms of justification?
                                    Not really.
                                    Moreover, I posit that the former is in fact misleading.
                                    1 2 Previous Next