INTRODUCTION:
Hey folks.. In this post we are going to look about abstract
class and its uses in java. Let’s jump to the post.
ABSTRACT CLASS IN JAVA:
Abstract class is
similar to interface . If you want to know about interface, have a look at this
post Interface in java
The reason we use this abstract class is that ,
When we create an abstract class, the class will have the
method declarations ie. We will say that we are going to use this method in
some other child classes . We won’t provide an implementation for the abstract
methods in the abstract classes.
The class which has an “ABSTRACT” keyword is called as
“abstract class”.
All the member functions inside the abstract class must also
be proceeded with “abstract “ keyword.
EXAMPLE:
abstract class AbstExample
{
abstract void add( );
abstract void sub( );
}
Class sum extends AbstExample
{
void add( )
{
int a= 8;
int b=9;
int sumvar = a +b;
System.out.println(“the sum is “ + sumvar ) ;
}
void sub( )
{
int a = 8;
int b = 6;
int diffvar = a –b;
System.out.println(“the diff is “ +diffvar );
}
public static void main(String args[] )
{
Sum s = new sum();
s.add( );
s.sub ( );
}
OUTPUT:
the sum is 17
the diff is 2
EXPLANATION:
As I said above.. the abstract class just contains
declaration and actual method definition is given in the class that extends the
abstract class.
But Abstract class is similar to interface . The main difference is, a class can extend only
one abstract class but in interface , a class can implement more than one
interface class.
IMPORTANT POINTS ABOUT ABSTRACT CLASS :
An abstract class can have CONSTRUCTORS inside it.
An abstract class cannot be instantiated .. meaning the
abstract class cannot be invoked separately using “new”keyword but WE CAN INVOKE THE ABSTRACT class if it
has “PUBLIC STATIC VOID MAIN “ inside it.
An abstract class is extended by another class using
“EXTENDS” keyword , whereas in INTERFACE we will use “IMPLEMENTS “ keyword
We can declare the member of abstract class as PRIVATE,
DEFAULT, PROTECTED OR PUBLIC . It can also be static but whereas in INTERFACE
, we cannot have private or protected methods. Interface methods cannot be
static too. It is by default “public”.
In abstract class we can have concrete methods along with
abstract methods..
EXAMPLE:
abstract class abs
{
abstract void add( ); // abstract method
public void sub( ) //
concrete method
{
int sub =9 -8;
System.out.println(“the sub is “ +sub );
}
}
So as shown above, the abstract class can have both abstract
method as well as concrete method. A method with full implementation is called
as Concrete method . An interface cannot have concrete method inside it.
Abstract class can have non final and non static variables
but wheras in interface, variables declared are always static and final.
WHEN TO USE INTERFACE AND WHEN TO USE ABSTRACT CLASS ?
If you have a class that needs one or more abstracted class
( INTERFACE OR ABSTRACT CLASS ) to be extended or implemented then go for
INTERFACE as interface will let the child class to implement one or more class
but,
If you don’t want more than one class to be extended then go
for ABSTRACT CLASS.
CONCLUSION :
So.. that’s it for
this post. Please hit like button and drop your comments below . Request for
the post you wish to learn is welcomed . Support me to post more .. !! Stay
tuned for more post . !! Thanks for reading .. Have a peaceful day ahead :)
No comments :
Post a Comment