Win a copy of Functional Design and Architecture this week in the Functional programming 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Casting , why so? Any help?

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any Help::
See the code:
public class MyClass{
public static void main(String argv[]){
MyClass Obj1;
vbClass Obj2;

Obj1=new vbeClass();
Obj2=new vbClass();
//Obj1.abc(); //Commented Line
Obj2.abc();
Obj2=(vbeClass) Obj1;
Obj2.abc();
}
}
class vbClass extends MyClass{
void abc(){System.out.println("vbClass->abc");}
}
class vbeClass extends vbClass{
void abc(){System.out.println("vbeClass->abc");}
}
if you uncomment commented line, it is a compiler error. It means Obj1 don't have abc available.
How following statement is giving output as "vbeClass->abc"?
Obj2=(vbeClass) Obj1;
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the same reason you must cast an object coming from an array to a reference that you desire.
For example:
String myString = new String("hello");
Vector apvector = new Vector();
apvector.add(myString);
/* must explicitly cast to use member function */
//
System.out.println((String)(apvector.get(0)).chartAt(2));
Regards,
Khalid
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Change your commended line to
((vbeClass)Obj1).abc()
And it will work fine
[This message has been edited by jafarali (edited July 18, 2000).]
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic