|
-
Sep 29th, 2002, 11:15 PM
#1
Thread Starter
Addicted Member
I don't know what to name this topic
class X
{
public:
double A(double x)
{
return x*x;
}
double B(double x)
{
return A(x)/2;
}
};
class Y ublic X
{
public:
double A(double x)
{
return x*x*x;
}
};
void main()
{
Y y;
cout<<y.B(3);
}
this gives 4.5
but in java
public class X
{
public:
double A(double x)
{
return x*x;
}
double B(double x)
{
return A(x)/2;
}
}
public class Y extends X
{
public:
double A(double x)
{
return x*x*x;
}
}
public class App
{
public static void main(String s[])
{
Y y=new Y();
System.out.println(y.B(3));
}
}
this gives 13.5
Why
Then what is the use of interface in java?
-
Sep 29th, 2002, 11:17 PM
#2
Frenzied Member
-
Sep 30th, 2002, 12:50 PM
#3
Try this in the C++ part:
Code:
class X
{
public:
virtual double A(double x)
{
return x*x;
}
double B(double x)
{
return A(x)/2;
}
};
class Y : public X
{
public:
virtual double A(double x)
{
return x*x*x;
}
};
void main()
{
Y y;
cout<<y.B(3);
}
It now should output 13.5 too.
Methods in Java are virtual by default. In C++ they are not because it would often produce unnecessary overhead. In Java this isn't important as in interpreted (byte-)code virtual functions don't create overhead.
If you don't know what virtual means look up polymorphism in your favourite C++ tutorial.
Z: It is the right forum. He's more likely to get an answer here than in the Java forums (why the hell should any Java programmer know about virtual functions or that there is something else unless he/she comes from a C++ background).
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 30th, 2002, 02:45 PM
#4
Fanatic Member
Hehe, I was about to say "virtual" was missing but I guess you beat me at it 
[EDIT]
Doesnt it just overload the function if there is no virtual and override it when there is? So, unless you say something like Y::A(x)/2 in the base function (and this wont work inline because Y is unknonwn at the time), it will always resolve to using X::A(x)/2!!
Last edited by MoMad; Sep 30th, 2002 at 02:49 PM.
-
Sep 30th, 2002, 03:11 PM
#5
Frenzied Member
Originally posted by CornedBee
Z: It is the right forum. He's more likely to get an answer here than in the Java forums (why the hell should any Java programmer know about virtual functions or that there is something else unless he/she comes from a C++ background). [/B]
True, forgive, it was too early in the morning =).
Z.
-
Sep 30th, 2002, 03:32 PM
#6
Mo: Y:: won't ever work in the base class because it's not guaranteed that it is really a Y (might be a X or even a Z derived from X too). It will resolve to X:: except if it's virtual, then it will resolve to ActualType::
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 30th, 2002, 03:37 PM
#7
Fanatic Member
isnt it something like typeof(*this):: or yea something to do with the object and not necesarily the class itself. And the Y:: was just for example. I meant it has to be able to do that kind of forward declaration or something.
-
Sep 30th, 2002, 11:00 PM
#8
Thread Starter
Addicted Member
I know it works with virtual function.
But why it works with java.I think only using interface it should work.
-
Oct 1st, 2002, 03:30 AM
#9
No, extends also uses only virtual functions.
But why exactly would better be asked in the Java forum.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|