• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abstract class & interface

 
Manikandan Swaminathan
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is wrong with returning an abstract class? You can return one of its subclasses.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
same is the case of Abstract class.....................

i hope it may be clear now....................
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)?
 
Ram Narayan.M
Ranch Hand
Posts: 247
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Manikandan Swaminathan
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic