The compiler reads the next line of code and know's line 2 will not compileDuplicate method sayHello() in type DataTypes
Answer Query- About Question enthuware.ocajp.i.v7.2.1074 :
Moderator: admin
Answer Query- About Question enthuware.ocajp.i.v7.2.1074 :
when I run this question the compilation error is on line 2.
-
- Site Admin
- Posts: 10240
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074 :
Hi,
I just tried to compile it and it shows error on //3
Can you please check the version of javac that you are using? ( javac -version )?
Paul.
I just tried to compile it and it shows error on //3
It makes sense because the method at //2 the method has not "already" been defined.C:\works>javac TestClass.java
TestClass.java:4: sayHello() is already defined in TestClass
public void sayHello() { System.out.println("Hello World "); } //3
^
1 error
Can you please check the version of javac that you are using? ( javac -version )?
Paul.
If you like our products and services, please help us by posting your review here.
Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074 :
Hi,
Thanks in advance.
The_Nick
Code: Select all
public class TestClass{
static int a = 10;
int a = 10; // it gives compilation error
public static void main(){ new TestClass().sayHello(); } //1
public static void sayHello(){ System.out.println("Static Hello World"); } //2
}
class JuniorTestClass{
int a = 10; it does not give compilation error even though the super "static int a = 10" is still visible
public void sayHello() { System.out.println("Hello World "); } // not allowed. so why there is this difference between instance variable and instance method?
}
The_Nick
-
- Site Admin
- Posts: 10240
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074 :
This is how they designed it. The language designers decided that it is not a good idea to have an instance method overridden/shadowed by static method and vice versa. One can certainly design their own language and permit it.
If you like our products and services, please help us by posting your review here.
Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074 :
One more thing about the topic,
class A{
static int a = 10;
//int a = 9; // if this line is uncommented it will give back a compilation error
}
class B extends A{
int a = 9; // with this line it will work even though actually the super static variable is still visible.
}
Why does it happen like that?
Thanks in advance.
The_Nick
class A{
static int a = 10;
//int a = 9; // if this line is uncommented it will give back a compilation error
}
class B extends A{
int a = 9; // with this line it will work even though actually the super static variable is still visible.
}
Why does it happen like that?
Thanks in advance.
The_Nick
-
- Site Admin
- Posts: 10240
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074 :
same reason
If you like our products and services, please help us by posting your review here.
-
- Posts: 21
- Joined: Tue Sep 09, 2014 3:28 pm
- Contact:
Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074
this question is somewhat ambiguous...the actual compilation error is visible on line 2, at least in Eclipse Kepler but i suspect in many more IDE's...but of course line 3 is the point at which the compiler thinks " hey, someone is pulling me a leg" ( i have asked her) so IMHO the right answer is line 2 since at this point there is a compilation error, but i would understand that the compiler needs line 3 to make this conclusion so this would be the argument for answering line 3...anyhow, its rich in ambiguity
-
- Site Admin
- Posts: 10240
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074
I respectfully disagree that there is any ambiguity with this question. First of all, please stop using IDE for this exam. What IDE shows is totally irrelevant for this exam.
Second, the question asks which line of code causes the compilation error and the line marked //3 is indeed the line that causes the compiler to balk. This is also proven by the output of javac:
Paul.
Second, the question asks which line of code causes the compilation error and the line marked //3 is indeed the line that causes the compiler to balk. This is also proven by the output of javac:
HTH,Code: Select all
C:\temp>javac TestClass.java TestClass.java:4: error: method sayHello() is already defined in class TestClass public void sayHello() { System.out.println("Hello World "); } ^ 1 error
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 21
- Joined: Tue Sep 09, 2014 3:28 pm
- Contact:
Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074
I now agree that using IDE's distorts things.
Maybe its an idea to mention javac compiler at the beginning or somewhere in every relevant question since i thinks its not clear to everyone
Nice to see differences between different compilers this way though..and again, thank you guys for doing such a good job running this software and forum, I hate to say it but its really worth the money
Maybe its an idea to mention javac compiler at the beginning or somewhere in every relevant question since i thinks its not clear to everyone
Nice to see differences between different compilers this way though..and again, thank you guys for doing such a good job running this software and forum, I hate to say it but its really worth the money
-
- Posts: 1
- Joined: Thu Mar 12, 2015 3:01 am
- Contact:
Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074
Any comments about the missing String[] args in the main method arguments?
Without that it will not run at all.
Without that it will not run at all.
-
- Site Admin
- Posts: 10240
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074
String[] args is there in the main method. I see it.
-Paul.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Fri Nov 18, 2016 10:49 am
- Contact:
Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074
I came here after hitting the "Discuss" button in the GUI.
Now, in java 8. This program will run without any runtime errors. And it will print "Hello World". Is it possible to adjust the testset to this new information?
Now, in java 8. This program will run without any runtime errors. And it will print "Hello World". Is it possible to adjust the testset to this new information?
-
- Site Admin
- Posts: 10240
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074
It doesn't compile on Java 8 either, let alone run. Are you sure you tried the exact same code given in the question?
-Paul.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 28
- Joined: Mon Sep 25, 2017 8:16 am
- Contact:
Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074
Code: Select all
public class SuperClass {
static void goClass() {}
}
class Subclass extends SuperClass implements doIt {
// void goClass() {} --> not compiling
static void goClass() {} // ok, does compile
public void goInterface() {} // ok, does compile
}
interface doIt {
static void goInterface() {}
}
< SuperClass/Subclass: You can't override/hide a static method with an instance method.
< Superinterface/Class: Yes, we can. Static methods are not inherited from interface to class, therefore aren't visible in the class.
-
- Posts: 85
- Joined: Mon Dec 24, 2018 6:24 pm
- Contact:
Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074 :
Hi, I remember you said it is ok to have two method with same signature but it will cause ambiguity once you start to call it. Am I right? Is there any rule regarding this case?You cannot have two methods with the same signature (name and parameter types) in one class
-
- Site Admin
- Posts: 10240
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074 :
I don't think I said that you can have two methods with the same signature in one class. Can you show me where?crazymind wrote: ↑Mon Jan 07, 2019 4:21 pmHi, I remember you said it is ok to have two method with same signature but it will cause ambiguity once you start to call it. Am I right? Is there any rule regarding this case?You cannot have two methods with the same signature (name and parameter types) in one class
Yes, there are several rules about this. You will need to go through a book to learn this topic. It is not possible to cover all possibilities here in a post.
If you like our products and services, please help us by posting your review here.
-
- Posts: 85
- Joined: Mon Dec 24, 2018 6:24 pm
- Contact:
Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074 :
Thanks, do you know which book cover this? I gonna read it.
-
- Site Admin
- Posts: 10240
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074 :
You will have to see the rules for inheritance, for default methods in interfaces, for overriding/hiding of static/instance methods.
You can go through any certification book or Java book for this. In Hanumant Deshmukh's OCAJP Fundamentals book, you can go through chapter 8, 9, and 11 thoroughly to understand all the rules.
You can go through any certification book or Java book for this. In Hanumant Deshmukh's OCAJP Fundamentals book, you can go through chapter 8, 9, and 11 thoroughly to understand all the rules.
If you like our products and services, please help us by posting your review here.
-
- Posts: 85
- Joined: Mon Dec 24, 2018 6:24 pm
- Contact:
Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074 :
admin wrote: ↑Mon Jan 07, 2019 9:56 pmI don't think I said that you can have two methods with the same signature in one class. Can you show me where?crazymind wrote: ↑Mon Jan 07, 2019 4:21 pmHi, I remember you said it is ok to have two method with same signature but it will cause ambiguity once you start to call it. Am I right? Is there any rule regarding this case?You cannot have two methods with the same signature (name and parameter types) in one class
Yes, there are several rules about this. You will need to go through a book to learn this topic. It is not possible to cover all possibilities here in a post.
Code: Select all
Standard Test 5, Question 21 What, if anything, is wrong with the following code? //Filename: TestClass.java class TestClass implements T1, T2{ public void m1(){} } interface T1{ int VALUE = 1; void m1(); } interface T2{ int VALUE = 2; void m1(); }
Are duplicate method allowed in this case? Why?Having ambiguous fields or methods does not cause any problems by itself but referring to such fields/methods in an ambiguous way will cause a compile time error.
-
- Site Admin
- Posts: 10240
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074 :
There are no duplicate methods or fields in the same class in the above code. The ambiguity referred to in the above code is due to inheritance. This is different from defining two methods with the same signature in the same class and it is allowed. As to why this is allowed? I don't know. Only Java designers can answer this question.
If you like our products and services, please help us by posting your review here.
-
- Posts: 85
- Joined: Mon Dec 24, 2018 6:24 pm
- Contact:
Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074 :
admin wrote: ↑Tue Jan 08, 2019 9:40 pmThere are no duplicate methods or fields in the same class in the above code. The ambiguity referred to in the above code is due to inheritance. This is different from defining two methods with the same signature in the same class and it is allowed. As to why this is allowed? I don't know. Only Java designers can answer this question.
Code: Select all
Standard Test 6, question 7 Consider the following code: interface Bar{ void bar(); } abstract class FooBase{ public static void bar(){ System.out.println("In static bar"); } } public class Foo extends FooBase implements Bar { } What can be done to the above code so that it will compile without any error?
This cause a compile error since the method conflict. Do you know where I can find rules for duplicate method? It is really confusing.
-
- Site Admin
- Posts: 10240
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074 :
Sorry, i dont know where you can find all the rules in one place but as I said before, you can go through any book.
The error above is because of static and instance and not because of signature.
The error above is because of static and instance and not because of signature.
If you like our products and services, please help us by posting your review here.
-
- Posts: 85
- Joined: Mon Dec 24, 2018 6:24 pm
- Contact:
Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074 :
You can have a class inherit a method with the same signature from an interface and a superclass though. This is allowed because the superclass's version always overrides the interface's version. The class doesn't get two implementations. It gets only the version from super class.
Find this rule in solution of Last day test, Question 57.
Who is online
Users browsing this forum: No registered users and 9 guests