• 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

is it illegal to have interface reference type referring to subclass (non-abstract) instance ?

 
Greenhorn
Posts: 21
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is it illegal to have interface reference type referring to subclass (non-abstract) instance ?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Assailant Roshi wrote:is it illegal to have interface reference type referring to subclass (non-abstract) instance ?


What, you mean like:
List<X> list = new ArrayList<X>();
?

If so, the answer is no - indeed, it's a recommended approach (see programming to the interface).

If not, I don't understand the question.

Winston
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You mean something like this?

That's fine. In fact, that's the whole point. You can't have an interface variable referring to anything other than a subclass (or more properly: implementing class) instance, because you can't instantiate an interface directly.

Or did you mean something else?

Edit: or...what Winston said while I was still typing.
 
Roshi Kumar
Greenhorn
Posts: 21
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for you replies .

Matthew Brown , you got it what was my query . I got my answer thanks.

 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can declare any variable with a type and create an instance which is a subtype of that type, as you have already been told.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This feature of using interface is the powerfull polymorphism feature in Java.. It is always advisible to use interface.

Example :
interface Animal{
void bark();
}

class Dog{
void bark(){
//perform bow bow here
}

}
class Cat{}{
void bark(){
//perform mew mew here
}
}

In the main method perform the following ..
Animal animal1 = new Dog();
Animal animal2= new Cat();

animal1.bar(); // bow bow is done for interface object
animal2.bark(); // mew mew is done for interface object
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic