I have 2 programs here.But I cant understand how we get the results in both,. 1)public void amethod(){ System.out.println("base"); } jbase(){ amethod(); } } public class Rtype extends jbase{ int i=1; public static void main(String arg[]){ jbase b=new Rtype(); System.out.println(b.i); b.amethod(); } public void amethod(){ System.out.println("R"); }} Ans-- In this we get R 99 R. I have problem only with the last R.
Another similar program is given below. 2)public class Superclass { public static void main(String[] args) { System.out.println(new Subclass().methodA()); }
protected int methodB() { System.out.println("methodB in Subclass"); return 1; } } Here Ans is SuperClass Constructor Executed SubClass Constructor Executed methodA in Superclass methodB in Superclass 9 How is MetodB in Superclass called instead of methodB in SubClass?
Thankyou gaurav for the explanation you gave for my other doubt. Expecting your help for this one also. /**1st program*/ 1. class JBase{ 2. public void amethod(){ 3. System.out.println("base"); 4. } 5. JBase(){ 6. amethod(); 7. } 8. } 9. public class Rtype extends JBase{ 10. int i=1; 11. public static void main(String arg[]){ 12. JBase b=new Rtype(); 13. System.out.println(b.i); 14. b.amethod(); 15. } 16. public void amethod() 17. { 18. System.out.println("R"); 19. } 20. } Ans-- R 99 R. I have problem only with the last R.If we call amethod() in JBase() shouldnt the JBase amethod() be executed.I know that if you call b.amethod() the RType amethod is called.
I have another similar doubt in this program /**2nd program*/ public class Superclass { public static void main(String[] args) { System.out.println(new Subclass().methodA()); } Superclass() { System.out.println("SuperClass Constructor Executed"); } private int methodB() { System.out.println("methodB in Superclass"); return 9; } int methodA() { System.out.println("methodA in Superclass"); return methodB(); } } class Subclass extends Superclass { Subclass() { System.out.println("SubClass Constructor Executed"); } protected int methodB() { System.out.println("methodB in Subclass"); return 1; } } //end
Ans is SuperClass Constructor Executed SubClass Constructor Executed methodA in Superclass methodB in Superclass 9
Howcome methodB in SuperClass is called.Can some one explain in conjunction with the first program.
Sorry Mindy but the code in Question 1 will not even compile as class JBase has not int i. So line 13 will not compile. You can format code as by using different tags mentioned at http://www.javaranch.com/ubb/ubbcode.html . Can you quickly repost one question at a time and let me work with you to try to resolve your issues.
Sorry that I missed the value for i.I have no idea how to use the link you gave me gaurav.Can you help me. /**1st program*/ 1. class JBase{ 2. int i=99; 3. public void amethod(){ 4. System.out.println("base"); 5. } 5. JBase(){ 7. amethod();} 8. } 9. public class Rtype extends JBase{ 10. int i=1; 11. public static void main(String arg[]){ 12. JBase b=new Rtype(); 13. System.out.println(b.i); 14. b.amethod(); 15. } 16. public void amethod() 17. { 18. System.out.println("R"); 19. } 20. } Ans-- R 99 R. I have problem only with the last R.If we call amethod() in JBase() shouldnt the JBase amethod() be executed.I know that if you call b.amethod() the RType amethod is called I have another similar doubt in this program
/**2nd program*/ public class Superclass { public static void main(String[] args) { System.out.println(new Subclass().methodA()); } Superclass() { System.out.println("SuperClass Constructor Executed"); } private int methodB() { System.out.println("methodB in Superclass"); return 9; } int methodA() { System.out.println("methodA in Superclass"); return methodB(); } } class Subclass extends Superclass { Subclass() { System.out.println("SubClass Constructor Executed"); } protected int methodB() { System.out.println("methodB in Subclass"); return 1; } } //end
Ans is SuperClass Constructor Executed SubClass Constructor Executed methodA in Superclass methodB in Superclass 9
Howcome methodB in SuperClass is called.Can some one explain in conjunction with the first program.
For the indexing, I just found out actually - use the UBB codes. Whenever you post some code, to save the indenting, put all your code between . Like here:
Ans is SuperClass Constructor Executed SubClass Constructor Executed methodA in Superclass methodB in Superclass 9
Howcome methodB in SuperClass is called.Can some one explain in conjunction with the first program. [/B]
What I attempted to do didn't work. To see how to format your code just click on the edit box above someone's post. You won't be able to edit their post but you will be able to find out how they were able to format their code. Once you figure this out could you fix your code and maybe delete the repeated versions? Thanks. I'm not sure but in response to your second code I believe the problem is that the methodA is calling methodB from within Superclass so it is going to call the Superclass method of methodB. I made both methods static and used the this keyword and still get your result so as far as the method is concerned it must not see subclass. Here's my code:
You've brought up an interesting question. I look forward to the replies. Joe [This message has been edited by Joseph Russell (edited February 15, 2001).]
Hi Mindy, If you take out private keyword for methodB() in Superclass, it's then printing 1 - so, it's got something to do with the access modifier in this case - can someone clarify, please? Thanks, Sri
Originally posted by Joseph Russell: [B]What I attempted to do didn't work. To see how to format your code just click on the edit box above someone's post. You won't be able to edit their post but you will be able to find out how they were able to format their code. Once you figure this out could you fix your code and maybe delete the repeated versions? Thanks. I'm not sure but in response to your second code I believe the problem is that the methodA is calling methodB from within Superclass so it is going to call the Superclass method of methodB. I made both methods static and used the this keyword and still get your result so as far as the method is concerned it must not see subclass. Here's my code:
You've brought up an interesting question. I look forward to the replies. Joe [This message has been edited by Joseph Russell (edited February 15, 2001).][/B]
Thanks Sri your right. I took out my static references and when I put in either protected or friendly in Superclass it calls on methodB in Subclass but when I have Superclass' methodB as private and Subclass' methodB as protected, private, public or friendly it calls on methodB in Superclass. When I leave my static references in there it calls on methodB in Superclass everytime. Can anyone provide insight as to why this happens?
Hi John Russel static methods cannot be overriden.They are only hidden(as are private methods).So it behaves in the same way.So we do not get a compiler error but there is no overriding as such in the case of static methods.
Duchang, Please read the JavaRanch Name Policy and re-register with a name that complies with the rules. Thanks for your cooperation. ------------------ Jane Griscti Sun Certified Programmer for the Java� 2 Platform
Post by:autobot
If you live in a cold climate and on the grid, incandescent light can use less energy than LED. Tiny ad:
a bit of art, as a gift, that will fit in a stocking