Answer Query- About Question enthuware.ocajp.i.v7.2.1074 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
John Fanta

Answer Query- About Question enthuware.ocajp.i.v7.2.1074 :

Post by John Fanta »

when I run this question the compilation error is on line 2.
Duplicate method sayHello() in type DataTypes
The compiler reads the next line of code and know's line 2 will not compile

admin
Site Admin
Posts: 10240
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074 :

Post by admin »

Hi,
I just tried to compile it and it shows error on //3
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
It makes sense because the method at //2 the method has not "already" been defined.

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.

The_Nick

Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074 :

Post by The_Nick »

Hi,

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?
}
Thanks in advance.

The_Nick

admin
Site Admin
Posts: 10240
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074 :

Post by admin »

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.

The_Nick

Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074 :

Post by The_Nick »

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

admin
Site Admin
Posts: 10240
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074 :

Post by admin »

same reason :)
If you like our products and services, please help us by posting your review here.

jbilkes
Posts: 21
Joined: Tue Sep 09, 2014 3:28 pm
Contact:

Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074

Post by jbilkes »

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 :)

admin
Site Admin
Posts: 10240
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074

Post by admin »

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:

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
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

jbilkes
Posts: 21
Joined: Tue Sep 09, 2014 3:28 pm
Contact:

Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074

Post by jbilkes »

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 :)

mpj1608
Posts: 1
Joined: Thu Mar 12, 2015 3:01 am
Contact:

Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074

Post by mpj1608 »

Any comments about the missing String[] args in the main method arguments?
Without that it will not run at all.

admin
Site Admin
Posts: 10240
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074

Post by admin »

String[] args is there in the main method. I see it.
-Paul.
If you like our products and services, please help us by posting your review here.

maarten03
Posts: 5
Joined: Fri Nov 18, 2016 10:49 am
Contact:

Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074

Post by maarten03 »

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?

admin
Site Admin
Posts: 10240
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074

Post by admin »

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.
If you like our products and services, please help us by posting your review here.

JuergGogo
Posts: 28
Joined: Mon Sep 25, 2017 8:16 am
Contact:

Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074

Post by JuergGogo »

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() {}
}
Just for clarification:
< 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.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074 :

Post by crazymind »

You cannot have two methods with the same signature (name and parameter types) in one class
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?

admin
Site Admin
Posts: 10240
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074 :

Post by admin »

crazymind wrote:
Mon Jan 07, 2019 4:21 pm
You cannot have two methods with the same signature (name and parameter types) in one class
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?
I don't think I said that you can have two methods with the same signature in one class. Can you show me where?

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.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074 :

Post by crazymind »

Thanks, do you know which book cover this? I gonna read it.

admin
Site Admin
Posts: 10240
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074 :

Post by admin »

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.
If you like our products and services, please help us by posting your review here.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074 :

Post by crazymind »

admin wrote:
Mon Jan 07, 2019 9:56 pm
crazymind wrote:
Mon Jan 07, 2019 4:21 pm
You cannot have two methods with the same signature (name and parameter types) in one class
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?
I don't think I said that you can have two methods with the same signature in one class. Can you show me where?

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(); } 
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.
Are duplicate method allowed in this case? Why?

admin
Site Admin
Posts: 10240
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074 :

Post by admin »

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.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074 :

Post by crazymind »

admin wrote:
Tue Jan 08, 2019 9:40 pm
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.

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.

admin
Site Admin
Posts: 10240
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074 :

Post by admin »

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.
If you like our products and services, please help us by posting your review here.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

Re: Answer Query- About Question enthuware.ocajp.i.v7.2.1074 :

Post by crazymind »

admin wrote:
Wed Jan 09, 2019 7:44 pm
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.
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.

Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests