• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

interface and abstract class

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When does it make sense to choose an abstract class over interface?
I have gone through some earlier posts but couldn't get it.
Can anyone explain me this with some example.
Thanks
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you do not want any implementation you go for an interface.
If you want some implementation (i.e. non-abstract methods) go for abstract class. (Remember abstract class can contain non-abstract methods methods)
Example:

Consider a abtract class Vehicle which has subclasses Car, Boat, Airplane
Now certain methods which are specific to the subclasses e.g. the move() method will be implemented in the subclasses and declared abstract in the abstract class Vehicle.
Certain methods such as doVehicleRegistration is common to all subclasses. So they are implemented in the abstract class Vehicle.

If you had used interface Vehicle instead of abstract Class Vehicle then
you had to give implementations for doVehicleRegistration method in all the classes (Car, Boat, Airplane).

Regards
Yohan Shikari,
SCJP 1.4
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abstract classes imply concrete inheritance.
Concrete inheritance is a horrible evil that plagues object-oriented programming with the blind following.
Interfaces are best used as the separation from 'programming interface' and implementation detail. To obscure this separation, usually in the name of 'saving typing' is just a horrible ghastly thing to do.
Read up on what 'Design By Contract' is (DBC).
Be nice to yourself and the clients of your software, declare ALL classes final.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
also the basics...

you cant extend more than 1 class but can implement many interfaces, just learnt that last week. am i right?
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. You are right.
You can extend only one class at a time in Java. Because Multiple inheritence is not supported in Java.

But you can implement more than one Interface at a time.
The Syntax is

<Access Specifier> Class Class_Name extends class1 implements interface1,Interface2,..,Interface n.

Hope you understood...

 
apoorba mohapatra
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can it be like this:
When you think "is-a", you should use an abstract class. When you think "can-act-like-a", use an interface.
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by apoorba mohapatra:
Can it be like this:
When you think "is-a", you should use an abstract class. When you think "can-act-like-a", use an interface.


More like an "IS-A" (inheritance) vs. "HAS-A" (interfaces, meaning capabilities) test.
Concrete class can inherit only one class, but implement many interfaces. Moreover, interfaces can inherit multiple other interfaces (but implement none, which makes sence-interface = zero implementation).
Think abstract if you wan to put all the functionalities of the follow-up concrete classes in one place... abstracts should contain the genotype of the followup classes.
Think interfaces when you want to implement functionalities selectively and you put each functionality in a separate interface. That way, you can choose which functionality to be implemented, and you can implement more than one.
Interfaces can also be implemented by abstract classes...
reply
    Bookmark Topic Watch Topic
  • New Topic