Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Final Methods & Overloading

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What isthe problem in following program.
class FinalOverLoad{
final void test(int a){
System.out.println("Final Integer Matrix A = " + a);
}
void test(double a){
System.out.println("Final Double Matrix A = " + a);
}
}
class FinalOverLoadSub extends FinalOverLoad{
void test(double a){
System.out.println("Value of A(INTEGER) in Sub " + a);
}
}

class TestFinalOverLoad{
public static void main(String args[]){
FinalOverLoad x = new FinalOverLoad();
FinalOverLoadSub y = new FinalOverLoadSub();
x.test(1);
x.test(1.0);
y.test(1);
}
}
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While calling test with FinalOverLoadSub object. It gives Ambiguity error since it is getting both test(int) and test(double).
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This seems like a bug to me.
Well Sumita,by making a method final you can prevent sub classes from overriding it but not from inheriting it.
So your sub class inherits the final method and since you're calling y.test(integer),the super class's test(int) must be called.
Note that even by removing the final keyword same problem persists.
ANY SOLUTIONS ANYONE?
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah! Pravin Panicker is corrrect.
That will call the Super Class Method.
Siva.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Dear

In your program you are calling y.test(1) here it check in the FinalOverLoadSub class for the method but it doesent find there its match(though double is accepting an int type void test(double a)).
Now this class is extended from FinalOverLoad so it goes there to find the desired method void test(int a) & it find there.
now the compiler had two method to execute so it is creating an ambiguity.
Now just go through the modified program of yours.
class FinalOverLoad{
final void test(double a){//changes made here.
System.out.println("Final Integer Matrix A = " + a);
}
void test(int a){//changes made here
System.out.println("Final Double Matrix A = " + a);
}
}
class FinalOverLoadSub extends FinalOverLoad{
void test(int a){//changes are made here.
System.out.println("Value of A(INTEGER) in Sub " + a);
}
}

public class TestFinalOverLoad{
public static void main(String args[]){
FinalOverLoad x = new FinalOverLoad();
FinalOverLoadSub y = new FinalOverLoadSub();
x.test(1);
x.test(1.0);
y.test(1);
}
}
it will work fine,
Now again if you put all these method in one class & than try to run the program it will not create a prob for you.
for ex.
public class T {

final void show(int i) {
System.out.println("In integer " + i);
}

public void show(double i) {
System.out.println("In double " + i);
}

public static void main(String args[]) {
T t = new T();
t.show(1);
t.show(1.0);
}
}
this will run fine .
class X{
final void show(int i) {
System.out.println("In integer " + i);
}
}

public class T extends X{



public void show(double i) {
System.out.println("In double " + i);
}

public static void main(String args[]) {
T t = new T();
t.show(1);
t.show(1.0);
}
}
here it will say for an aambiguou method.
by now you must have understand the thing.
lokesh
 
He was expelled for perverse baking experiments. This tiny ad is a model student:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic