| Author |
Abstract class & interface
|
Manikandan Swaminathan
Ranch Hand
Joined: Aug 10, 2008
Posts: 115
|
|
Hi Ranchers,
While reviewing the code for one of our applications, i came across the following scenario.
There was an abstract class (say ABC) and an interface (say XYZ). The interface definition looked as follows
public interface XYZ{
ABC getDetails() throws Framework Exception;
....
etc
}
On seeing this,i was bit confused as to what is the need to specify a method with return type of abstract class in an interface?
Have anyone of you used/faced similar scenario in the past? Would be great if they can shed some light on this
Thanks in advance.
Cheers
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32687
|
|
|
What is wrong with returning an abstract class? You can return one of its subclasses.
|
 |
Shanky Sohar
Ranch Hand
Joined: Mar 17, 2010
Posts: 1046
|
|
when we use interface and abstract class reference then ..with that we can only call those methods which are declared in the interface or abstract class not those which is a method of concrete class and not define in abstract class...........
like
by using the reference of Interface A we can call only two methods which are declared in Interface A. anything other will be called by class A instance...
|
SCJP6.0,My blog Ranchers from Delhi
|
 |
Shanky Sohar
Ranch Hand
Joined: Mar 17, 2010
Posts: 1046
|
|
same is the case of Abstract class.....................
i hope it may be clear now....................
|
 |
Vinoth Kumar Kannan
Ranch Hand
Joined: Aug 19, 2009
Posts: 276
|
|
ABC returned in the method signature just indicates what type of reference you are going to give back,rather than the instance itself.
A reference to an abstract class can very well hold instances of its subclasses.
|
OCPJP 6
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
Raghuraman Guruswamy wrote:
On seeing this,i was bit confused as to what is the need to specify a method with return type of abstract class in an interface?
Do you think that, an abstract can't be returned from an interface methods or from any methods rather than interface methods(in Java)?
|
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
|
 |
Ram Narayan.M
Ranch Hand
Joined: Jul 11, 2010
Posts: 244
|
|
This scenario can be explained in the form of :
interface MobileDetail {
SimCard getSimCardDetails();
MobileBattery getBatteryDetails();
}
With the MobileDetail as an interface, a class implementing this interface can return a related details of Mobile...
Since an entity does not have property that is instance variables to be specified and specifies only the behavioral structure, interfaces are used...
|
SCJP 6 [SCJP - Old is Gold]
|
 |
Manikandan Swaminathan
Ranch Hand
Joined: Aug 10, 2008
Posts: 115
|
|
Dear all,
Thanks for your replies..
I was just wondering the need for method declaration in interface with return type of abstract class. It was not a question of right or wrong or why it cant be done. I know that it is possible
I was just curious to understand under what situations would one go for this?
Thanks again
|
 |
 |
|
|
subject: Abstract class & interface
|
|
|