Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 03-08-2005, 11:44 AM   #11 (permalink)
 
True Techie

Join Date: Sep 2004

Posts: 156

ZoneFire

Default

a questio here too...
what does it mean by "interfaces cannot contain concrete methods"?
but an abstract class can contain concrete methods?
what does it mean by concrete methods?
thank you
__________________
The important thing is not to stop questioning...
Albert Einstein
ZoneFire is offline  
Old 03-08-2005, 05:43 PM   #12 (permalink)
 
Ultra Techie

Join Date: Sep 2003

Location: Bamberg, Germany

Posts: 549

Iron_Cross

Send a message via ICQ to Iron_Cross Send a message via MSN to Iron_Cross Send a message via Yahoo to Iron_Cross
Default

A concrete method is one where it actually has functionallity. For example this is concrete method:
Code:
public void printSomething()
{
    System.out.println("Printing a string of text characters!");
}
An abstract or non-concrete is one where the return type, name, and arugment list are known but what the method actually does is NOT known.
Code:
void printSomething();
In this case the return type is void, the name is printSomething and the aguement list contains nothing.

Now in an interface you're basically just describing a method. When you implement an interface you are creating that described method. That way you are guarunteed to be able to use it, no matter how it works.

An abstract class is something completely different. It's bascially just a place holder in a hierarchy tree of classes. An abstract class can not be instantiated, but can be inherited from.

For example, if I wanted to create a two classes NumberTextBox and LetterTextBox which only except numbers or letters, respectivly, I would first create a class called TextBox which has all the basic functionality that a text box needs, such as getting keyboard input, printing to the screen, stuff like that. But I don't want anyone to be able to use the class TextBox, just NumberTextBox and LetterTextBox.

Instead of writing the exact same basic functionality into both NumberTextBox adn LetterTextBox I would just inherit both of them from TextBox. But to ensure that people can't use TextBox I would mark that class as abstract.

The reason an abstract class has concrete methods is because you can't inherit from a method that doesn't exist yet (i.e. a non-concrete method) because it doesn't do anything.

I hope that helps clarify it a bit. If you have any more questions just ask
__________________

See today\'s Penny-Arcade!(May contain foul lanuage)
Pain is weakness leaving the body.

PM Me for my MSN
Iron_Cross is offline  
Old 03-09-2005, 08:01 PM   #13 (permalink)
 
True Techie

Join Date: Sep 2004

Posts: 156

ZoneFire

Default

So, when using the Abstract Class, i can inherit from the class and define the concrte methods in the class. However, i cant instantiate a new class from Abstract class.

While using interface, i just defined the method but without any implementation.When the interface is implemented in a client class, we need to specify the implementation and algorithm to do the work.

am i right?
thank you, Iron_Cross
__________________
The important thing is not to stop questioning...
Albert Einstein
ZoneFire is offline  
Old 03-09-2005, 08:15 PM   #14 (permalink)
 
Monster Techie

Join Date: Sep 2004

Posts: 1,447

Alex81388

Send a message via AIM to Alex81388
Default

Think of the interface as a guidelines or rules book. The interface simply says what methods are required to use it, whith their return typs and perameters.

You can only actually PUT TO USE one of the methods in another class, but you will have to have the others present (even if they do absolutely nothing) so the interface sees all of its required methods are there.
__________________
Sig removed due to foul language. Please read the rules regarding the allowable content of sigs before reposting your COMPLIANT sig.
~Trotter
4/21/06
Alex81388 is offline  
Old 03-09-2005, 08:32 PM   #15 (permalink)
 
Super Techie

Join Date: Mar 2005

Posts: 259

C.Ingram

Send a message via AIM to C.Ingram Send a message via Yahoo to C.Ingram
Default

Quote:
Originally posted by ZoneFire
So, when using the Abstract Class, i can inherit from the class and define the concrte methods in the class. However, i cant instantiate a new class from Abstract class.

While using interface, i just defined the method but without any implementation.When the interface is implemented in a client class, we need to specify the implementation and algorithm to do the work.

am i right?
thank you, Iron_Cross
Pretty much, yes. The former (can) pretty much define an incomplete class. You inherit from the class, and fill in the blanks. The latter is similar, but to a lesser extent.

Use abstract classes when you want to provide some code. Use interfaces when you don't want to provide some code, and want the programmer to do *everything* him/herself.

http://java.sun.com/docs/books/tutor.../abstract.html
http://www.javaworld.com/javaworld/j...-abstract.html
__________________
Christopher Ingram
Principal Consultant, Souken Group, LLC.
C.Ingram@SoukenGroup.com
(856) 392 5244 -- (866) Go Souken
C.Ingram is offline  
Old 03-09-2005, 11:25 PM   #16 (permalink)
 
Ultra Techie

Join Date: Sep 2003

Location: Bamberg, Germany

Posts: 549

Iron_Cross

Send a message via ICQ to Iron_Cross Send a message via MSN to Iron_Cross Send a message via Yahoo to Iron_Cross
Default

Quote:
Originally posted by ZoneFire
So, when using the Abstract Class, i can inherit from the class and define the concrte methods in the class. However, i cant instantiate a new class from Abstract class.

While using interface, i just defined the method but without any implementation.When the interface is implemented in a client class, we need to specify the implementation and algorithm to do the work.

am i right?
thank you, Iron_Cross
You're right.
__________________

See today\'s Penny-Arcade!(May contain foul lanuage)
Pain is weakness leaving the body.

PM Me for my MSN
Iron_Cross is offline  
Old 03-15-2005, 12:32 PM   #17 (permalink)
 
True Techie

Join Date: Sep 2004

Posts: 156

ZoneFire

Default

thanks a lot, Iron_Cross
but then i have some problem writing a class.
when i compiled the source code, the following error messages is shown.

Quote:
D:\My Java\Rational.java:5: Rational is not abstract and does not override abstract method longValue() in java.lang.Number

public class Rational extends Number {
^
1 error
can someone tell me what is the problem?
besides, do we have any method to check whether the input is an integer?

thank you
__________________
The important thing is not to stop questioning...
Albert Einstein
ZoneFire is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On