About Question enthuware.ocajp.i.v7.2.1250 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
arjun gaur
Posts: 20
Joined: Sun Feb 16, 2014 3:28 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1250 :

Post by arjun gaur »

Answer is wrong..look at this code.there should be an Is-a relationship,and since the access is "default" so both classes should be in same package.
class A
{
int a;
}
class B extends A
{
public static void main(String[] args)
{
B b=new B();
A a=new A();

System.out.println(b.a);
}
}

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

Re: About Question enthuware.ocajp.i.v7.2.1250 :

Post by admin »

I have no idea what you mean. Can you explain why you think the given answer is wrong?
If you like our products and services, please help us by posting your review here.

Tony.Singarayar
Posts: 3
Joined: Sat Oct 25, 2014 2:17 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1250 :

Post by Tony.Singarayar »

Code: Select all

class B {}
class B1 extends B {}
class B2 extends B {}
public class ExtendsTest{
  public static void main(String args[]){
     B b = new B();
     B1 b1 = new B1();
     B2 b2 = new B2();
     // insert statement here
  }
}
i think b1 = (B1) b; has the logic as b2 = (B2) b1


Why is b1 = (B1)b; correct over b2 = (B2)b1? can you please explain?

Thanks,
Tony Singarayar

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

Re: About Question enthuware.ocajp.i.v7.2.1250 :

Post by admin »

No, it doesn't have the same logic. It is possible for b to point to an object of clasd b1 but it is not possible for b1 to point to an object of class b2 at all.

Think of it this way - an animal reference can point to a Dog or a Cat, but a dog reference can never point to a Cat because a Dog is an Animal but a Cat can never be a Dog.
If you like our products and services, please help us by posting your review here.

padraig
Posts: 1
Joined: Fri Jan 16, 2015 6:56 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1250 :

Post by padraig »

Hi, can you post a link to where this casting is explained?
Thanks

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

Re: About Question enthuware.ocajp.i.v7.2.1250 :

Post by admin »

This is quite fundamental. Any good Java book will do. Or just google.
If you like our products and services, please help us by posting your review here.

liviu_varlam
Posts: 3
Joined: Fri Dec 11, 2015 6:06 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1250 :

Post by liviu_varlam »

why are there 2 casts required for the last line?

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

Re: About Question enthuware.ocajp.i.v7.2.1250 :

Post by admin »

Two casts are not needed. Actually, no cast is needed here. The explanation is only trying to illustrate that after casting b1 to B, you have to cast it again to B1 to make it work.
The explanation has now been updated to make it clear.
thank you for your feedback!
Paul.
If you like our products and services, please help us by posting your review here.

dmcler
Posts: 2
Joined: Fri Nov 27, 2015 6:52 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1250 :

Post by dmcler »

first answer is right, but wrong answer is highlighted green
Attachments
ent.JPG
ent.JPG (76.73 KiB) Viewed 6437 times

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

Re: About Question enthuware.ocajp.i.v7.2.1250 :

Post by admin »

The question is asking which lines will fail. That is why option 3 is highlighted in green. It is the right option.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 21 guests