public class A {
public int x = 42;
public class B extends A {
int x = 17
Unlike methods, fields in a subclass hide those of the same name in the superclass.
Therefore, the fields in the subclass don't need to have equal-or-less restrictive access modifier than the hidden fields.
-------
protected A() {}
public B() {super();}
because of "protected" super constructor is visible from class B.
-------
In Java fields and static methods aren't polymorphic.
A obj = new B();
System.out.println(obj.x);
That means obj.x is the field of the reference type (A obj =.. ).
A is correct because, constructor has a protected modified, so child class can see that constructor, and instance variables not override. if father class has a default access this would be a compilation error cause chid class cannot see the super constructor of father class
the static method and variables are acceses by the Type of object not the real object.
so x in A is acceded.
if B obj = new B(); => then x in b is printed
A is correct you cannot override instance variables
upvoted 2 times
...
Log in to ExamTopics
Sign in:
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
ASPushkin
5 months, 4 weeks agod7bb0b2
1 year, 1 month agod7bb0b2
1 year agoOmnisumem
1 year, 5 months agotmuralimanohar
1 year, 7 months agoStavok
1 year, 8 months agoRP384
1 year, 11 months agoTADIEWA
2 years, 1 month ago