enthuware.ocajp.i.v7.2.1222

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

Moderator: admin

Post Reply
ashishrai.kv
Posts: 33
Joined: Tue Jan 09, 2018 2:12 am
Contact:

enthuware.ocajp.i.v7.2.1222

Post by ashishrai.kv »

Code: Select all

class A{
   public void m1() {   }
}
class B extends A{
   public void m1() {   }
}
class C extends B{
   public void m1(){
      /*  //1
      ... lot of code.
      */
   }
}


ans-You can access class B's m1() using super.m1() from class C.

according to above answer , doesn't using super.m1() allows the constructor to use the class A's method from C ?

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

Re: enthuware.ocajp.i.v7.2.1222

Post by admin »

Not sure which constructor are you talking about. I don't see any constructor here. Syntax of super dot only allows you to go one level up. You can put a print statement in the methods and try it out.
If you like our products and services, please help us by posting your review here.

ashishrai.kv
Posts: 33
Joined: Tue Jan 09, 2018 2:12 am
Contact:

Re: enthuware.ocajp.i.v7.2.1222

Post by ashishrai.kv »

Got my answer as you said super constructor will allow only one level up from the class its been called. is it?

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

Re: enthuware.ocajp.i.v7.2.1222

Post by admin »

Again, I have no idea what you are trying to say by, "super constructor will allow only one level up from the class its been called". What exactly are you trying to do?
If you like our products and services, please help us by posting your review here.

ashishrai.kv
Posts: 33
Joined: Tue Jan 09, 2018 2:12 am
Contact:

Re: enthuware.ocajp.i.v7.2.1222

Post by ashishrai.kv »

Code: Select all


class A{
   public void m1() {   }
}
class B extends A{
   public void m1() {   }
}
class C extends B{
   public void m1(){
      /*  //1
      ... lot of code.
      */
   }
}

ans-You can access class B's m1() using super.m1() from class C. 

according to above answer , doesn't using super.m1() allows the constructor to use the class A's method from C ?
here what is super.m1().

let for example there are three classes with same method class A,Band C

i am asking If we call a method using Super().method from class C which method super will invoke, class A's or B's ?

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

Re: enthuware.ocajp.i.v7.2.1222

Post by admin »

The explanation is talking about the method m1. It is not talking about a constructor. super.m1() is a way to invoke the immediate superclass's version of m1 instead of the current class's. Thus, as the explanation says, if you call super.m1 from class C, it will invoke B's m1 (and not A's). If you invokes super.m1() from class B, then it will invoke A's m1.

You may want to go through this tutorial for more information: https://docs.oracle.com/javase/tutorial ... super.html
If you like our products and services, please help us by posting your review here.

ashishrai.kv
Posts: 33
Joined: Tue Jan 09, 2018 2:12 am
Contact:

Re: enthuware.ocajp.i.v7.2.1222

Post by ashishrai.kv »

Thank you , got the answer :)

elias86
Posts: 7
Joined: Fri May 04, 2018 4:14 am
Contact:

Re: enthuware.ocajp.i.v7.2.1222

Post by elias86 »

Why the program tells me: "There is no construct like super.super. So, there is no way you can access m1() of A from C."? What does it means? So if a sub-class haven't got a constructor that invocates a super constructor i cant' use its methods?

Can someone help me to chiarify this question please?

Thanks, Elias.

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

Re: enthuware.ocajp.i.v7.2.1222

Post by admin »

It means that you can't access A's member from C using some thing like super.super.methodInA() even though you can access B's member from C using super.methodInB();.

So while super.something is valid, super.super.something is not valid.
If you like our products and services, please help us by posting your review here.

raj_dp
Posts: 16
Joined: Sun Jun 12, 2016 7:43 am
Contact:

Re: enthuware.ocajp.i.v7.2.1222

Post by raj_dp »

I want to understand two things pertaining to this question.
1) The first correct answer says - "You cannot access class A's m1() from class C for the same object ( i.e. this)."
Does this mean the following:
class C extends B{
public void m1(){
this.m1();
}

But this will mean calling C's m1() itself. The meaning of the statement is quite confusing.
2) The second correct answer says - "You can access class B's m1() using super.m1() from class C."
Does it mean the following:
class C extends B{
public void m1(){
super.m1();
//This is supposed to call class B's m1() method.
}
Regards
Raj

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

Re: enthuware.ocajp.i.v7.2.1222

Post by admin »

raj_dp wrote:
Tue Dec 11, 2018 7:43 am
I want to understand two things pertaining to this question.
1) The first correct answer says - "You cannot access class A's m1() from class C for the same object ( i.e. this)."
Does this mean the following:
class C extends B{
public void m1(){
this.m1();
}

But this will mean calling C's m1() itself. The meaning of the statement is quite confusing.
The statement is talking about accessing A's implementation of m1 in an object of class C. If you try to call this.m1, as you have said, it will be calling its own m1, which proves it is not possible to invoke A's m1.
2) The second correct answer says - "You can access class B's m1() using super.m1() from class C."
Does it mean the following:
class C extends B{
public void m1(){
super.m1();
//This is supposed to call class B's m1() method.
}
Regards
Raj
Yes, when you do super.m1(), you are able to invoke B's m1.
If you like our products and services, please help us by posting your review here.

raj_dp
Posts: 16
Joined: Sun Jun 12, 2016 7:43 am
Contact:

Re: enthuware.ocajp.i.v7.2.1222

Post by raj_dp »

Thanks for your clarification.
Regards
Raj

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

Re: enthuware.ocajp.i.v7.2.1222

Post by flex567 »

So no matter what you do, in class C you can only access C's m1() even by casting this to B or A. So, this option will not work.

Code: Select all

class A{
   public void m1() { System.out.println("xyz");  }
}
class B extends A{
   public void m1() {   }
}
class C extends B{
   public void m1(){
      new A().m1();
   }
}

class TestClass{
	public static void main(String[] args){
		new C().m1();
	}
}


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

Re: enthuware.ocajp.i.v7.2.1222

Post by admin »

That statement is about that particular option. The option says, "You can access class A's m1() using ( (A) this ).m1() from class C." and the first sentence of the explanation, which you have ignored, says, "Note that selection of method to be executed depends upon the actual object class.".

You are taking the statement out of context. Your code shows that you are accessing some other object's m1, not of "this". The actual class of object in your code is not C but A.
If you like our products and services, please help us by posting your review here.

swarna pakeer
Posts: 16
Joined: Thu Mar 19, 2020 2:27 pm
Contact:

Re: enthuware.ocajp.i.v7.2.1222

Post by swarna pakeer »

option B,C,D says "from class c" but they did not mention object is created for class C only . So if we create object for class A in class C then A's m1() is called and your explanation is little confusing as it says "no matter what you do u cannot call A's m() from Class C". And option A is clearly talking about C's object as it says "this" but remaining options are not clear on that. So here do we need to assume that "from class C" means object is created for class C ?

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

Re: enthuware.ocajp.i.v7.2.1222

Post by admin »

There is no need to assume anything in options b, c, and because complete line of code is given. B says, "using super.m1() ", C says, " using ( (A) this ).m1()", and D says, " using super.super.m1()", so, I am not sure where you think there is a confusion of using a reference to another object. If the code is using "super" and exists in class C, then it is quite clear it is talking about the current object which is an object of class C.
If you like our products and services, please help us by posting your review here.

swarna pakeer
Posts: 16
Joined: Thu Mar 19, 2020 2:27 pm
Contact:

Re: enthuware.ocajp.i.v7.2.1222

Post by swarna pakeer »

i am clear with options but what i actually meant was the sentence in the explaination "super.super is an invalid construct. So, there is no way you can access m1() of A from C." if we create a object of class A in class C we can access A's m1() . so can u please remove that second part of that explanation which says no matter what u do u cannot access A's m1() from C.

And general question- So incase if only code under main method is given ,do we need to assume that it is written in a class or select option as "compile error" if there is such option given?

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

Re: enthuware.ocajp.i.v7.2.1222

Post by admin »

swarna pakeer wrote:
Tue Jun 02, 2020 1:20 am
i am clear with options but what i actually meant was the sentence in the explaination "super.super is an invalid construct. So, there is no way you can access m1() of A from C." if we create a object of class A in class C we can access A's m1() . so can u please remove that second part of that explanation which says no matter what u do u cannot access A's m1() from C.
Sure, your feedback is taken into consideration and it will be updated if deemed necessary.
And general question- So incase if only code under main method is given ,do we need to assume that it is written in a class or select option as "compile error" if there is such option given?
Yes, you have to assume that the main method exists in a class. It can't run on its own. Also, you should assume that anything that is not given or shown in the question, is valid and will not affect the answer.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 42 guests