?
Solved

When to use abstract class and when to use interface?

Posted on 2006-07-23
9
Medium Priority
?
262 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 500 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 500 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 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.
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 500 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month12 days, 1 hour left to enroll

752 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