Search This Blog

Thursday, April 29, 2010

Difference Between Interface and Abstract Class in Java

Java Abstract Class & Interface:

I am admire always about java classes and Interface.
Did any(normal) software developer think about why Class and Interface.
I have started discover about the need of class and Interface after joining in software development company.
That time i don't have deep knowledge about the Java, So i have used to ask all doubt to my Team lead ,he has only 2 year Exp. He teach ed me about all technique behind Java.

What is Class?
All are used to define class is the collection of instance variable and different methods for
different operation.
As of my knowledge and understandable class is a entity in real world is nothing but the one Real object (ie. Human is a entity ) and that object is have set of property(ie., Gender,Height,weight,country) are called attribute(Instance variable).
Same Real Object has set of operation (Speak,Read,Write) are called Methods in Java

So class declaration will as follow in java.
package com.blogspot.microtechinfo;

/**
*
* @author root
*/
public class Human {

char Gender;
int Height;
int weight;
int country;
public void Read()
{System.out.println("Read Up");}
public void Write()
{System.out.println("Write");}
}

Where the Reusability will come into pictures of Java?
All we used to think that java provides more re usability than C. How Java achieving reusability answer is using inheritance.
Inheritance is possible for both class and Interface.

What does means Inheritance?
Inheritance its the process to get the property & operation from the inherited class. The inherited class are called Parent class.
The inheritance class is called Child class.
We have class called Human. All employee of the organization are human and has some set operation(methods) and Property(atribute).
In java we can achieve the inheritance using two key word are extends for class and implements for Interface.
With example ::
package com.blogspot.microtechinfo;

/**
*
* @author root
*/
public class Employees extends Human {
String designation="";
double salary;
///for overduty calculation
public int getOverDuty()
{
return 5;
}
public static void main(String str[])
{
System.out.println("With in Employee");
Employees employees=new Employees();
///////Miracle start here ///
////How Read& Write method is visible here????
employees.Read();
employees.Write();
System.out.println("GetOverDuty:::"+employees.getOverDuty());
///////All credit goto the extends keyword in java.
}

}
output:::
With in Employee
Read Up
Write
GetOverDuty:::5

Its all about Inheritance and Re usability are achieved in java.
Before need to know the Difference between abstract class and Interface we must familiar with the inheritance.

Interface in Java:
Its a type of class only has the declaration of methods and Definition of the the property(Instance variable).
In more technical :
Its a set of predeclared rules that should be implemented by the followers(Class) of the interface.
In our example I am going to declare that every employer should have set of functionality like isTeamLead,isIIMorIITStudent, isIndianCitizen,totalInvestMent



Interface for rules defined for Employer:

package com.blogspot.microtechinfo;

/**
*
* @author root
*/
public interface EmployeerInter {
public boolean isTeamLead(boolean set);
public boolean isIIMorIITStudent(boolean set);
public boolean isIndianCitizen(boolean set);
public void totalInvestMent(int InvestAmt);

}

employer class should implements as follow::

package com.blogspot.microtechinfo;

/**
*
* @author root
*/
public class Employeer implements EmployeerInter {

public boolean isIIMorIITStudent(boolean set) {
return set;
}

public boolean isIndianCitizen(boolean set) {
return set;
}

public boolean isTeamLead(boolean set) {
return set;
}

public void totalInvestMent(int InvestAmt) {
System.out.println("InvestAmt::"+InvestAmt);
}
public void OwnMethod()
{
System.out.println("I am from Employeer");
}
public static void main(String arg[])
{
Employeer employeer=new Employeer();
employeer.OwnMethod();
///////Statr the Magic Here
System.out.println("isITTMorIITStudent::"+employeer.isIIMorIITStudent(true));
System.out.println("isIndianCitizen::"+employeer.isIndianCitizen(false));
System.out.println("isTeamLead:::"+employeer.isTeamLead(true));
/////////Here the Employeer Can define there own functionality for Implemented class
//////The Magic is fully depends upon the Implement key word

}


}

Output::
I am from Employeer
isITTMorIITStudent::true
isIndianCitizen::false
isTeamLead:::true

Now we have understand the concepts of re usability clearly so we can move to the concept of
abstract class.
Requirement::

Parent class should have implemented method and set of Rules for child class in that case which will be supported.
1.Interface does not support because only declaration is possible.
2.Inherited Class method should have definition always.
So java introducing the concept of Abstract class.

What is abstract Class:
Its has both Implementation and declaration of methods(i.e. Rules for the child class).
Abstract class Syntax
abstract class SampleAbstract{}
method declaration::
abstract public void abstractMethod();

Sample Code:::
package com.blogspot.microtechinfo.abstractpkg;

public abstract class SampleAbstract {

abstract public void abstractMethod();
public void MethodDef()
{
System.out.println("Method Definition");

}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("with in Main");


}

}

package com.blogspot.microtechinfo.abstractpkg;

public class AbstractMethodImp extends SampleAbstract {

@Override
public void abstractMethod() {
// TODO Auto-generated method stub
System.out.println("I am implemented abstract method..");

}

public static void main(String arg[])
{

AbstractMethodImp abstractMethodImp=new AbstractMethodImp();
abstractMethodImp.abstractMethod();
abstractMethodImp.MethodDef();
}
}


Conclusion is very simple:




These are main difference between abstract class and Interface abort from Inheritance and abstract class. If any other difference please leave as comments.

No comments:

Post a Comment