aspose file tools*
The moose likes Beginning Java and the fly likes Is it Polymorphism Concept wrong or right? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is it Polymorphism Concept wrong or right?" Watch "Is it Polymorphism Concept wrong or right?" New topic
Author

Is it Polymorphism Concept wrong or right?

sachin santaji
Greenhorn

Joined: Mar 20, 2012
Posts: 8
In the below program,I have some doubts on polymorphism concept i.e; while call by Reference

Here I am creating one super class as A and subclass as B.I am using one more class as "HelloAll" for accessing these two class A and B.
First i am creating the Class 'A' object and than creating one reference type as 'B'. And then assigning the object of A class to reference type of subclass as B.
Here depend on the polymorphism concept it must be access the object of 'A' class by using the 'B' class reference type.But it is giving the Run time error.


[Added code tags - see UseCodeTags for details]

Is it possible to solve this problem or not?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8012
    
  22

sachin santaji wrote:In the below program,I have some doubts on polymorphism concept i.e; while call by Reference

Totally unrelated.

Here depend on the polymorphism concept it must be access the object of 'A' class by using the 'B' class reference type.But it is giving the Run time error.

I think you need to read the tutorials again. Polymorphism is solely concerned with how an object behaves; not what (nor indeed, what type) it is - although the 2nd may have some bearing on the 1st.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

You can only cast a reference to another type if the object it references is of that other type.

Here, a references an A object. Not a B. So trying to cast the reference to a B isn't going to work.

As for how you solve it - well, since the program doesn't do anything, you can solve it just by deleting the line that's giving the error!

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Winston Gutkowski wrote:
sachin santaji wrote:In the below program,I have some doubts on polymorphism concept i.e; while call by Reference

Totally unrelated.


Also, Java doesn't even have call-by-reference. References are passed by value. Nothing is passed by reference.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is it Polymorphism Concept wrong or right?