• 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's an interface?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone!

I've been reading about interfaces topics from SJCP Study Guide (Sierra-Bates).
My question is they are a class, object or something else?

My first impression of them is they aren't classes ergo they are not objects, and then what suppose are they?

My ideas...
Interfaces are not a class because don't inherits from any class, inclusive Object class?
Interfaces are not a class because haven't constructors and not participate in inheritance object tree.
Interfaces are not objects because can't instantiate them.

But why I can make a code like below?



 
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have not understood the doubt clearly. Remove public in 1st line, otherwise you get a compiler error. You have only created a reference of type Jumpable and not an object of that type. Try changing line 12 to "Object refObject = new Jumpable(); " and see what happens. Note that interfaces cant have a constructor.

See these links :
http://download.oracle.com/javase/tutorial/java/concepts/interface.html

http://www.cpe.eng.cmu.ac.th/wp-content/uploads/javainterface.pdf
 
Ranch Hand
Posts: 180
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch

There are two kinds of types in Java: primitive types and reference types. An interface is a reference type; just like a class. However, the difference is that an interface is an abstract reference type. An abstract reference type is a reference type that is not fully defined; it is just an abstraction. Because it is not fully defined, an abstract reference type cannot be instantiated. It can be fully defined by:
  • extending it - extend an abstract class and provide concrete implementations for all of its abstract methods
  • implementing it - implement an interface and provide concrete implementations for all of its abstract methods

  •  
    Rahul Sudip Bose
    Ranch Hand
    Posts: 637
    Eclipse IDE Firefox Browser Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    continuing what ogeh ikem said :

    Here is a demo of abstract class :



    Error :


    The output of the program is :
    Abstract class constructor
    Concrete class constructor


     
    Joan Alonso
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks guys for your comments and observations.

    First of all, the code must have been something like this:
    And I was sure interfaces aren't a class but my doubt start when I saw that the compiler legally compile the assignment interface's reference (jumpable) to a Object reference (refObject), but now I'm agree with the Ogeh's answer, just there exists two types the compiler recognize: primitives types and reference types, I think the assignment is legally because at last the reference jumpable must have an object assigned (heap JVM) and then the refObject can handle this object and invoke just the Object's methods, in otherwise the refObject will be usless (have no object to manipulate), the jumpable reference just plays as a link between the refObject and the object that jumpable have assigned.

    My conclusions: the interfaces aren't a class, aren't an object, and don't belong to the object's tree hierarchy.. et al., just they are a reference type to the compiler in the code above.

    Thanks guys,
    I hope understand my wording, english isn't my native language.
    reply
      Bookmark Topic Watch Topic
    • New Topic