• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Polimorphism x Casting

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
I was reading the post that Vidhya Suvarna wrote and a doubt appeared.

B ref2 = (B) ref1;
System.out.println(ref2.g());

I know the casting occurred cause ref1 is of type A.
I know polymorphism applies only to overridden instance methods and the object's type determines at runtime which methods can be called.
But when casting and polymorphism get together, how should I handle?

I thought that ref2.g() would call the B's method cause of the casting on ref1... but It seems the C's method is been called..

Here is the code...


public class Polymorphism2 {
public static void main(String[] args) {
A ref1 = new C();
B ref2 = (B) ref1;
System.out.println(ref2.g());
}
}

class A {
private int f() { return 0; }
public int g() { return 3; }
}

class B extends A {
private int f() { return 1; }
public int g() { return f(); }
}

class C extends B {
public int f() { return 2; }
}

The output is 1.

Code from: Vidhya Suvarna's post
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought that ref2.g() would call the B's method cause of the casting on ref1... but It seems the C's method is been called..

Sorry, but it seems to me that ref2.g() IS calling B's method, which is why the result is = 1. Am I missing something?
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi



Here we are actually overriding the method g() and give a new method f() - since the method f() in class A was private, it won't be available in B and so cannot said to be overrided. They are two separate methods.
In class C, we are defining a new method, f() and give a definition.

When the method g() is called with the object of C, obviously, the method dedfined in B will be called(It is the method available in C through inheritance.). the method will call f() method. according to class B method g(), there is a private method f() only. so it will call the method and returns the result, 1.
if f() was public, the method will be overrided in the class C, and when g() calls it, the method defined in C will be executed.
 
Nya Iwa
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jennifer and Anoobkumar!

I'm sorry but maybe I couldn't express myself better.
But thanks for your attention!
After reading your messages and drawing the heap on paper I could understand how casting works with polymorphism.

Thanks again!
 
reply
    Bookmark Topic Watch Topic
  • New Topic