Java Question

Status
Not open for further replies.
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.
 
so that means "?" is used to replace if statements...
hence, the code will look neat and better organised...
thank you :)
 
can someone tell me about what is dynamic binding in Java?
i am not clear with this concept...
thank you :)
 
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.
 
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 :)
 
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.
 
oh..thanks
but then, how we can implement interface in our program?
how both of them are related?

thank you ;)
 
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.
 
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 :)
 
Status
Not open for further replies.
Back
Top Bottom