Solved

When to use abstract class and when to use interface?

Posted on 2006-07-23
9
212 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
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
 
LVL 8

Expert Comment

by:kaliyugkaarjun
ID: 17165967
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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:mayankeagle
mayankeagle 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
scoreUp challenge 14 48
sumHeights2  challenge 7 75
computer science syllabus 3 52
java  and programming certification ? 4 64
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now