• 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

interface

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Suppose "Product" is an interface and "ProductImpl" is the class which implements "Product" interface which consists of a constructor
ProductImpl();

Then what does the following statement means
Product prod = new ProductImpl();

Can we give ProductImpl prodimpl = new ProductImpl() ???
what is wrong with the above statement?
 
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you asking why Product p = new ProductImplm();

//we can refer the instance of the class ProductImplm implementing an inteface Product by a Product p??.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by gayathri mukkavilli:
...what does the following statement means
Product prod = new ProductImpl();
...


A new instance of ProductImpl is being created, and a reference to this object is being assigned to a variable of type Product. This is an upcast, because ProductImpl implements the Product interface.

It's kind of a shorthand version of...
 
Ranch Hand
Posts: 335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Product prod = new ProductImpl();

Can we give ProductImpl prodimpl = new ProductImpl();

both are valid.

in first statement you use reference of Product as PruductImpl implements Product it passes IS-A test so can be assigned.

in second statement you create reference of same class and object so both are valid.
 
reply
    Bookmark Topic Watch Topic
  • New Topic