This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Abstract class & interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Abstract class & interface" Watch "Abstract class & interface" New topic
Author

Abstract class & interface

Manikandan Swaminathan
Ranch Hand

Joined: Aug 10, 2008
Posts: 117
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: 38062
    
  22
What is wrong with returning an abstract class? You can return one of its subclasses.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

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: 1051

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: 247

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: 117
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Abstract class & interface
 
Similar Threads
Abstract Class Within an Interface
Multiple Beans for same Home And Remote interfaces
default package
instance variables
creating abstract classes within an interface in java