abstract class Vs Interface real life scenerios
posted 13 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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

posted 13 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.
posted 13 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi Folks,
I think there are more differences technically we have between the two..!!
I think there are more differences technically we have between the two..!!
posted 13 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.
posted 13 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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..!!
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..!!
posted 13 years ago
Hi Guys ,
Please advise for the other scenerios also..!!
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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..!!

posted 13 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.
No pain, No gain.
OCJP 1.6, Liferay Certified Developer 6.1
posted 13 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I would suggest you to pick Effective Java by Joshua Bloch and it has a really detailed chapters for Interfaces and classes.
Mohamed Sanaulla | My Blog | Author of Java 9 Cookbook | Java 11 Cookbook
posted 13 years ago
@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
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 |








