0

Good day everybody.

I have some confusion about abstract superclass and subclasses in servlets:

I have abstract servlet superclass:

public abstract class CatalogPage extends HttpServlet {


    public CatalogPage() {

        super();

    }

    private CatalogItem[] items;

    private String[] itemsID;

    private String title;



    public void setItems(String[] itemsID) {

        this.itemsID = itemsID;

        items = new CatalogItem[itemsID.length];

        for(int i=0; i<items.length; i++) {

                items[i] = Catalog.getItem(itemsID[i]);

            }   
    }



    public void setTitle(String title) {
        this.title = title;
    }



    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

            if (items == null) {

                response.sendError(response.SC_NOT_FOUND, "Missing Items");         
                return;
            }

            response.setContentType("text/html");

            PrintWriter out = response.getWriter();
            String docType =
              "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
              "Transitional//EN\">\n";
            out.println(docType +
                        "<HTML>\n" +
                        "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" +
                        "<BODY BGCOLOR=\"#FDF5E6\">\n" +
                        "<H1 ALIGN=\"CENTER\">" + title + "</H1>");

            CatalogItem item;

            for(int i=0; i<items.length; i++) {

                    item = items[i];


            if(item == null) {

                out.println("<FONT COLOR=\"RED\">" +
                        "Unknown item ID " + itemsID[i] +
                        "</FONT>");


                    }  else {


                out.println();
                String formURL = "OrderPage";

                formURL = response.encodeURL(formURL);

                out.println
                  ("<FORM ACTION=\"" + formURL + "\">\n" +
                   "<INPUT TYPE=\"HIDDEN\" NAME=\"itemID\" " +
                   "        VALUE=\"" + item.getItemID() + "\">\n" +
                   "<H2>" + item.getShortDescription() +
                   " ($" + item.getCost() + ")</H2>\n" +
                   item.getLongDescription() + "\n" +
                   "<P>\n<CENTER>\n" +
                   "<INPUT TYPE=\"SUBMIT\" " +
                   "VALUE=\"Add to Shopping Cart\">\n" +
                   "</CENTER>\n<P>\n</FORM>");

                    out.println("<HR>\n</BODY></HTML>");

            }

        }

    }

}

And of abstract superclass, subclass:

@WebServlet("/KidsCatalogPage")
public class KidsBooksPage extends CatalogPage {

    public void init() {

            String[] kbp = {"lewis001", "alexander001", "rowling001"};  

            setItems(kbp);
            setTitle("All-Time Best Children's Fantasy Books");

    }

}

If I invoke for subclass KidsBooksPage I know it do initialisation init() method first, but the question is:

What make my subclass KidsBooksPage invoke abstract super class CatalogPage . How it works?? Can Understand it. Please explain to me.

Thank you.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Vad
  • 75
  • 4
  • 10
  • You don't *invoke* a class - you *invoke* a method. Which method are you talking about? Or are you talking about a constructor? It's really unclear what you're asking. – Jon Skeet Oct 16 '13 at 16:00
  • I'm talking about doGet() method. There is no doGet in KidsBooksPage how it invoke doGet() from CatalogPage if I only work with KidsBooksPage.I know that request in this case will invoke doGet but doGet() in abstract class which is superclass and it's mean that abstract class will be invoked automatically when we work with KidsBooksPage class?? – Vad Oct 16 '13 at 16:15
  • If you want to invoke super implementation of init method, you can make 'super.init()' call in the subclass. – ovunccetin Oct 16 '13 at 16:19
  • That has nothing to do with servlets - it's just the normal behaviour of inheritance... – Jon Skeet Oct 16 '13 at 16:29

1 Answers1

0

As Jon Skeet said in comments, this is about a core concept (Inheritance) in Object-Oriented programming (OOP), has nothing to do with Servlets specifically (except maybe your homework ), and is not really a question for StackOverflow.com.

If you understand conventional programming and how conventional compiling and linking predetermine at compile-time exactly what bits will be executed at run-time, and therefore wonder how execution can seem to "jump around" between classes, then you need to learn about 'late binding' & 'dynamic dispatch', the special sauce that makes OOP so powerful.

'abstract' is a bit of distraction regarding your question. Simply means that a concrete subclass is required, but without abstract your question remains the same.

Try reading up on Inheritance and Late Binding.

What Is Inheritance?

Inheritance

Late Binding

Dynamic Dispatch

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154