About Question enthuware.ocajp.i.v7.2.1250 :
Moderators: Site Manager, fjwalraven
-
- Posts: 20
- Joined: Sun Feb 16, 2014 3:28 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1250 :
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);
}
}
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);
}
}
-
- Site Admin
- Posts: 10074
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1250 :
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.
-
- Posts: 3
- Joined: Sat Oct 25, 2014 2:17 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1250 :
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
}
}
Why is b1 = (B1)b; correct over b2 = (B2)b1? can you please explain?
Thanks,
Tony Singarayar
-
- Site Admin
- Posts: 10074
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1250 :
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.
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.
-
- Posts: 1
- Joined: Fri Jan 16, 2015 6:56 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1250 :
Hi, can you post a link to where this casting is explained?
Thanks
Thanks
-
- Site Admin
- Posts: 10074
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1250 :
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.
-
- Posts: 3
- Joined: Fri Dec 11, 2015 6:06 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1250 :
why are there 2 casts required for the last line?
-
- Site Admin
- Posts: 10074
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1250 :
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.
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.
-
- Posts: 2
- Joined: Fri Nov 27, 2015 6:52 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1250 :
first answer is right, but wrong answer is highlighted green
- Attachments
-
- ent.JPG (76.73 KiB) Viewed 6514 times
-
- Site Admin
- Posts: 10074
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1250 :
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.
Who is online
Users browsing this forum: No registered users and 6 guests