GeekInterview.com
Series: Subject: Topic:
Question: 266 of 2110

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.

Asked by: Interview Candidate | Asked on: Aug 14th, 2007
Showing Answers 1 - 4 of 4 Answers
agsuvidha

Answered On : Aug 14th, 2007

View all answers by agsuvidha

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 problemsSuper 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.

  
Login to rate this answer.

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...

  
Login to rate this answer.
sreekanth

Answered On : 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

  
Login to rate this answer.
vanu sharma

Answered On : 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. 

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.