Monday, May 12, 2014

Java

1. In Java, all non-static methods are by default "virtual functions." Only methods marked with the keyword final, which cannot be overridden, along with private methods, which are not inherited, are non-virtual.


You have to go out of your way to write non-virtual functions by adding the "final" keyword.
This is the opposite of the C++/C# default. Class functions are non-virtual by default; you make them so by adding the "virtual" modifier.
n object-oriented programming, a virtual function or virtual method is a function or method whose behavior can be overridden within an inheriting class by a function with the same signature. This concept is a very important part of the polymorphism portion of object-oriented programming (OOP).
Answering an architecture question about a specific language like this requires great communication skills and a deep mastery of underlying principles of the Java compiler, specifically interfaces, abstract classes, and how inheritance works.
Clarifying question: "Virtual means an extensible or simulated artefact, do you mean you want to know if Java can represent extensible or simulated artefacts? Of Course! There are many ways!"
Then guide the interviewer onto something specific that can be answered.
Yes you can write virtual functions in Java with interfaces.
Java interface methods are all "pure virtual" because they are designed to be overridden. For example:
interface Bicycle {         //the function applyBrakes() is virtual because
    void applyBrakes();     //functions in interfaces are designed to be 
}                           //overridden.

class ACMEBicycle implements Bicycle {
    public void applyBrakes(){               //Here we implement applyBrakes()
       System.out.println("Brakes applied"); //function, proving it is virtual.
    }
}
Yes you can write virtual functions in Java with abstract classes.
Java Abstract classes contain implicitly "virtual" methods, implemented by classes extending it. For Example:
abstract class Dog {                   
    final void bark() {               //bark() is not virtual because it is 
        System.out.println("woof");   //final and if you tried to override it
    }                                 //you would get a compile time error.

    abstract void jump();             //jump() is a virtual function because it
}                                     //is part of an abstract class and isn't
                                      //final.  
class MyDog extends Dog{
    void jump(){
        System.out.println("boing");    //here jump() is being overridden, a 
    }                                   //demonstration that it is virtual.
}
public class Runner {
    public static void main(String[] args) {
        MyDog myDog = new MyDog();       //instantiating myDog
        myDog.jump();                    //calling the overridden function jump()
    }
}
You can force a function to NOT be virtual in a generic class by making it final
For example:
class myJavaFoobarClass {

    final boolean giveMeTrueFunction()   //this Java function is NOT virtual
    {                                    //because final keyword prevents this
        return true;                     //function from being modified in a
    }                                    //subclass.

    boolean isItRainingFunction()   //this Java function IS virtual because
    {                               //without the final keyword, the function
        return false;               //can be overridden in a subclass.
    }
}
I got this interview question from a programming Shop in Boston MA in the form: "Explain the differences in how Java programmers use final and finalize, and explain how they modify variables and objects. What they are looking for is not that you can regurgitate the specific rules, but that you can reason correctly and consistently about how the java compiler interprets these keywords, even if you haven't used them

No comments:

Post a Comment