Calling a Overriding method in a abstract class
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi all,
I have basic question which confused me. I am pasting the code below
Here the doit() method is overridden by class B. Now how do I access the doit() method of class A, from class B.
Thanks in Advance,
Regards,
Sivaraman.L
I have basic question which confused me. I am pasting the code below
Here the doit() method is overridden by class B. Now how do I access the doit() method of class A, from class B.
Thanks in Advance,
Regards,
Sivaraman.L
Sivaraman Lakshmanan
Ranch Hand
Posts: 231
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi All,
In the previous post the class B code is wrong so I am posting it again.
Regards,
Sivaraman.L
In the previous post the class B code is wrong so I am posting it again.
Regards,
Sivaraman.L
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
It's unclear what you intend this to mean: "Now how do I access the doit() method of class A, from class B."
1. Are you trying to invoke A's implementation of doit as a subroutine from within one of B's methods, say its version of doit? Use super:
2. Or are you confused why the following give the same results?
In both cases, B's implementation of doit is invoked because the object being passed the message "doit" is an instance of B (because you wrote new B() twice).
1. Are you trying to invoke A's implementation of doit as a subroutine from within one of B's methods, say its version of doit? Use super:
2. Or are you confused why the following give the same results?
In both cases, B's implementation of doit is invoked because the object being passed the message "doit" is an instance of B (because you wrote new B() twice).
There is no emoticon for what I am feeling!
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Actually What happen every time when you write such a code
e.g.
It will search automatically in Base Class first and if there present then execute that method otherwise it checks the method declaration in Derived Class. This is the way of Execution. So this is a known behaviour of JAVA. SO don't be surpriced too much.
.
Hope You will be understand this.

e.g.
It will search automatically in Base Class first and if there present then execute that method otherwise it checks the method declaration in Derived Class. This is the way of Execution. So this is a known behaviour of JAVA. SO don't be surpriced too much.
.
Hope You will be understand this.
posted 19 years ago
Thats actually the opposite of what will happen.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
It will search automatically in Base Class first and if there present then execute that method otherwise it checks the method declaration in Derived Class...
Thats actually the opposite of what will happen.
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Sivaraman Lakshmanan
Ranch Hand
Posts: 231
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi all,
Thanks for your replies. I want to call the A's doit() implementation from class B but not from any method in B, but from the B's main method.
something like...
Regards,
Sivaraman.L
Thanks for your replies. I want to call the A's doit() implementation from class B but not from any method in B, but from the B's main method.
something like...
Regards,
Sivaraman.L
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
You can do it as follows:
OR
OR
posted 19 years ago
This will not produce the results requested. Both examples will still call B's doit() method. In fact, I do not think there is any way to call A's doit() method from main unless you have an instance of A. An instance of B will always invoke B's doit() method, even if you have a reference to an A pointing to it.
Layne
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Originally posted by Ajay A Patil:
You can do it as follows:
OR
This will not produce the results requested. Both examples will still call B's doit() method. In fact, I do not think there is any way to call A's doit() method from main unless you have an instance of A. An instance of B will always invoke B's doit() method, even if you have a reference to an A pointing to it.
Layne
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Yoy are right Layne,
You can call A's doit() either as a super.doit() in a method or with instance of A but in this case as A is abstract 2nd option is gone.
Shrinivas
You can call A's doit() either as a super.doit() in a method or with instance of A but in this case as A is abstract 2nd option is gone.
Shrinivas
| Onion rings are vegetable donuts. Taste this tiny ad: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |







