Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

SCJP Interfaces

 
Ranch Hand
Posts: 282
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
This statement can be found in the SCJP book.

"You implement an interface by properly and concretely overriding all of the
methods defined by the interface."

So if you implement an interface you must override the methods. Overriding mean to write your own code. So what's the use of implementing an interface if you have to write your own code that overrides the code that has already been written.

inside the Jumper interface we have
public Jump(int height){
//make the kangaroo jump
}

class Kangaroo() implements Jumper{

public Jump(int height){
//make the kangaroo jump
}

}
SO if we cannot use the orginal code then what the use? When I used the MouseListener I had to write my own code. I used GetX and GetY but those were not funcitons of MouseListener. I used them inside of the MousePressed(.....) method. I have read three books on the subject and none of them tell me why I should use them. It good OO practice, it hides the details is what it says, but how do I benefit from it? I know that I have asked this before but after reading the SCJP Chapter 2 I have rethought the idea.
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java Tutorial:Interfaces

Java World: Using Interfaces
[ September 14, 2007: Message edited by: Edwin Dalorzo ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by James Hambrick:
inside the Jumper interface we have
public Jump(int height){
//make the kangaroo jump
}


This does not work. In an interface, you can only declare methods. You can not provide an implementation of a method inside an interface.
 
James Hambrick
Ranch Hand
Posts: 282
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
Yea, I figured that out, just trying to type something up write quick as an example.
 
James Hambrick
Ranch Hand
Posts: 282
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
So I read through most of that link that was given and an interface is a contract, it gives a list of methods that any class that implements it must override. So I could skip the interface part of it, define the method inside the class and be done with it. I read something about using an interface as a type and casting objects to it. You can then use the metods as long as they are in the same class. I can still do that without an interface as far as I know. But I may just be confused. At one time I thought that an interface was the skeleton of another class and when you implemented an interface and called a method it ran the code from insode that class. But I have not heard that anywhere.
 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you just misunderstood the use of interface. Trust me, interface is a good thing, not a bad thing as you imagined. Interface also is an important part of polymorphism. There are lots of interfaces that comes prepackaged with java and j2ee that are ready for use. Meaning you don't have to write any code for it. Just use it. Because someone else has already written the code for you. I think you will find out the purpose of interface later once you leaned more about java.
[ September 14, 2007: Message edited by: Tony Smith ]
 
Sheriff
Posts: 67671
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interfaces free the caller from having to know what class specifically implements the interface.

Imagine a program that gets its data from a database. Even though JDBC makes calling SQL statements easy, the actual SQL can vary from DB to DB. If the data store is implemented as an interface, let's say with methods such as getMeThis() and getMeThat(), the actual implementation of the class that implements that interface is moot.

Therefore, any number of classes that implement the interface, whether it be for MySQL, or PostgreSQL, or DBASE or any other database can be written and the caller of the interface method doesn't know or care which class is being used.

The interface defines a contract that all database access classes must follow, but the caller of that contract doesn't need to know what was used.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me try with an example. Let's say you are creating a video game. In this game you have magicians (like Gandalf). Magicians know spells that they can cast on creatures (friends or foes) or themselves. Different magicians could know or learn different spells and new spells could be created in the future.

So, let's define an interface for Spells, because we still do not know what are all the possible implementations of all spells, right?



Ok, now lets implement a few Spells, other programmers or yourself in other releases of your game in the future could extend the implementation of available Spells.

Notice how every Spell implements the same method, but every spell implements it in a different way.



Now, we need to define a Magician class, capable to cast spells.



Notice how I used the interface Spell, instead of the concrete implementations. If I had the concrete implementations, then a Magician could only invoke the three basic spells we created. But since I used the interface, in the future we could add any kind of new spell and the Magician would be capable to cast them.

Let's see how we can use this class:



Does this help somehow?
[ September 14, 2007: Message edited by: Edwin Dalorzo ]
 
James Hambrick
Ranch Hand
Posts: 282
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 think I kinda get it. So if a class implements the interface its going to give the inner workings of what the interface defined. any class that does not implement the interface can use it as long as there's some reference to the class that puts the inner working into that method.
[ September 14, 2007: Message edited by: James Hambrick ]
 
James Hambrick
Ranch Hand
Posts: 282
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
This is what I would of done if I was to write such a progam(great crateive example BTW).

Please try and disreagard any errors in the code and get the jest fo what I am saying.


Basically having one method called and then determining what spell needs to be done. Instead of the interface and the classes for the spells.
[ September 14, 2007: Message edited by: James Hambrick ]
 
You get good luck from rubbing the belly of a tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic