Difference Between Abstract Class And Interface In Java
An abstract class in java is a class which has at least one abstract function.
An interface in java is an entity and not a class which has all its methods as abstract.We cannot have even one implementation of function or method in an interface in java.
An abstract class with all its methods as abstract is close to being an interface.But the concept for usage of both is polls apart in context.
An abstract class looks like :
public abstract class AbstractClassExample1{ abstract void abstractMethod(); // concrete methods are still allowed in abstract classes void nonAbstractMethod() { System.out.println("This is a non abstract method."); }
Here we can see that we can have both abstract and non abstract methods in abstract class.
An interface looks like :
interface InterfaceExample{ public void method1(); public void method2(); } }
Here in the interface all the methods or functions are abstract.
All the methods in interface need to be public .
Now comes the scenario ,when to use an abstract class and when to use an interface.
Scenarios where we want re usability of some of the methods or functions and some methods or functions are left for the client to implement as per their requirement calls for the use of an abstract class.In such cases client knows that some of the default implementation is already there and some part is up to them how to implement in the form of methods or functions.In such cases it logical to extend the concrete class from an abstract class.
Now comes the scenario where two coders are working for an application or software in which one calls the function or method in his module which is written by another coder.If in future the first coder who writes the method or function ,for some reason changes the function signature it will hamper the code of the coder who is calling the function.To avoid these type of issues we can use interfaces.If the person who writes the method implements it with interface and the function call is also done with the reference type interlace,then both he coders know the standard to be followed in order to write or call a method.Thus in order to follow some standard we use interfaces.
Number of View :2192Related posts:
- Life Cycle Of A Servlet Explained Handling Servlet Life-Cycle Events Servlet Life Cycle Servlet Interview Questions Java Servlet Programming
- Quartz Open source Scheduler Basic Schedulers in Java Install Quartz for JAva
- Difference Between Parameters And Attributes In Servlets/JSP Parameter vs. Attribute
- Request Dispatching In Servlets Servlets / Request Dispatcher RequestDispatcher Vs SendRedirect What Is A Request Dispatcher And How Does It Work What Is A Request Dispatcher And How Do I Use It Include Forward Redirect
- Concept Of Session In J2EE Session Tracking In J2EE Session Management Seesion Concept Servlet Basics Servlet Guide Session Tracking
Tags: abstract class, interface, java
This entry was posted on Sunday, May 13th, 2012 at 12:25 pm and is filed under Java, OPPS, software, Tech-Tips, Tips & Tricks. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.