Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is it Polymorphism Concept wrong or right?

 
sachin santaji
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10109
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic