Introduction
Java provides a special
type of class called an abstract class which helps us to organize our classes
based on common methods. An abstract class lets you put the common method names
in one abstract class without having to write the actual implementation code. In
another words we can say that an abstract class is a class that is declared by
using the abstract keyword.
It may or may not have abstract methods. Abstract classes cannot be
instantiated, but they can be extended into sub-classes.
Why We Use Abstract Classes: There are situations in which you will want
to define a super class that declares the structure of a given abstraction
without providing a complete implementation of every method. That is, sometimes
you will want to create a super class that only defines a generalized form that
will be shared by all of its subclasses, leaving it to each subclass to fill in
the details. Such a class determines the nature of the methods that the
subclasses must implement. One way this situation can occur is when a superclass
is unable to create a meaningful implementation for a method. This is the case
with the class Figure used in the preceding example. The definition of area( )
is simply a placeholder. It will not compute and display the area of any type of
object.
As you will see as you create your own class libraries, it is not uncommon for a
method to have no meaningful definition in the context of its superclass. You
can handle this situation two ways. One way, as shown in the previous example,
is to simply have it report a warning message. While this approach can be useful
in certain situations—such as debugging—it is not usually appropriate. You may
have methods which must be overridden by the subclass in order for the subclass
to have any meaning. Consider the class Triangle. It has no meaning if area( )
is not defined. In this case, you want some way to ensure that a subclass does,
indeed, override all necessary methods. Java's solution to this problem is the
abstract method.
An application of abstract class with JSP:
Here we are going to prepare an application of abstract class with JSP , we
using NetBeans IDE(7.0) for it.
Step 1 : Creating a new project: In this
step we click on the file menu and select New Project option:
Step 2 : Choosing Project: In this step we
select the project type. We select java web application and click on the Next
button:
Step 3 : Name and Location: In this step
we give it a specific name UsingAbstractionJsp and also give it a specific
loccation:
Step 4 : Select a Server and provide it a
setting: In this step we select GlassFish Server 3.1 version (java EE 6 web)
and Set the context path:
Step 5 : Select Framework: There is no
need to select any framework just click on the Finish button:
Step 5 : Choose File: In this step we
choose an jsp file and starts coding on it:
Index.jsp File: In this file we create
an abstact class M, it has a abstract method. We provide body to it in another
class N.
<HTML>
<HEAD>
<TITLE>Using Abstract Classes In JSP</TITLE>
</HEAD>
<BODY bgcolor="green">
<H1>Using Abstract Classes In JSP</H1>
<%!
javax.servlet.jsp.JspWriter
localOut;
abstract class M
{
abstract String getText() throws
java.io.IOException;
public void printem() throws
java.io.IOException
{
localOut.println(getText());
}
}
class N extends M
{
String getText() throws
java.io.IOException
{
return "Hello from JSP!<br/> My Name Is:Vikas<br/>My Title
Is:Mishra";
}
}
%>
<%
localOut = out;
N nObject = new N();
nObject.printem();
%>
</BODY>
</HTML>
Step 6 : Compile and Run File: After completion Compile it and run it on the
any browser :
Output: Then it will give the following output: