Question

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.

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top