Solved

When to use abstract class and when to use interface?

Posted on 2006-07-23
9
228 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Cipher Configuration on Apache HTTPD 4 62
Groovy problem when using SOAPUI : DispatchException occurred 7 43
arguments to jar 5 26
mysql jsp example issue 32 32
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.

813 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

14 Experts available now in Live!

Get 1:1 Help Now