Difference between interface and abstract class with ex.

Answers were Sorted based on User's Feedback



Difference between interface and abstract class with ex...

Answer / aditya

Abstract Class :
1. Can have implemented(non-abstract) method
2. Can have any visibility public,protected,private


Interface :
1. All methods are non-implemented(abstract)
2. Only visibility is public or none
3. All variables should be static and final

Is This Answer Correct ?    17 Yes 0 No

Difference between interface and abstract class with ex...

Answer / sreenivas g

Abstract Class : If a class contains atleast one
non-concrete(not implemented) method, it is said to be
Abstract class. it should be defined with keyword abstract.
It is a combination of both implemented and non-implemented
methods.

Interface : By default all the methods in Interface are
abstract methods. All the methods are non-implemented in
interface.

Is This Answer Correct ?    18 Yes 2 No

Difference between interface and abstract class with ex...

Answer / 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

Difference between interface and abstract class with ex...

Answer / vinoth sing

abstract class contain both abstract and non abstract methods
vs
interface can have only abstract methods.

a class implementing a interface itself a abstract class.

eg for interface

interface shape2d
{
double getArea();
}

class circle implements shape2d
{
int radius;
public double getArea()
{
return math.pi*r*r;

}
circle(int radius)
{
this.radius=radius;
}
class circledemo
{

public static void main (string args[])
{
circle c= new circle(10);
system.out.println(c.getArea());
}

}

Is This Answer Correct ?    4 Yes 1 No

Post New Answer

More Core Java Interview Questions

Suppose there is an Online shopping cart application having different objects like Cart, SelectionItem, Billing, COnfiguration, Authentication, Authorization, PersonalDetails etc. Out of this which one can be made a singleton pattern and why?

2 Answers   RBS,


If you do not want your class to be inherited by any other class. What would you do?

0 Answers  


Create a form of user Login in JSP, that accepts the user name and password from user and authenticate it with user names and passwords stored in database. (Plz provide me answer immediately)

1 Answers   ABC,


Can singleton class be serialized?

0 Answers  


What is a return in java?

0 Answers  


What is the unit of plancks constant?

0 Answers  


What is meant by attribute?

0 Answers  


What is the dot operator?

0 Answers  


Can we make a constructor final?

0 Answers  


How will you communicate between two applets?

0 Answers  


I have an HashMap object, which has with key and value pair. It has 10 keys and values in that object. Now the question is I want insert new key and value in middle or any where in that list but not at the end or at the top. Is it possible or not. If yes how can we achieve this one?

2 Answers   Huawei,


What access modifiers can be used for methods?

0 Answers  


Categories