Click here to Skip to main content
15,895,192 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi Everyone
This is my code:
Point.java
Java
public class Point {
	
	protected double x;
	protected double y;
	
	public Point() 
	{
		
	}
	
	public Point(double x, double y)
	{
		this.x = x;
		this.y = y;
	}
	
	public Point(Point point)
	{
		x = point.x;
		y = point.y;
	}
	
	public Point add(Point z)
	{
		return new Point(x + z.x, y + z.y);
	}
	
	@Override
	public String toString()
	{
		return "Point: (" + x + "," + y + ")";
	}
}

Shape.java:
Java
public abstract class Shape {
	protected Point position;
	
	public void move(double x, double y){ 
		position.x = x;
		position.y = y;
	}
	
	public abstract void show();
}

Line.java:
Java
public class Line extends Shape{
	
	Line(Point start, Point end)
	{
		position = new Point(start);
		this.end = new Point(end.x - start.x, end.y - start.y);
	}
	
	@Override
	public String toString()
	{
		return "Line: " + position + " to " + position.add(end);
	}
	
	public void show()
	{
		System.out.println("\n" + toString());
	}
	
	private Point end;
}

MainClass.java:
Java
public class ShapesMain {

	public static void main(String[] args) {
		Shape shapes[] = new Shape[10];
		Point pts = null;
		Point p1 = null;
		Point p2 = null;
		double minRadius = 20.0;
		double maxRadius = 20.0;
		double maxDist = 100.0;
		int minPts = 2;
		int maxPts = 15;
		int choice = 0;
		
		for(int i = 0; i < 10; i++)
		{
			choice = (int) (Math.random() * 3);
			switch(choice)
			{
			case 0:
				p1 = new Point((int) (Math.random()*maxDist), (int) (Math.random()*maxDist));
				p2 = new Point((int) (Math.random()*maxDist), (int) (Math.random()*maxDist));
				shapes[i] = new Line(p1, p2);
				break;
			case 1:
				p1 = new Point((int) (Math.random()*maxDist), (int) (Math.random()*maxDist));
				shapes[i] = new Circle(p1, (int) (minRadius + (maxRadius - minRadius) * Math.random()));
				break;
			case 2:
				p1 = new Point((int) (Math.random() * maxDist), (int) (Math.random() * maxDist));
				p2 = new Point((int) (Math.random() * maxDist), (int) (Math.random() * maxDist));
				shapes[i] = new Rectangle(p1, p2);
				break;
			default:                                                      
		          System.out.println("\nInvalid shape choice = " + choice);
		          System.exit(1);
		          break;
			}
		}
		
		for(Shape shape : shapes)
		{
			shape.show();
		}
	}

}

Sample Output:
Line: Point: (53.0,50.0) to Point: (54.0,36.0)

Question:
When I call the show method(Polymorphic behavior), how does the the toString() method in the point class gets called?

If shapes[1] is a line and i call shapes[1].show, it executes
Java
public void show()
    {
        System.out.println("\n" + toString());
    }

which in turn calles the toString() method:
Java
@Override
	public String toString()
	{
		return "Line: " + position + " to " + position.add(end);
	}

how does the position in the toString() gets replaced by toString() method in the Point class?

Thank you
Posted
Comments
Sergey Alexandrovich Kryukov 29-Dec-14 12:27pm    
I did not get it. Are you asking about the mechanism, how the call is dispatched to the version of the runtime type, despite the fact that the calling code is in the base class? If this is the question, the answer is: the call is done through virtual method table...
—SA
abisheak Balaji 29-Dec-14 21:19pm    
I understand that.What I am trying to ask is that how does position variable of type Point calls it's toString() method from the toString() method of Line class.

1 solution

Please see my comment to the question. If I understood it correctly, you want to understand the mechanism, which is very good. Please see:
http://en.wikipedia.org/wiki/Dynamic_dispatch[^],
http://en.wikipedia.org/wiki/Method_overriding[^],
http://en.wikipedia.org/wiki/Virtual_method_table[^].

Please try to understand it. If it is hard to make a whole picture based in this information, please ask; I would show this in a simple example, but it should be really much simpler; yours clouds the essence of things under a lot of unrelated detail.

—SA
 
Share this answer
 
Comments
abisheak Balaji 29-Dec-14 21:16pm    
Those resources were really helpful.Thanks! :-)
@Override
public String toString()
{
return "Line: " + position + " to " + position.add(end);
}
My question is how does the position variable of type Point in this line of code actually refers to toString method in the Point class.Shouldn't I specify it as position.x and position.y to get the co-ordinates?or atleast shouldn't I do something like position.toString()?

Thanks
Sergey Alexandrovich Kryukov 29-Dec-14 22:23pm    
If position is of the type Point, you don't have to address to x and y separately, because you already defined Point.toString. In good programming style such method is a must. In practice, however, this implementation is often ignored, because methods showing coordinate use its own formatting.

Using string concatenation is not really good, it's much better to use string formatting:
http://docs.oracle.com/javase/tutorial/java/data/numberformat.html.

Normally, from the practical standpoint, toString implementations in purely data classes are rather considered as some fall-back must-have methods, not so much of refined methods which go to the end-user applications where pure data is usually used and formatted. Such fall-back string representation is more used during development, testing, via the debugger, etc. However, nothing prevents you writing "final" toString representations for ad-hoc highly semantic (less abstract) types...

If I finally answered your questions, please consider to accept this solution formally (green "Accept" button). In all cases, your follow-up questions will be welcome.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900