Public class test{ public void hello() { System.out.println("Helllo"); }}public class test1 extends test{ public void say(){ super.hello(); hello();}public void main(String arg[]){ test1 t = new test1(); t.say();}}In the above code what will be difference of super.hello(); and hello();. What is use of super instead of calling directly method name.

Showing Answers 1 - 12 of 12 Answers

agsuvidha

  • Aug 14th, 2007
 

There are some errors in this program first. this program will not compile...
1) There are 2 public classes, there should be only 1 public class which has main function
2) Main function is not a static function, so while executing it will not be able to call it

Now your query after removing the above problems
Super is used to call the base class methods or constructors.
In this example, as you have not overridden the hello method, so it will give you the same result with the both statements. Otherwise super.hello would have called the hello method of the base class and hello would have called the method of the current class.

  Was this answer useful?  Yes

The above code when compiled would give errors due to two public classes being saved in the same file...
that is test1 and test should be two different files  or remove public keyword from test  class...
next the main method is identifed as static by java compiler which it does not find here...

So it raises an error for that too.

super.hello() and hello() mean the same thing here as the hello function is not overrided by tset1 class...

  Was this answer useful?  Yes

sreekanth

  • Aug 23rd, 2007
 

there is no difference in the above program. because hello() is available in test class only and test1 is extending test. if there is hello method in test1 class then it will make much difference

  Was this answer useful?  Yes

vanu sharma

  • Sep 21st, 2007
 

If the superclass and the subclass are having the method with same name then to call the method of superclass, super keyword is used but since the above code is not having any hello method in the subclass so it will always call the method of superclass. 

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions