mardi 22 avril 2014

Est-ce que vous pouvez écrire des fonctions virtuelles en Java ? -Débordement de pile


I wondered if you can write virtual functions in Java. Can I see some examples of Java virtual functions?




From wikipedia



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.





All functions in Java are virtual by default.


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.




Can you write virtual functions in Java?


Coding interviewers love this question. Yes. Although Java does NOT have a virtual keyword, Java has virtual functions and you can write them.


In 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.




All non-private instance methods are virtual by default in Java.


In C++, private methods can be virtual. This can be exploited for the non-virtual-interface (NVI) idiom. In Java, you'd need to make the NVI overridable methods protected.


From the Java Language Specification, v3:



8.4.8.1 Overriding (by Instance Methods) An instance method m1 declared in a class C overrides another instance method, m2, declared in class A iff all of the following are true:



  1. C is a subclass of A.

  2. The signature of m1 is a subsignature (§8.4.2) of the signature of m2.

  3. Either * m2 is public, protected or declared with default access in the same package as C, or * m1 overrides a method m3, m3 distinct from m1, m3 distinct from m2, such that m3 overrides m2.





Yes, you can write virtual "functions" in Java.



I wondered if you can write virtual functions in Java. Can I see some examples of Java virtual functions?



From wikipedia



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.




All functions in Java are virtual by default.


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.



Can you write virtual functions in Java?


Coding interviewers love this question. Yes. Although Java does NOT have a virtual keyword, Java has virtual functions and you can write them.


In 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.



All non-private instance methods are virtual by default in Java.


In C++, private methods can be virtual. This can be exploited for the non-virtual-interface (NVI) idiom. In Java, you'd need to make the NVI overridable methods protected.


From the Java Language Specification, v3:



8.4.8.1 Overriding (by Instance Methods) An instance method m1 declared in a class C overrides another instance method, m2, declared in class A iff all of the following are true:



  1. C is a subclass of A.

  2. The signature of m1 is a subsignature (§8.4.2) of the signature of m2.

  3. Either * m2 is public, protected or declared with default access in the same package as C, or * m1 overrides a method m3, m3 distinct from m1, m3 distinct from m2, such that m3 overrides m2.




Yes, you can write virtual "functions" in Java.


0 commentaires:

Enregistrer un commentaire