Solved

When to use abstract class and when to use interface?

Posted on 2006-07-23
9
256 Views
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
0
Comment
Question by:Atkari
[X]
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
9 Comments
 
LVL 92

Accepted Solution

by:
objects earned 125 total points
ID: 17165618
0
 

Author Comment

by:Atkari
ID: 17165730
give the example
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Assisted Solution

by:kaliyugkaarjun
kaliyugkaarjun earned 125 total points
ID: 17165979
WHat is abstract class?
http://www.programmersheaven.com/2/FAQ-JAVA-What-Is-Abstract-Class

What is interface?
http://java.sun.com/docs/books/tutorial/java/interpack/interfaceDef.html
http://java.sun.com/docs/books/tutorial/java/concepts/interface.html

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.
0
 
LVL 8

Assisted Solution

by:colr__
colr__ earned 125 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.
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 125 total points
ID: 17167768
0
 
LVL 8

Expert Comment

by:kaliyugkaarjun
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).
0

Featured Post

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

729 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