• 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:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

abstract class Vs Interface real life scenerios

 
Ranch Hand
Posts: 203
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Folks,

I have been doing lot of googling and R&D but still can't be clear the practical real life examples that in which situation we use abstract class and in which situation we do use interface..??Please guide me that what are the practical uses of these two..?Thanks in advance
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The basic difference between the 2 should tell you its usage. You can only extend 1 abstract class while you can implement any number of interfaces.
 
Saral Saxena
Ranch Hand
Posts: 203
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Folks,
I think there are more differences technically we have between the two..!!
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you need it to include any implementation? If you do, you need to use an abstract class, because interfaces can't contain implementation. If you don't, use an interface because it's more flexible (because you can implement multiple interfaces).

One approach that's fairly common is to have an interface defining the contract a class has to meet, and then an abstract class implementing that class that provides either common behaviour that subclasses will need, or sensible default behaviour. In this case subclasses will usually extend the abstract class, but they are still able to implement the interface directly if that's more appropriate.

For an example in the core Java libraries: java.util.List is an interface. java.util.AbstractList is an abstract class, and ArrayList, LinkedList and Vector all inherit from that.
 
Saral Saxena
Ranch Hand
Posts: 203
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

There are many other scenarios can You guys also fit the concept of these two(abstract class and interface ) where they both are use practically..!!
 
Saral Saxena
Ranch Hand
Posts: 203
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Saral Saxena wrote:Hi Guys,

There are many other scenarios can You guys also fit the concept of these two(abstract class and interface ) where they both are use practically..!!



Hi Guys ,

Please advise for the other scenerios also..!!
 
Ranch Hand
Posts: 171
Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Find the example below,

If you wanted your clients can customize your product by them, can provide two options as bellow,

* Only can implement all the functions which are defined by you. ( interface )
* Can implement few other functions other than core functions defined in your class ( astract )

When you provide interface to clients to customize, you would be processing the functions which are defined only in the interface. (This is only we provide non-implemented functions and we know to process). But in-case of abstract, core functions implemented by you and extra functions left non-implemented.

other real time example is lorry/bus chase which is delivered from company is example for abstract since all the default implementations are done. If needed then can customize your self by building body type you wanted. In case of interface, gear-box, clitch, acceilator which is left you to operate.

This is the only difference between the two. Hope this clarifies you.
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would suggest you to pick Effective Java by Joshua Bloch and it has a really detailed chapters for Interfaces and classes.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:For an example in the core Java libraries: java.util.List is an interface. java.util.AbstractList is an abstract class, and ArrayList, LinkedList and Vector all inherit from that.


@Saral: You asked for real-life scenarios. So here are two, both from Matthew's example:

1. Why use an interface:
Consider the following definition:
ArrayList<String> myList = new ArrayList<String>();
This ties 'myList' to a specific implementation (an ArrayList); and if you discover later on that 'myList' would have been much better as a LinkedList, you will have to change every piece of code in your system that uses 'myList'.
On the other hand:
List<String> myList = new ArrayList<String>();
makes no such assumption. If all the places that use (and all the methods that take) 'myList' assume only that it is a List, you only have ONE thing to change if you want to make it a LinkedList: the definition. Viz:
List<String> myList = new LinkedList<String>();

2. Why use an abstract class:
As Matthew said, they make very useful "skeleton implementations" of existing interfaces. Take the following example (filched directly from Effective Java):defines a fully-functioning List (albeit a fixed-length one) derived from an array or a list of values. Anywhere in your code, you can create it:
List<Integer> nums = new ArrayAsList<Integer>(1, 2, 3, 4, 5);
and then use it just like any other List, viz:
Integer three = nums.get(2);
...
for (Integer num : nums) {
   // do stuff with 'num'
}
...
Collections.shuffle(nums);
...


Given all the things that you can do with a List, it makes AbstractList an extremely powerful building block.

HIH

Winston

 
Can you really tell me that we aren't dealing with suspicious baked goods? And then there is this tiny ad:
The new gardening playing cards kickstarter is now live!
https://www.kickstarter.com/projects/paulwheaton/garden-cards
reply
    Bookmark Topic Watch Topic
  • New Topic