Link to home
Start Free TrialLog in
Avatar of foongkim
foongkim

asked on

Interfaces, Class, Package

I am beginner to JAVA. I need to understand more about these terms more clearly. I have go throguth all the documentation in the Web pages yet still confussed with this terms.

I can't understanf clearly and imagine the situation for these 3 terms.

As I know, this 3 contains the library of all the methods of library in. ALl these 3 are library which contain all the built-in function. Am i correct?

I hope some of you can explain more to me in lay-man terms.

Avatar of foongkim
foongkim

ASKER

And when I start the JAVA coding, the first few lines, stated that;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

what is this all about? is it the java.io is a class or package or interfaces?
For this example, I used the 'public void doGet(HttpServletRequest request, HttpServletResponse response)' in my coding part.

So, what is this again? Public void...... response). Is this the class or method? or what is this?
ASKER CERTIFIED SOLUTION
Avatar of girionis
girionis
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for your fast responde.

javax.servlet.http is the package contain all the class like HttpServlet.... and in the HttpServlet class it's contain the methods called doGet(), doPost etc....
And if I import the javax.servlet.http, the I am able to access all the "things" inside. TQ so far.

But, under a certain packages, it's contains another things called "Interface". What is this Interface? Compare with the "Class"?

http://java.sun.com/products/servlet/2.1/api/packages.html
 An interface is nothing more than a "contract" between your class and the functionallity your class must have. By implementing an interface you agree to implement in your class all of the method signatures contained in the interface. Pay attention to the phrase "method signatures". Interfaces do not contain method implementations but only method signatures. It is up to your class to implement these methods according to your needs. FOr example imagine the following interface:

public interface Multiplication
{
    public void multiply(int number);
}

  As you can see there is no method body within the method in the interface but only the signature of the method. Now if you have a class Arithmetic you *must* implement the multiply method any way you wish:

public class Arithmetic implements Multiplication
{
    public void multiply(int number)
    {
        number = number *5;
    }
}

  while in another class you might want to implement it differently:

public class MoreArithmetic implements Multiplication
{
    public void multiply(int number)
    {
        number = number * 20;
    }
}

  You can see that the same method must appear within both of the classes but we can have two different implementations of the method.

  Hope it helps.
 An interface is nothing more than a "contract" between your class and the functionallity your class must have. By implementing an interface you agree to implement in your class all of the method signatures contained in the interface. Pay attention to the phrase "method signatures". Interfaces do not contain method implementations but only method signatures. It is up to your class to implement these methods according to your needs. FOr example imagine the following interface:

public interface Multiplication
{
    public void multiply(int number);
}

  As you can see there is no method body within the method in the interface but only the signature of the method. Now if you have a class Arithmetic you *must* implement the multiply method any way you wish:

public class Arithmetic implements Multiplication
{
    public void multiply(int number)
    {
        number = number *5;
    }
}

  while in another class you might want to implement it differently:

public class MoreArithmetic implements Multiplication
{
    public void multiply(int number)
    {
        number = number * 20;
    }
}

  You can see that the same method must appear within both of the classes but we can have two different implementations of the method.

  Hope it helps.
Hm.... still confussing. Any web-sites to expalin more? (Please suggest those in your bookmark).

========================================================

public class Arithmetic implements Multiplication
{
   public void multiply(int number)
   {
       number = number *5;
   }
}
========================================================
public class MoreArithmetic implements Multiplication
{
   public void multiply(int number)
   {
       number = number * 20;
   }
}

========================================================


I didn't find any differences in these 2 method. Only thr inside calculation part is different only.

Which one is method signarute and what is implements? (I think you responde wile be like this : my Godness, this little kids is terrible.... )

Ha...
hmmm, i think you better read a jave book first. You are not going to learn the language by only asking questions here. Better read a book or good tutorial first and if you still have questions, you can ask them here.
I don't have the time to search some good tutorials for you now, but the sun site http://developer.java.sun.com/developer/infodocs/ should be a good start(look at the java tutorial and New-to-Java center). There is also a popular free book "thinking in Java" that you can download at http://www.mindview.net/Books/TIJ/

P.S. don't give me points for this. This is just an advice. girionis answered your questions perfectly

Didier
Yes. I realised that. Thanks for your web-sites. TQ.
I hope I can get more useful web-sites instead of wasting time to hunt around. :)
 
Hmm, the free book is best. Any other suggestion for me to find this kind of book in the net??
Here you can find some more links to online books http://java.about.com/cs/books/index.htm
 The method signature is the:  public void multiply(int number). It says that there must exists a method that is public, returns nothing and accepts an integer as a parameter. Maybe in my example above I did not write down proper implementations so you can distinguish between them. What I wanted to show you is that the implementation can change and be different for the same signature method. Take a look:

=======================================================
public class Arithmetic implements Multiplication
{
  public void multiply(int number)
  {
      number = number *5;
  }
}
========================================================
public class MoreArithmetic implements Multiplication
{
    int multiplier = 5;
  public void multiply(int number)
  {
      multiplier = multiplier *2;
      System.out.println("Multiplying...");
      number = number * multiplier;
  }
}
========================================================

  Hopefully it is clearer to you now. There are two different implementations for the same method signature.

 Hope it helps.
Thanks anyway...