Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Queries

 
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Could someone explain to me why ref2 prints out 1?
How is the actual explanation for
"PolyA ref1 = new PolyC();" ? Really in doubt.
public class Poly
{
public static void main(String args[]) {
PolyA ref1 = new PolyC();
PolyB ref2 = (PolyB)ref1;
System.out.println(ref2.g());
// This prints 1
// If f() is not private in PolyB,
// then prints 2
}
}
class PolyA {
private int f() { return 0; }
public int g() { return 3; }
}
class PolyB extends PolyA {
private int f() { return 1; }
public int g() { return f(); }
}
class PolyC extends PolyB {
public int f() { return 2; }

Thanks.
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
When you declare method f()
in class PolyB as private,the access of this method to the object of class PolyC is denied and hence the function f() is not over ridden.Howevr ref2 is originally an object of PolyC being downcast to Poly B.So when u declare function f() as public ,because of over ridden function f() the control goes to f() of PolyC
Am i clear .....
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"PolyA ref1 = new PolyC();"
Because PolyC is a subclass of PolyB, and PolyB is a subclass of PolyA, an object of type PolyC can be assigned to a reference PolyA without the need of a cast.
"
PolyA ref1 = new PolyC();
PolyB ref2 = (PolyB)ref1;
System.out.println(ref2.g());
"
First look for the type of the reference on wich the method is invoked (PolyB). Now is there a matching declaration or inherited method? Yes, there is. But the call f() within g() in PolyB is the same as this.f(); the type of this is the class that contains it. Thus the class PolyB itself must be searched for the method f(). Once found, is it private? yes thus it's not overridable by the f() method in PolyC. That is the reason why making it public executes the method f() in PolyC
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so casting to a parent class overrides the accessible methods in the subclass?
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Casting is needed because the declared type of ref1 (PolyA) is a superclass of the declared type of ref2 (PolyB).
If the method invoked is polymophic, the implemention in the derived class would be called.
I wouldn't say a cast overrides methods. A cast changes the compile type of an expression and
(in the case of an assignment)
it ensures the type of an object (runtime) is compatible with the compile type of a variable.
 
Steven Wong
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for clearing my doubts.
I understand better now.
Clement
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

so casting to a parent class overrides the accessible methods in the subclass?


Casting has nothing to do with overriding anything.
Casting is used to make compatible assignments between object references while keeping the actual runtime type of the object instance unchanged.

PolyA ref1 = new PolyC();


Here Clement made an object of type PolyC pointed to by an object reference of type PolyA. Objects by themselves are not publicly visible; they can only be accessed indirectly through object references.
The type of an object and the object reference pointing to that object need not be the same, but there must an inheritance relationship at least, where the type of the object reference is a supertype of the object instance. Otherwise it will not compile. The code above works because PolyA is a supertype of the object instance.

PolyB ref2 = (PolyB)ref1;


Two things are happening here:
(1) Clement is casting the object reference ref1 which is of type PolyA to type PolyB, because the assignment is a narrowing conversion.
(2) The object reference ref2 is now pointing to the instance object (which is still of type PolyC). Now both ref1 and ref2 are pointing to the same object. The code above works too because PolyB is also supertype of the object instance.

System.out.println(ref2.g());


Java uses dynamic method lookup to resolve which method it will call. Basically it goes like this:
The Java genie inside the box says to himself: :roll:
"I have an object reference ref2 invoking an overriden method g().
Inside the object reference ref2 is the object instance.
What is the type of this object instance? It is PolyC.
What is the highest superclass of PolyC with a non-private g() defined? It is PolyA.
Is g() in PolyA overriden by a subclass? Yes, it is overriden by PolyB.
I now examine g() of PolyB.
Is g() in PolyB overriden by a subclass? No, therefore this is the method I must invoke."
Invoking g() of PolyB the Java Genie notices another method invocation: this.f(). He repeats the above procedure, determining that the this object reference holds an object instance of type PolyC. Listening in...
"...What is the type of this object instance? It is PolyC.
What is the highest superclass of PolyC with a non-private f() defined? It is PolyB..
Is f() in PolyB overriden by a subclass? No (since it is private), therefore this is the method I must invoke."
And so he invokes f() of PolyB, which returns 1.
-anthony
 
Joshua Kueck
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh.. I see now, I think .. thanks Anthony.. and thanks Java genie ...
 
Weeds: because mother nature refuses to be your personal bitch. But this tiny ad is willing:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic