[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Abstract class and Interface problem!

Posted on 2000-01-25
6
Medium Priority
?
280 Views
Last Modified: 2010-04-01
Dear all,

I would like to ask what the difference between abstract class and interface.

Which situation I should define a interface and which situation should define a abstract class?


Please give me an example.


Thanks a lot!

Benson
0
Comment
Question by:benson031397
6 Comments
 
LVL 7

Expert Comment

by:Ravindra76
ID: 2385894

Abestrct class is a class which contain at least one abstract method
or

which extends a abstract class and allows derived classes to implement the abstract methods and it won't provide implementation.

Abstract classes may contain non abstract methods

Interface will contain will contain pure abstract methods.
Interface won't contain non abstact methods.

IF your class contain all abstract methods, use interfaces.
IF your class contain at least one non abstract method and at least one abstract method, use abstract class


Best of luck
0
 

Expert Comment

by:z_karthik
ID: 2385912
I consider interface more of a tool to share the same method signatures for different classes.  
with interfaces you can  simulate multiple inheritances. If you think you are defining the same kind of methods ( methods which do simlar stuff except they work on instance variables defined at that level ) create an interface with a meaningful method signature and try to make sure anybody implementing types of your classes also implements the interface.
0
 
LVL 1

Expert Comment

by:BillyAbbott
ID: 2388425
interfaces can't have method bodies in.
abstract classes can, but must still have some abstract declarations.

i use interfaces when i want to provide a definetion of a class, but all the direct subclasses will have different enough implementations so that no method modies should be written.

i use abstract classes when some implementation details will be shared amongst all or pretty much all of the direct subclasses, so that you will need to override a method instead of implement it.

interfaces are also useful as marker classes to group collections of classes together so that you can store them in arrays or other data structrures.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 

Accepted Solution

by:
g_senthilkumar earned 150 total points
ID: 2388459
Hi,
  As all the guys told,both abstract class and interface will delcare the methods which all the derived classes has to override.
   In abstract class,
  it contains atleast one abstract method(ie method prototype,but no implementation) and all the derived classes extending that abstract classes has to override that abstract method.Otherwise the derived class will become abstract.Abstract classes can contain normal methods(ie mehtods with implementations).That normal derived class dont need to override.
   In Interface it can contain only method prototype,no implementation at all.By default all the methods in interface becomes public and abstract.
Thats why if you implementing the interface and that while overriding the interface methods you have define that methods as public,since in jave you can not reduce visibility.
   In interface all the methods are be default public and abstract and all the variables are by default public,static and final.So you can only use the variables and you can not change the value.
   In abstract normally,it should contain atlest one abstract method.That time compilier enforce you to declare the class as abstract.Even the abstract class can exist without any abstract methods.That time the compiler wont enforce you to declare the class as abstract.That time if you declare the class as abstract,it wont give any error.The only thing is you cannot create instances of that abstract classes.
   So both abstract class and interface are used to define the methods which can be overriden by all the derived classes.But in abstract class you can define normal methods also.But interface,by default all the methds are abstract.So for that reason you can say interface is a pure abstract class,otherwise one step ahead of abstract class.
   Refer Thinking in Java book for more information about Abstract class and interfaces in the context of polymorphism.
abstract class Exam
{
  public void abstract display();
}
public class DerExam1 extends Exam
{
  public void display()
  {
    System.out.println("inside DerExam1");
}
}
public class DerExam2 extends Exam
{
  public void display()
  {
    System.out.println("inside DerExam2");
}
}
public class DerExam
{
   public static void main(String []args)
{
  Exam e=new DerExam1();
  e.display();
  e=new DerExam2();
  e.display();
}
}
   By having the handle of the base class,you can call methods in derived class using polymorphism.
   In the same example instead of Exam class define Exam interface and implement that,It will work out.
   I hope u got some idea about Interface and polymorphism.



 
0
 
LVL 5

Expert Comment

by:Jod
ID: 2388528
[Hi benson - your other question has got rather technical so I'll try to keep it simple here :)]

A good basic principle to use is this:

**********************
1)
**********************

Use an interface to group a number of classes into one parent type (such as sheep and wolves into animals for example).

Use inheritance and possibly an abstract base class when you are refining the behaviour of the same type of object. For example if you have a sheep object and you want to divide your sheep into two subclasses such as sheep with horns and sheep without horns.


One way to look at it is how much of the code I inherit from the base class is actually useful to me.

If you inherit lots of code and are only changing one method for example then it is practical to inherit from super class or abstract base class.


**********************
2)
**********************

If you abstract class has no implementations or you find that you always need to override the base class implementations in most cases then your code will be more flexible by using an interface.

**********************
3)
**********************

Another point is, if your classes need to inherit from more than one thing then you should use an interface.

For example, if you need to express information about animals your subclasses (such as sheep) might have the need to inherit from two things:

Animal
Carnivore
Herbivore

In this case if you use an abstract base class you cannot make your sheep class be of type animal and of type herbivore.

You may want to do this, because you might want to deal with animals that eat grass differently to animals that eat other animals.

If you use interfaces in Java, then you can do this.


Does this help?


0
 
LVL 5

Expert Comment

by:Jod
ID: 2389305
>> [Hi benson - your other question has got rather technical so I'll try to keep it simple here

Sorry, ignore this line of my above comment - got it confused in with another answer.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

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

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 …
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

590 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