• Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt on Dynamic polymorphism  RSS feed

 
Martin Arun Paul Perumala
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following program illustrates dynamic polymorphism by passing required object at run time by "command line arguments". The program is very simple finding out area of rectangle and square using Hierarchial inheritance.(Shape is super class)

MY Problem is that in the last second line of the code " x = args[0] " I am trying to pass the object (square) using command line arguments. But the error is getting displayed as "incompatible type one is string and other is object"
Kindly someone help me .
Thanks
// I did not post full code //
class Shapedemo
{
public static void main(String args[])
{
Shape x;
Rectangle rectangle = new Rectangle();

Square square = new Square();

rectangle.setvalue();
square.input();

x= args[0];
x.area();


}
}
[ December 11, 2008: Message edited by: Martin Arun Paul Perumala ]
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch. Please use code tags when you post code.
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
args is String that is declared in the main.(i dont know what do you mean by saying that you are sending object through Command line)
X is a shape class reference.
Now string class does not extend shape.
So String class and Shape class has no relation.
So the error pops.
[ December 11, 2008: Message edited by: James Tharakan ]
 
Martin Arun Paul Perumala
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean I am trying to execute the program like

" Java Shapedemo square" where I am passing square object as commandline argument.

Or in another way can a object be passed as a commanline argument ?

without commandline argument the code would look like

x=rectangle;
x.area();

x=square;
x.area();

I want to calculate Square area only(not both) at run time..how can I do it ?
Thanks .
[ December 11, 2008: Message edited by: Martin Arun Paul Perumala ]
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i totally understood what is your doubt and what is that you are trying to do.
But i really dont know how to explain that to you.
And as far as i know It is impossible
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
args[] is declared as String in the main.
So what ever argument you send to main would be taken as String.
No matter what you send (shape or rectangle object as you are thinking) it would consider it as a string.
[ December 11, 2008: Message edited by: James Tharakan ]
 
Martin Arun Paul Perumala
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks anyways.. Atleast now I know that it is impossible. loll
 
Campbell Ritchie
Marshal
Posts: 56587
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are ways of passing Rectangle as a command-line argument and getting a Rectangle object; you can use a Map<String, Shape> or a factory method which takes the name of the class as a parameter.
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Campbell,
Can you explain it how with a simple example?
Because even if we pass map , it would be taken as only strings in main method right?
Regards.
 
Campbell Ritchie
Marshal
Posts: 56587
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would have to populate the Map before starting the application.
 
Ove Lindström
Ranch Hand
Posts: 326
Android Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not impossible, but hard... And you need to know a thing or two about how Java works behind the scene.

Take a look at this code.

First, the abstract Shape-class.


Then the implementing Rectangle class.



And now the magic... Comments in code...

 
Martin Arun Paul Perumala
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Lindstrom I appreciate that..
 
Ove Lindström
Ranch Hand
Posts: 326
Android Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Martin Arun Paul Perumala:
Thanks Lindstrom I appreciate that..


YW... But still... it is easier to take a string that represents a shape and then look up the shape in ha Map of some sort.
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
while calling the main method with the Rectangle class, campbell said to populate the map;
This is what I've done:

output:


Regards.
 
Ove Lindström
Ranch Hand
Posts: 326
Android Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me tell you this once more, and VERY SLOWLY.

You can NOT send in an OBJECT from the command prompt!!!

The only thing you can do is send in Strings and then react depending on the what string that was sent in.

My very, almost, pseudo-code take a name of a java class, a height and a width AS STRINGS!

What you should do is create a map that has a key-value pair, where the key is the string that you want the user to sent into the program from the command prompt and the value is the class/object that you want to use.

So if you populate the map like this:
Map<String, Class<? extends Shape> myShapes = new HashMap<String, Class<? extends Shape>();
myShapes.put("rectangle", Rectangle.class);
myShapes.put("circle", Circle.class);

then you can get the class from the map and then create a new instance of that class.


EDIT:

Also...

Originally posted by ramya narayanan:

output:


Regards.



This it totaly rubbish. When starting a Java application you send in ONE and only ONE java class as argument and that is the one with the main-method. From there you should create any other objects.

Also, if you extend a HashMap, you are a HashMap and there is no need to instanciate another HahsMap.

My suggestion is that you take two steps back and start over again with the basics of Java.
[ December 11, 2008: Message edited by: Ove Lindstr�m ]
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If not, then how can you populate a map & send it at run time as an commandline argument.
Regards.
 
Ove Lindström
Ranch Hand
Posts: 326
Android Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ramya narayanan:
If not, then how can you populate a map & send it at run time as an commandline argument.
Regards.


Do you understand how a map works? It has a key that is unique and value that is some sort of object. The key can be a String. The value a class. Then you can send in a STRING from the commandline and lookup the class in the map, using the STRING that you got from the commandline as the key.

And I'm not yelling. My tone of voice just guarantee that I'm not being misunderstood or ignored...
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks & now I have understood.
Regards.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!