Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 02-26-2005, 09:55 AM   #1 (permalink)
 
True Techie

Join Date: Sep 2004

Posts: 156

ZoneFire

Default Java Question (Dynamic Binding)

Quote:
hour = ( ( h >= 0 && h < 24 ) ? h:0 )
may i know what is the function of the "?" in the syntax above?
thank you
__________________
The important thing is not to stop questioning...
Albert Einstein
ZoneFire is offline  
Old 02-26-2005, 04:37 PM   #2 (permalink)
 
Super Techie

Join Date: Jan 2005

Posts: 295

gab00n

Default

Well it is like an if statement, it's name is the conditional operator in
C++, probably the same in java.

So if you wanted to convert it into an if statement it would look like this.
Code:
 
 if(h>= 0 && h < 24)
{
    hour = h;
}
else
{
    hour = 0;
}
It is very handy for keeping the code nice and neat, i use it very often as a result. So the stuff infront of the ? is the condition that must be met in order for it to be true and for hour to = h, the stuff after : is the false part and hour will be assigned 0.
__________________
\"Today\'s scientists have substituted mathematics for experiments, and they wander off through equation after equation, and eventually build a structure which has no relation to reality.\" Nikola Tesla
gab00n is offline  
Old 02-26-2005, 08:29 PM   #3 (permalink)
 
True Techie

Join Date: Sep 2004

Posts: 156

ZoneFire

Default

so that means "?" is used to replace if statements...
hence, the code will look neat and better organised...
thank you
__________________
The important thing is not to stop questioning...
Albert Einstein
ZoneFire is offline  
Old 02-27-2005, 07:25 AM   #4 (permalink)
 
True Techie

Join Date: Sep 2004

Posts: 156

ZoneFire

Default

can someone tell me about what is dynamic binding in Java?
i am not clear with this concept...
thank you
__________________
The important thing is not to stop questioning...
Albert Einstein
ZoneFire is offline  
Old 02-27-2005, 05:04 PM   #5 (permalink)
 
Super Techie

Join Date: Jan 2005

Posts: 295

gab00n

Default

It's the process of binding a function call to a particular instance method of a class. It is needed when the compiler determines that there is more than one possible instance method from one of many class's that could be executed by a particular function call. It is used so that you don't have to write conditional statements to choose which code should be executed.
__________________
\"Today\'s scientists have substituted mathematics for experiments, and they wander off through equation after equation, and eventually build a structure which has no relation to reality.\" Nikola Tesla
gab00n is offline  
Old 03-01-2005, 11:48 AM   #6 (permalink)
 
True Techie

Join Date: Sep 2004

Posts: 156

ZoneFire

Default

thanks for your explanation, gab00n
besides, i got question about Interfaces, can someone explain the concept of Interfaces in Java to me?
i just knew that it can be used to replace the Multiple Inheritance, but how it works?

thank you
__________________
The important thing is not to stop questioning...
Albert Einstein
ZoneFire is offline  
Old 03-01-2005, 04:18 PM   #7 (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

An interface is just a set of methods. You use it, so that you can know a certain object contains a certain set of methods without having to know the object. And yes, it's used in multiple instead of multiple inheritence.
__________________

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-01-2005, 07:31 PM   #8 (permalink)
 
True Techie

Join Date: Sep 2004

Posts: 156

ZoneFire

Default

oh..thanks
but then, how we can implement interface in our program?
how both of them are related?

thank you
__________________
The important thing is not to stop questioning...
Albert Einstein
ZoneFire is offline  
Old 03-02-2005, 09:44 AM   #9 (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

Say I have an interface named MyInterface which contains two methods doSomething() and doSomethingElse() I would create the interface like this
Code:
public interface MyInterface{
    void doSomething();
    void doSomethingElse();
}
Then I'd use it like this
Code:
public class SomeClass implements MyInterface{
    public void doSomething(){
         // do some work
    }

    public void doSomethingElse(){
        // do some work here too
    }

    //
    // Anything else I want this class to have can go here
    //
}
Then to actually use that class I'd do something like
Code:
public class SomeOtherClass{
     public static void main(String[] args){
          // I can either use a direct instantiation like this
          SomeClass obj = new SomeClass();
          obj.doSomething();
          // When using direct instantiation I have direct access to
          // everything that class has to offer, all methods (non private)
          // all fields (non private) and so on and so forth

          // Or I can use indirect instantiaion
          MyInterface obj = new SomeClass();
          obj.doSomethingElse();
          // now I only have access to the interface methods
    }
}
Interfaces are good with multiple inheritence, abstraction, and reflection. Hope this clarifies it a bit. Oh, and you can use as many interfaces as you like, just separate them by commas.
__________________

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-02-2005, 12:07 PM   #10 (permalink)
 
True Techie

Join Date: Sep 2004

Posts: 156

ZoneFire

Default

wow, it is really nice...
the explanation is clear and understandable
now, i can grab the concept of interfaces in Java.
thanks a lot, Iron_Cross
__________________
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