About Question enthuware.ocajp.i.v7.2.1220 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.1220 :
I tried executing this program so just wanted to make sure whether class A and B are in the class file C. If all of them are in the same class file then i think option1 will work.
Please advise!
Please advise!
Re: About Question enthuware.ocajp.i.v7.2.1220 :
I am sorry the answer was correct... i wrote program wrongly.
-
- Posts: 37
- Joined: Thu Feb 13, 2014 12:58 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1220 :
Is it Not possible if the variable of class A is private so b.i refers to variable of class B?
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1220 :
No, explanation to option 1 explains it.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Wed May 28, 2014 7:20 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1220 :
The answer that question is wrong. I'm sure that option 3 - System.out.println(b.i); will print 20 - is correct. I've tested that situation and I saw that "b.i" works perfectly
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1220 :
Please try the code that is given in the question exactly as given.trovanfloyd wrote:The answer that question is wrong. I'm sure that option 3 - System.out.println(b.i); will print 20 - is correct. I've tested that situation and I saw that "b.i" works perfectly
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 32
- Joined: Sat May 02, 2015 11:57 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1220 :
Compiler says cannot make a static reference to a non-static field.
class A{
private int i = 10;
public void f(){}
public void g(){}
}
class B extends A{
public int i = 20;
public void g(){}
}
public class C{
A a = new A();//1
A b = new B();//2
public static void main(String[] args) {
System.out.println(b.i);
}
}
Also, how will we be able to access "i" in class B?
class A{
private int i = 10;
public void f(){}
public void g(){}
}
class B extends A{
public int i = 20;
public void g(){}
}
public class C{
A a = new A();//1
A b = new B();//2
public static void main(String[] args) {
System.out.println(b.i);
}
}
Also, how will we be able to access "i" in class B?
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1220 :
The code that you are trying to compiler is probably different from what you've pasted here.
If you like our products and services, please help us by posting your review here.
-
- Posts: 32
- Joined: Sat May 02, 2015 11:57 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1220 :
Sir,
I have pasted the program as is,
My question is that since "i" is public in class B, how can we access it?
I have pasted the program as is,
My question is that since "i" is public in class B, how can we access it?
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1220 :
Variables and static methods are not overridden and so access to variables and static methods is determined at compile time based on the type of the variable (instead of type of the object referred to by the variable, as is the case with instance methods.)subhamsdalmia wrote:Sir,
I have pasted the program as is,
My question is that since "i" is public in class B, how can we access it?
So if you declare b to be of type B, you can access b.i.
If you like our products and services, please help us by posting your review here.
-
- Posts: 66
- Joined: Mon Feb 20, 2017 12:31 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1220 :
Hi!
I would like to know why is compiling and running without problem the statement:
b.f();// A has got f(), but the object is of type B.
If there is not method f() in the B class...
Why is running without problem??
Thank you!!
I would like to know why is compiling and running without problem the statement:
b.f();// A has got f(), but the object is of type B.
If there is not method f() in the B class...
Why is running without problem??
Thank you!!
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1220 :
If B is a subclass of A and if A has non private f() then B will inherit f(). Thus, there is no issue in calling b.f().
If you like our products and services, please help us by posting your review here.
-
- Posts: 4
- Joined: Sun Oct 29, 2017 7:30 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1220 :
In the given code,
public class C{
A a = new A();//1
A b = new B();//2
}
if we were to do System.out.println(a.i);
Would this now print 10, or would it be a compile error?
Thanks!
public class C{
A a = new A();//1
A b = new B();//2
}
if we were to do System.out.println(a.i);
Would this now print 10, or would it be a compile error?
Thanks!
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1220 :
What happened when you tried it out?
If you like our products and services, please help us by posting your review here.
-
- Posts: 8
- Joined: Tue Jan 26, 2021 7:55 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1220 :
Hi,
public void f(){}
why b.f( ) is compile error?
For me, it works without errors ... :/
public void f(){}
why b.f( ) is compile error?
For me, it works without errors ... :/
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1220 :
It is not a compile error that is why this option is not correct. Option 5, "None of the above statements is correct", is the correct option.burlacu.valeri wrote: ↑Thu Feb 25, 2021 2:24 amHi,
public void f(){}
why b.f( ) is compile error?
For me, it works without errors ... :/
If you like our products and services, please help us by posting your review here.
-
- Posts: 8
- Joined: Tue Jan 26, 2021 7:55 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1220 :
Hi,
ok, now I have understood.
ok, now I have understood.
Who is online
Users browsing this forum: No registered users and 101 guests