Solved

Interfaces, Class, Package

Posted on 2002-04-03
13
225 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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
 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
hashmap order 17 43
glassfish admin console 1 23
restrict decimal places for double datatype 10 34
Find out all repos that a user is most active on Github 1 35
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

696 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