Virtual Keyword
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Why is "virtual" keyword not supporte in Java ?
The first reason i am aware of is that as Java does not support Multiple Inheritance, the Diamond shape of class heirarchy will not occur in Java, But then i was told there is another more important reason for it, Any idea what is it ?
The first reason i am aware of is that as Java does not support Multiple Inheritance, the Diamond shape of class heirarchy will not occur in Java, But then i was told there is another more important reason for it, Any idea what is it ?

Stephen D'Souza
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
What do you mean by "virtual" is not supported ?
An interface's methods are implicitly "pure virtual" methods.
Abstract classes also contains implicitly "virtual" methods, implemented by classes extending it.
I don't see the relation between "virtual" and multiple inheritance.
An interface's methods are implicitly "pure virtual" methods.
Abstract classes also contains implicitly "virtual" methods, implemented by classes extending it.
I don't see the relation between "virtual" and multiple inheritance.
[My Blog]
All roads lead to JavaRanch
Stephen D'Souza
Greenhorn
Posts: 3
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I am talking of the "virtual" keyword.
Consider this example of Inheritance in C++,
A,B,C and D are Classes
Classes B and C are derived from class A. Class D is derived from Classes B and C
If the above example is Checked properly then each object of D will have 2 Instances of Objects of Class A (one coming from B other from C).
So get rid of that problem we have to declare Class A as "virtual" so that D is forced to have only one instance of A.
Now my question is Java does not support "virtual" keyword. One reason is that the above situation would never arise in a Java Program as Java does not support Multiple Inheritance, I wanna know about the other reason ???
Consider this example of Inheritance in C++,
A,B,C and D are Classes
Classes B and C are derived from class A. Class D is derived from Classes B and C
If the above example is Checked properly then each object of D will have 2 Instances of Objects of Class A (one coming from B other from C).
So get rid of that problem we have to declare Class A as "virtual" so that D is forced to have only one instance of A.
Now my question is Java does not support "virtual" keyword. One reason is that the above situation would never arise in a Java Program as Java does not support Multiple Inheritance, I wanna know about the other reason ???
Stephen D'Souza
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
This is a Java Forum. not a C++ forum. Still, to answer your question, here are some C++ fundas :
The keyword virtual in C++ is used in two different contexts. One is for denoting virtual base classes in multiple inheritence. This usage doesnot
exist in Java since Java doesnot support multiple inheritence.
The second usage is to denote methods that will have run time binding. In that context, all non-private and non-final instance methods in java are dynamically bound. Thus, there is no need of a virtual keyword in this context too.
finally, one might ask why java doesnot allow "selective" runtime binding of methods by explicitly providing a construct like "virtual". In other words, why are all the methods "run time bound" by default? The reason is that C++ and Java are based on totally different philosophies. C++ belives "Don't pay for what you don't use" whereas Java believes that "Programmer should think about writing clean code whereas efficiency, to a great extent, is the JVM's headache".
The keyword virtual in C++ is used in two different contexts. One is for denoting virtual base classes in multiple inheritence. This usage doesnot
exist in Java since Java doesnot support multiple inheritence.
The second usage is to denote methods that will have run time binding. In that context, all non-private and non-final instance methods in java are dynamically bound. Thus, there is no need of a virtual keyword in this context too.
finally, one might ask why java doesnot allow "selective" runtime binding of methods by explicitly providing a construct like "virtual". In other words, why are all the methods "run time bound" by default? The reason is that C++ and Java are based on totally different philosophies. C++ belives "Don't pay for what you don't use" whereas Java believes that "Programmer should think about writing clean code whereas efficiency, to a great extent, is the JVM's headache".
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Java does not have a virtual keyword because all non-static methods use dynamic binding and and therefore are be default virtual. In Java, the programmer doesn't have to decide whether to use dynamic binding.
Stephen D'Souza
Greenhorn
Posts: 3
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Thank you Guys for Helping me out here.
Stephen D'Souza
| LOOK! OVER THERE! (yoink) your tiny ad is now my tiny ad. Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |











