• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is this what interface type as parameter actually do?  RSS feed

 
Ted Schrey
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this a correct understanding of Interface as Parameter? If any class/object implements an interface, then if you use that object as a parameter of the Interface type, you can pass the object into a method, and it will retain all the attributes of the Object? This would mean that I can take two separate kinds of objects that implement the same interface and they can both be used in the method with all their characteristics in tact?

Example:

public interface Iface{
void printIt();
}

public class Book implements Iface {
}

public class Shirt implements Iface{
}

public class Box {
//lets say I have a method to accept "things" of type Iface that will add the "things" to an ArrayList. If I add books and shirts as their own Object, I'd need two arrayLists right? But if I make them of the Iface type, they can both be stored on one ArrayList and keep all their characteristics? Is that right?
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can have a Book or a Shirt and pass both to something expecting an IFace. Only the compiler won't be happy; neither of those classes has a printIt method. So they aren't really IFaces at all.
You can have a List<IFace> or a List<Book> or a List<Shirt>. The List<IFace> will accept books and shirts.

Don't call interfaces things beginning with I. That isn't the Java convention (looks more like C# but the principles are the same). Call it something like Printable.
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...retain all the attributes of the Object?

This isn't quite the way I'd say it. I'd say that if a class implements an interface, then an object from that class is guaranteed to have the behavior of the interface. Behavior comes from methods and an interface guarantees the signatures of the methods (or, with Java 8, even default methods).

So in your example, both Book and Shirt will have a method called "printIt" that take no parameters and returns nothing, but one might print "I'm a Book" and the other might print "I'm a Shirt."
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually interface need came from the fact that when one class needed to extend more than one class that what java don't allow
So one class can extend one class and can implement several interface
+it provides you with its one additional feature that any class no matter where they are in inheritance tree can implement interface

Similarly like a class ,interface type variable can accept object of class implementing it(in case of class you need a object of its sub class )


At compile time compiler checks in a class (interface) whose reference variable is declared
For a method which is called by .(dot) operator after reference variable

At run time JVM can only see the object so it checks for a method with same return type and argument as suggested by the compiler

So if you had overrided the method of parent class(interface) in child class it will run Child's method otherwise parent's method
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't say parent and child; computer inheritance is different from biological inheritance.
 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:Actually interface need came from the fact that when one class needed to extend more than one class that what java don't allow
So one class can extend one class and can implement several interface

I don't think that is the reason why interfaces exist. When a class extends another, it gets properties as well as behaviour. Interfaces have no behaviour and only public properties.
All an implementing class gets is the abstract methods which need an implementation (except for marker interfaces). So there is no similarity in extending a class and implementing an interface from the inheritance point of view.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya you are right in saying there is slight difference
But don't you think
Interface provides all its implementing classes , common protocol in form of its method signature just like a class
So one should not fear about all programmers designing their class which is implementing our interface

that they would make even tiny mistake while implementing interface's method

So if you don't want any programmer
to make a tiny mistake while implementing certain method in their classes use interface

Whenever I have to give example nothing comes to my mind except headfirst

So consider a situation
You have an abstract super class Animal
and there are many subclasses like Cat,Dog,Hippo
Now a thought came in your mind to include certain behavior of pet animals like (befriendly (),bedecent())
Now how you are going to do this
Problems you will face :
1-You surely don't expect Hippo to be friendly or to be decent so you can't add them to Animal class
2-You need a common contract so that programmers don't make tiny mistake
3-You want to make polymorphic use of pets
Don't you feel the need of 2 super class

Oh!!!Yeah then come interface the saviour


Interface can be used for other different purposes but I feel in such situations interfaces are blessing
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing more now interface can also have methods with body
Not only poor undervalued abstract method
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, interfaces were probably not introduced because of the problems of double inheritance, but they certainly make double inheritance easier to avoid. They were introduced to encapsulate functionality.

Default methods in Java8 interfaces are a red herring. Forget about them. And don't call abstract methods undervalued
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:
Oh!!!Yeah then come interface the saviour

Interface can be used for other different purposes but I feel in such situations interfaces are blessing


Well, I wouldn't go that far...

Can interfaces be used in many use cases that C++ uses multiple inheritance for? Yes. Can interfaces be used to replace multiple inheritance in all use cases easily? Of course not.

Henry
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you are actually describing, ST, is a mixin interface (informal term because you use it to mix in additional functionality).

Back to animals. I have seen a youtube video recently about a family who adopted a small ♀ hippo when it was a “little girl”, and that hippo now walks into and out of their home quite peacefully and friendly, in fact turning from a wild animal to a pet when she approaches that family. Dogs and cats are naturally wild and need to be tamed to be pets. That is exactly where a mixin interface would come in handy.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All you guys are missing a point
I my self had said that there may be more uses of interfaces but I feel it is used to its best in avoiding multiple inheritance
@Campbell :I am also very sad that people under value abstract method. Atleast they must be given some respect as they make polymorphism easily applicable
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Campbell for clearing things
I always knew you are great in explaining things.
I loved the example and my nickname "ST".its awesome
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In which case you should have called me CR

You're welcome
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!