• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Abstract class & interface

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79259
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is wrong with returning an abstract class? You can return one of its subclasses.
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
same is the case of Abstract class.....................

i hope it may be clear now....................
 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)?
 
Ranch Hand
Posts: 247
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic