This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is the advantageDeclaring variables with interface names

 
Ashwin Raghavan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • 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??
 
Rajdeep Biswas
Ranch Hand
Posts: 207
1
Eclipse IDE Java Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks for the reply. You answered my doubt precisely.
 
Campbell Ritchie
Sheriff
Posts: 48407
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12086
29
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic