• 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 Concept

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ?
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
And then the flying monkeys attacked. My only defense was this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic