• 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

what is the advantageDeclaring variables with interface names

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

This might be a very naive question. but still am posting it.
Let me elaborate the above question.

Suppose i have an interface interface1,

I implement it in a class called class1.


What is the differnce between the below two delcarations
class1 x = new class1();
and
interface1 x = new class1();



Second part of my question is, I had a method in class which was not delcared in interface. But When i delcare a variable of type interface1 x, i am still able to access that method. How??
 
Ranch Hand
Posts: 233
1
Eclipse IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

What is the differnce between the below two delcarations
class1 x = new class1();
and
interface1 x = new class1();


The most basic difference that I see is that you can carry the object. For example, we have a Math interface, and an Add class implementing it. Let there be another class ZigZag implementing Math interface, that has some specific functionality.


The doSomething() method in ZigZag class does not know if the parameter is coming from Add class or some other class, it only knows that the incoming parameter must be of Math type (specifically any class implementing Math interface). So, z.doSomething(a1); will not work. Hence you could move object in your application easily and knowingly.

I had a method in class which was not delcared in interface. But When i delcare a variable of type interface1 x, i am still able to access that method. How??


Considering the method is not declared in interface,
• and it is of instance type, then the method in the object's type (class) will be invoked. Instance methods are executed based upon the object used to invoke it.
• and it is of static type, then it will lead to compiler error. The method with same signature must be present in the interface. Static methods are executed based upon the type of the variable or if accessed directly like , the object's type's or its super-class's static myMethod() will be invoked.
Go with my English literature and try some examples on your own.
 
Ashwin Raghavan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply. You answered my doubt precisely.
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome again

Have a look round the Ranch; you will find discussions like this one, which I hope will be helpful about similar questions.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also...remember that in the 'real world', most of the time you will not be writing all the code. It is common for different groups to work on different parts of the code. In fact, code could be written by a group in a completely different company.

Using interface names allows that other group/company make changes to their side without you worrying about your code.

You can think of an interface as being similar to a physical USB port. Let's say I am going to get some data from a service on a weekly basis. By using a USB interface, they can send me a thumb drive, a cell phone, an external hard drive, or hundreds of other possible devices. I don't really care, as long as it uses a USB.

By using an interface in Java, this other group can change the underlying object type and how it does its work behind the scenes. As long as it still implements the Java interface, I can 'plug it in' to my code and not care about anything.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic