Difference between interface and abstract class with ex.
Answer Posted / janardhan
abstract class:-- abstract class contains abstract methods
and concreat methods,abstract class may have Zero(0)
abstract methods also, for example HttpServlet class is
best example for abstract class, HttpServlet class contains
Zero abstract methods, The SUN People declared HttpServlet
as abstract,B'ecz out side people unable to create object
for HttpServlet(abstract class)and due to security reasons
HttpServlet declared as abstract class.
If you are declared abstract methods in a abstract class,
we should provide the implementation in child classes.
we can't create Object for abstract class, we can create
refrence for abstract class.
In real-time, we can use abstract classes depends on
requirement only.
Interface:---
By default all the methods in Interface are public abstract
methods.Variables are by default public static final.
All the methods are non-implemented in interface and we
need to provide the implemantation for abstract methods in
interface implemented class(child class)
For example Servlet is a interface, if MyServlet class
implements Servlet, then we should provide the
implementation for Servlet interface methods like init
(),service(), destroy(),servletConfig(),etc..
EX:-- MyServlet implements Servlet{
init(){
........
........
}
service(){
........
...........
}
destroy(){
........
........
}
//we have some of the methods of Servlet interface ,those
//methods we need to implement here
}
Mostly In real time we need to use Interfaces.
for example :--
All business methods we need to declare in interface and we
need to provide the implementation for those business
methods in implemented class.
Main purpose of interface is :--
Reusability
maintainability
Example:--
public interface DaoInterface{
public abstract getDetails();
public abstract getProductId();
public abstract getProductName();
public abstract searchProduct();
public abstract editProduct();
.........
............
}
public class DaoClass implements DaoInterface{
public abstract getDetails();{
// we need to write our business logic here depends on
requirement.
}
public abstract getProductId();{
// we need to write our business logic here depends on
requirement.
}
public abstract getProductName();{
// we need to write our business logic here depends on
requirement.
}
public abstract searchProduct();{
// we need to write our business logic here depends on
requirement.
}
public abstract editProduct(); {
// we need to write our business logic here depends on
requirement.
}
}
Note:-- The above DaoInterface business methods any body
can implement their own way and write business logic
depends requirement.
Thanks
Jana
| Is This Answer Correct ? | 9 Yes | 0 No |
Post New Answer View All Answers
How many bits are used to represent unicode, ascii, utf-16, and utf-8 characters?
Can we define private and protected modifiers for variables in interfaces?
Can we print null in java?
What is difference between static class and normal class?
What is int lol?
what type of questions asked for barclays technologies pune please send urgent
Explain runtime exceptions?
What is variable in java?
What is double checked locking in singleton?
Explain abstract class in java?
Given a singly linked list, determine whether it contains a loop or not without using temporary space?
Hi i am creating desktop application in that i want calling to mobile number. i have java telephone api (JTAPI) but i dont understand how it configure & use plese help me
Does java map allow duplicates?
Is array size fixed in java?
Why singleton pattern is better than creating singleton class with static instance?