Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Interface Concept Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interface Concept" Watch "Interface Concept" New topic
Author

Interface Concept

Joy Vergis
Ranch Hand

Joined: Sep 14, 2009
Posts: 45
Please help me to understand the underlined :

You can declare variables as object references that use an interface rather than a class type. Any instance of any class that implements the declared interface can be stored in such a variable. When you call a method through one of these references, the correct version will be called based on the actual instance of the interface being referred to. This is one of the key features of interfaces.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11172
    
  16

there is no limit to the number of classes that can implement an interface. ClassA, ClassB, and ClassC might all implement the FredStuff interface.

I can then, in my code, make a reference type of FredStuff:

FredStuff fred = getActualObject(7);

now, the getActualObject(int x) method returns a different kind of object depending on the parameter. It may return a ClassA, a ClassB, a ClassC or some other class in the future that nobody has even written yet...but as long as it implements the FredStuff interface, it doesn't matter.

once i get the actual object, i can call the doFredStuff() method:

fred.doFredStuff();

the JVM will call the verison of doFredStuff appropriate for whatever type the object actually is at runtime. I don't have to know what it is when I write the code, or when I compile the code.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
I think that's a good description. I would like to add one thing.

An interface declaration says "Anything that implements this interface is guaranteed to have these methods in it". The Java compiler and runtime enforce this, so that you can depend on classes to have the methods their interfaces (and classes) indicate they have.

There are special cases, such as casting and "optional methods", but the above is the general idea.

rc
Devendra Walanj
Greenhorn

Joined: Apr 21, 2010
Posts: 12
There are special cases, such as casting and "optional methods", but the above is the general idea.


What are these special cases.
Optional Methods ! ... heard for the first time, can you please elaborate ?


Keep Moving Forward
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
Devendra Walanj wrote:
There are special cases, such as casting and "optional methods", but the above is the general idea.


What are these special cases.
Optional Methods ! ... heard for the first time, can you please elaborate ?


Look in the javadoc for java.util.AbstractList - you will find notations on several methods in that for (optional operation).

I think this was a bad idea. I suppose the Java Gods that did this had their reasons, but I think an optional operation renders the interface MUCH weaker than it was. Before "optional operations", you were protected against run-time errors due to unimplemented code, which was a large part of the whole point behind a strongly typed language. Now, if someone decides to replace a library and they happen to use lists (sound likely?), it is possible that code that worked before won't work now, because the caller depended on these optional operations, and there is no way I know of to tell whether a particular implementation supplies them or not.

rc
Joy Vergis
Ranch Hand

Joined: Sep 14, 2009
Posts: 45
Hi,
I am reading through the interface concept. I am writing a code in which I have written interfacess1 St=new Eg_one(); in main method. I wrote this line to check whether the code compiles or not. It compiled successfully.









Please help me to understand when i can use such reference variables.

Thanks,
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38087
    
  22
What is the problem? You have two interfaces, which the class implements. That is correct Java™. Apart from the inappropriate use of capital letters and underscores in your method names, but that's a style problem.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interface Concept
 
Similar Threads
Accessing Implementations Through Interface References
Requirement of Interface in Java
Interface Q
How do you have an object of an Interface
Abstract Classes and Interfaces