What is an Abstract Class

What is an Abstract Class ?

Abstract Class is one of the important concepts in java what all software professionals and all computer science students should be strong enough to code in. Abstract Class in Java is also an important question asked in majority of the interviews and in exams as well. Since guys find hard and quiet confusing in this concept of object oriented programming studies, I described the easiest way on the exact concept with examples in this page. Let me share my knowledge on this with you.

An abstract class may contain complete methods or incomplete methods. A method with abstract keyword should be implemented in the class in which it is extended. All the abstract methods should be implemented in the class in which it is extended. A method without abstract keyword in abstract class should be implemented right there itself as shown in the example.  A class may inherit only one abstract class. An abstract class can contain fields, constructors, etc… Abstract classes should be extended with another class or abstract class using the keyword extends. Abstract classes are faster than interface. You can have a look at the example described below.

Abstract class in Java with Example.

abstract class insurance
{
int a, b;
void plan (int aa, int bb)
{
//body implementations
}
abstract void trading ();
abstract void Citibank ();
}

Note: Each subclass of insurance which is not abstract such as bank and SBI must sure provide implementations for the trading and Citibank methods.

class bank extends insurance

{
void trading ()
{
//body implementations
}
void Citibank()
{
//body implementations
}
//Its your choice to make use of "void plan (int aa, int bb)" method here. Since it is not abstract it may or may not be used.
}
class SBI extends insurance
{
void trading ()
{
//body implementations
}
void Citibank ()
{
//body implementations
}
}

You should create an instance for class bank and for class SBI.

Abstract Class

abstract class insurance
{
abstract void Citibank ();
void Lifeinsurance ()
{
System.out.println ("Life Insurance plans are good always");
}
}
class trading extends insurance
{
void Citibank ()
{
System.out.println ("It’s better to create a Citibank account");
}
}
class bank
{
public static void main (String args[])
{
trading trade = new trading();
trade.Citibank ();
trade.Lifeinsurance ();
}
}
Abstract Class Extends Abstract Class
 abstract class a{
public abstract void insurance ();
public abstract void trading ();
}
abstract class b extends a
{
public void insurance ()
{
System.out.println ("Insurance is claimed");
}
}

No comments:

Post a Comment

My Profile