Win a copy of Fixing your Scrum this week in the Agile forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

(Design Perspective)Difference Between Abstractclass/Interface

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I want to know difference between Abstract class and Interface .
1)Like How should I know where to use interface and where to use abstract class.

Regards
Shri Sonparote
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Shri,

The question "what is the difference between an abstract class and an interface" is asked very often here on JavaRanch. I guess you understand the difference, but you're asking when it is better to use an abstract class, and when it is better to use an interface.

When you design software, there are always multiple ways to design it. There's almost never just one single way that is the only way or the absolute best way to design software. So there is no exact answer to the question when you should use an abstract class and when you should use an interface; nor is there a set of cookbook rules to find out what you should use.

There are a few things that you can take into account:

  • A class can implement multiple interfaces, but it can only extend a single (abstract) class. So interfaces are more flexible; they don't force you to use a specific superclass.
  • When you write a non-abstract class and you implement an interface, you are required to implement all methods in the interface. This can be cumbersome if the interface has lots of methods. In an abstract class, you can make only some of the methods abstract, and a non-abstract subclass only has to implement the abstract methods.


  • To learn about how interfaces and abstract classes are used in practice, have a look at how they are used in the standard Java API. Have a look, for example, at the interfaces and classes in the package java.awt.event. That package contains a number of interfaces that represent event listeners: KeyListener, MouseListener, WindowListener etc. Each of those interfaces contain a number of methods that are called when a specific event happens.

    MouseListener contains five methods: mouseClicked, mouseEntered, mouseExited, mousePressed, mouseReleased. Suppose that you want to write a class that has to do something when the mouse is clicked. You create a class that implements MouseListener. Now you have to implement all five methods in your class, even when the class is only interested in mouseClicked events. Your class would look something like this:

    Note that you have the mouseClicked method which does something, and four empty methods that do nothing, but that are required because you must implement all methods in interface MouseListener. This is cumbersome!

    To make this easier, there is also an abstract class MouseAdapter. This class implements MouseListener with empty methods. If you change your class to extend MouseAdapter instead of implement MouseListener directly, then you don't have to provide all the interface methods, because MouseAdapter already provides them for you. So your class can be much simpler:
     
    shri Sonparote
    Ranch Hand
    Posts: 30
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi
    Jesper

    Thanks
    Shrikant
     
    Uh oh, we're definitely being carded. Here, show him this tiny ad:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic