• 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

Why do we need interface return type?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I would like to know why do we need an interface return type? Can anybody gimme an example please?
 
Ranch Hand
Posts: 242
Mac Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi kathik prasad

See the code below


interface Animal{}

class Elephant implements Animal{}
class Tiger implements Animal{}

public class Main
{
final static public void main(String[] args)
{
Animal animal = method(args[0]);
}

public static Animal method(String arg)
{
if(arg.equals("Vegie"))
return new Elephant();
else
return new Tiger();
}
}


Here which object to be created depends on the input from user..

And mostly we use abstract classes and interfaces to have common parent type..Or to make use of polymorphism..

Regards..
 
reply
    Bookmark Topic Watch Topic
  • New Topic