Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


When to use abstract class and when to use interface?

Posted on 2006-07-23
Medium Priority
Last Modified: 2010-03-31
Hello Expert,
 what is abstract?
 what is interface?              
 Please tell me When to use abstract class and when to use interface?
 plz give sutable example and defination.

                                        Thanks you
Question by:Atkari
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 92

Accepted Solution

objects earned 500 total points
ID: 17165618

Author Comment

ID: 17165730
give the example

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Assisted Solution

kaliyugkaarjun earned 500 total points
ID: 17165979
WHat is abstract class?

What is interface?

DIfference between the two

An abstract class can have instance methods that implement a default behavior. An Interface can only declare constants and instance methods, but cannot implement default behavior and all methods are implicitly abstract. An interface has all public members and no implementation. An abstract class is a class which may have the usual flavors of class members (private, protected, etc.), but has some abstract methods.

Assisted Solution

colr__ earned 500 total points
ID: 17166052
By implementing an interface, you guarantee that the class can service the methods defined in that interface. For example, if you’ve got an interface with a method getX(), then any class that implements that interface MUST  implement the method getX().

This is useful when you don’t know for sure what class type you are going to be using. For example, imagine an object reference passed into a method. You might not be aware what the type of the class is, but you might only need its getX() method - you can set the type argument in the method to be of the type of the interface, so no matter what the type of the class that is passed in, you know you can definitely call getX().

Abstract classes are a similar concept, but can contain pieces of implementation. So, an abstract class may have one method that is implemented/defined, and another method that is declared as abstract and which requires the programmer to implement.

Abstract classes and interface cannot be instantiated - they can only be extended by other classes.
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 500 total points
ID: 17167768

Expert Comment

ID: 17176165
some more

 Neither Interface or Abstract classes can be instantiated. An interface has all public members and no implementation. An abstract class is a class which may have the usual flavors of class members (private, protected, etc.), but has some abstract methods.

Use interface as much as possible and only use an abstract class in the case where you want to provide some (but not all, of course) implementation. In practice, you want to prefer using, passing and returning interfaces, not abstract class references. I think even in the case where you have an abstract class to share some implementation, you still want to have an interface represent what will be used by the client (for an example look at the new Collections -- you often pass around Collection, List, Set or Map references, but not a AbstractList, AbstractSet or AbstractMap, references).

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question