Home arrow static arrow Java Programming [Archive] - doubt in inheritance
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - doubt in inheritance
This topic has 6 replies on 1 page.

Posts:13
Registered: 2/6/01
doubt in inheritance  
Jul 24, 2004 6:40 AM



 
Hi guys, i have a doubt in over loading/overriding.... just listen the class here down

i have a class purchased from 3rd party. i have a method called Test(int a ) its implementation would be like this.
<code>
class A {
public void Test(int a ) {
System.out.println("This is integer value");
}
}

</code>

in that class A lot of methods are there other than this, i have a class B where i need to share those functionality so i extend the class A
and also i need to overload the functionality Test(float a ) to accomplish some different task. so i am over loading in sub class. The B class Structure is ..

<code>
class B extends A {
public void Test(float a){
System.out.println("This is Float value");
}
}

</code>

now i am using this class B

<code>
class Test {
public static void main(String[] args){
B objb = new B();
A obja;
objb.Test(6);
}
}
</code>
so when i call the function Test by passing int argument it says "Ambigues" error. why its rising ??? how can call Test with int argument without creating object of A class
 

Posts:18,384
Registered: 21.03.00
Re: doubt in inheritance  
Jul 24, 2004 6:48 AM (reply 1 of 6)



 
Hi,

What you are showing is method overloading. This will execute the test method in a:

        B objb = new B();        ((A)objb).Test(6);


/Kaj

Ps. The code attribute should be inside [] and not inside
 

Posts:13
Registered: 2/6/01
Re: doubt in inheritance  
Jul 25, 2004 4:56 AM (reply 2 of 6)



 
Hi friends,

Thanks for your reply. But i am not able to compile that code, it rises compile time error. saying like "Ambiguies .... " error
 

Posts:24,036
Registered: 2/3/03
Re: doubt in inheritance  
Jul 25, 2004 7:08 AM (reply 3 of 6)



 
Post the exact error message; it would be helpful to know what the compiler thinks is ambiguous. Example:
class Foo {        public static void main(String[] args) throws Exception {        new Foo().go();    }        void go() {        B b = new B();        b.test(6);  // A: integer     }        class A {        public void test(int a) {            System.out.println("A: integer");        }    }        class B extends A {        public void test(float b) {            System.out.println("B: float");        }    }}
Also, please read the [url=http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html]Code Conventions for the Java™ Programming Language[/url] and start your methods with a lowercase character.
 

Posts:18,384
Registered: 21.03.00
Re: doubt in inheritance  
Jul 25, 2004 7:38 AM (reply 4 of 6)



 
Post the exact error message; it would be helpful to
know what the compiler thinks is ambiguous.
Example:
class Foo { public static void main(String[] args) throwsException {new Foo().go();} void go() {B b = new B();b.test(6);  // A: integer } class A {public void test(int a) {System.out.println("A: integer");}} class B extends A {public void test(float b) {System.out.println("B: float");}}}
Also, please read the
[url=http://java.sun.com/docs/codeconv/html/CodeConvTOC
doc.html]Code Conventions for the Java?
Programming Language
[/url] and start your methods
with a lowercase character.

That code does neither compile using borland compiler (JBulder x) nor javac for JDK 1.4. It says:

"A.java": F:\projects\Test\src\A.java:16: reference to test is ambiguous; both method test(int) in Foo.A and method test(float) in Foo.B match at line 16        b.test(6); // A: integer


But it does compile fine if you replace b.test(6) with ((A)b).test(6). (Tried it with javac, and JBuilder compiler)

/Kaj
 

Posts:24,036
Registered: 2/3/03
Re: doubt in inheritance  
Jul 25, 2004 7:49 AM (reply 5 of 6)



 
Funny - it compiles and runs fine for me with no message of ambiguity when using javac from the 1.5 jdk.

I'm surprised that the compiler you're using considers '6' ambiguous in this regard, as integers should be interpreted as int and fractions should be interpreted as doubles, unless otherwise specified. I don't have the 1.4 jdk on this box, but I'll check it later out of my own curiosity...
 

Posts:24,036
Registered: 2/3/03
Re: doubt in inheritance  
Jul 25, 2004 7:57 AM (reply 6 of 6)



 
Compiles and runs fine on Linux w/J2SDK 1.4.2

Hmmm....
 
This topic has 6 replies on 1 page.