Solved

Interfaces, Class, Package

Posted on 2002-04-03
13
190 Views
Last Modified: 2011-04-14
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.

0
Comment
Question by:foongkim
  • 7
  • 4
  • 2
13 Comments
 

Author Comment

by:foongkim
ID: 6916997
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?
0
 

Author Comment

by:foongkim
ID: 6917002
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?
0
 
LVL 35

Accepted Solution

by:
girionis earned 50 total points
ID: 6917200
 I did not quite understand your first question.

  import java.io.*; tells the virtual machine to look for specific classes in the java.io package of classes. The java.io package contains classes for input and output functionallity. For example the class File is within this package. Each class resides under the io folder which resides under the java folder. For instance the class File is in the structure java/io/File.

  The public void doGet(HttpServletRequest request, HttpServletResponse response) is a public method (it can be accessed by any class in any package) that returns nothing to the caller (that's why it is void). This method defined in a class called HttpServlet. This class resides under the
javax.servlet.http package. By doing a import javax.servlet.http.*; it means that you can use all the classes from this package - including the HttpServlet and all of its methods (including the public void doGet(HttpServletRequest request, HttpServletResponse response)). What this methods does is to perform the HTTP GET operation. You pass two parameters to this method: The HttpServletRequest that encapsulates the request to the servlet and the HttpServletResponse that encapsulates the response from the servlet

  Hope it helps.
   
0
 

Author Comment

by:foongkim
ID: 6917215
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
0
 
LVL 35

Expert Comment

by:girionis
ID: 6917252
 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.
0
 
LVL 35

Expert Comment

by:girionis
ID: 6917261
 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.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:foongkim
ID: 6917317
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...
0
 
LVL 2

Expert Comment

by:DidierD
ID: 6917509
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
0
 

Author Comment

by:foongkim
ID: 6917554
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. :)
 
0
 

Author Comment

by:foongkim
ID: 6917557
Hmm, the free book is best. Any other suggestion for me to find this kind of book in the net??
0
 
LVL 2

Expert Comment

by:DidierD
ID: 6917744
Here you can find some more links to online books http://java.about.com/cs/books/index.htm
0
 
LVL 35

Expert Comment

by:girionis
ID: 6919069
 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.
0
 

Author Comment

by:foongkim
ID: 6961624
Thanks anyway...
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now