Working With Inheritance Concept in JSP


Inheritance

Inheritance enables programmers to define an "is-a" relationship between a class and a more specialized version of that class. For example, if a Student class extends a Person class, then it should be the case that Student is-a Person. The subclass Student builds on the functionality of the superclass Person. Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. In the Java programming language, each class is allowed to have one direct superclass, and each superclass has the potential for an unlimited number of subclasses.The syntax for creating a subclass is simple. At the beginning of your class declaration, use the extends keyword, followed by the name of the class to inherit from.

class auto extends vehicle

Constructor

A constructor creates an Object of the class that it is in by initializing all the instance variables and creating a place in memory to hold the Object. It is always used with the keyword new and then the Class name. For instance, new String(); constructs a new String object.
Sometimes in a few classes you may have to initialize a few of the variables to values apart from their predefined data type specific values. If Java initializes variables it would default them to their variable type specific values. For example you may want to initialize an integer variable to 30 or 40 based on a certain condition, when your class is created. In such a case you cannot hard code the value during variable declaration. Such kind of code can be placed inside the constructor so that the initialization would happen when the class is instantiated.

Now we are going to develop an application to describe the concept that how to work with these two concept. For this we should follow the following step.

Step 1: Creating a new project.

This is the first application, in this step we select the New Project option from File menu

create new project.jpg

Step 2:
Select Application Type

In this step we select web application option from Java web and then click on the next button.

select new web application.jpg

Step 3:
Name and Location.

In this step we give it a specific name InheritanceJsp and select a specific location for it and then click on the next button.

name and location.jpg

Step 4:
Server and Setting.

In this step we select a specific server glassfis and configure it.

server and setting.jpg

Step 5: Select Framework.

There is no need to select any framework for this aplication.

selectframework.jpg

Step 6: Create new JSP file.

In this application we create two JSP file (useinheritance.jsp and useconstructor.jsp)

create new jsp file.jpg

UseInheritance.jsp

<HTML>
     <HEAD
>
     <TITLE>Inheritance in JSP</TITLE>
     </HEAD>
     <BODY BGCOLOR="cyan">
           <H1>Inheritance in JSP</H1>
           <%!
                 javax.servlet.jsp.JspWriter localelement;
               class vehicle
                {
                 public void start() throws java.io.IOException
                {
                 localelement.println("Starting Any Vechicle<BR>");
                }
                }
               class automobile extends vehicle
               {
               public void drive() throws java.io.IOException
               {
               localelement.println("Driving The car First<BR>");
               }
               }
           %>
           <%
               localelement = out;
               out.println("Creating an automobile...<BR>");
               automobile a = new automobile();
               a.start();
               a.drive();
            %>
       </BODY>
</html>

UseConstructor.jsp

<HTML>
   <HEAD>
   <TITLE>
Constructors and Inheritance In JSP</TITLE>
   </HEAD>
   <BODY BGCOLOR="skyblue">
   <H1>Constructors and Inheritance In JSP</H1>
   <%!
        javax.servlet.jsp.JspWriter localelement;
   class A
       {
        A() throws java.io.IOException
       {
        localelement.println("In A\'s constructor<BR>");
       }
        A(String s) throws java.io.IOException
       {
        localelement.println("In A\'s String constructor<BR>");
        localelement.println(s);
       }
       }
       class B extends A
       {
       B(String s) throws java.io.IOException
       {
       super(s);
       localelement.println("In B\'s String constructor<BR>");
       localelement.println(s);
       }
       }
      %>
      <%
      localelement = out;
      B obj = new B("Hello from JSP Constructor<BR>");
      %>
      </BODY>
</HTML>


Step 7:
Compiling and running the application.

After completion of the coding part now compile it for the first time and then run it on the specific web server and find the output.

Output

UseInheritance.jsp

useinheritence.jsp.jpg

UseConstructor.jsp


Useconstructor.jsp.jpg

Resources related to this article.

Up Next
    Ebook Download
    View all
    Learn
    View all