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
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
Step 2: Select Application Type
In this step we select web application option from Java web and then click
on the next button.
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.
Step 4: Server and Setting.
In this step we select a specific server glassfis and configure it.
Step 5: Select Framework.
There is no need to select any framework for this aplication.
Step 6: Create new JSP file.
In this application we create two JSP file (useinheritance.jsp
<TITLE>Inheritance in JSP</TITLE>
public void start() throws
localelement.println("Starting Any Vechicle<BR>");
class automobile extends vehicle
public void drive() throws
localelement.println("Driving The car First<BR>");
localelement = out;
out.println("Creating an automobile...<BR>");
automobile a = new automobile();
<TITLE>Constructors and Inheritance In JSP</TITLE>
<H1>Constructors and Inheritance In JSP</H1>
A() throws java.io.IOException
A(String s) throws java.io.IOException
class B extends A
B(String s) throws java.io.IOException
localelement.println("In B\'s String
localelement = out;
B obj = new B("Hello from JSP
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.
Resources related to this article.