H
HalcyonWild
Hi all,
First of all, Thanks a lot to all of you. Helped me clear up my
confusion to some extent.
I was trying out examples posted by people on this thread. I picked up
on examples given by Shakah, Thomas Weidenfeller and Dale King, and
worked upon it.
See Listing 1. I was thinking what if it is not possible to have a
common base class for the two subclasses, for example, I decide that
MyCircle and MySquare are two different classes altogether. Now I
create a MyCircle instance and a MySquare instance. And call a local
overloaded getArea() method. Whatever is passed, it recognizes the
correct call, and calls the appropriate class method to calc the area.
Note that passing MyShape instance will not work, in case you pass
MyShape and do not overload getArea() to receive MyShape parameter.
Polymorphism is one way here.
The Listing 2 changes a bit. I added getShape() method which returns
MyShape. Now MyCircle and MySquare are subclasses of MyShape. I do not
know what getShape() will return me. I can very well put MySquare
there, instead of MyCircle. And it works fine.
One dead end. I cannot modify MyCircle properties using the MyShape
ref, like setting the radius (in case i do not use the constructor to
set radius). I would need to know what shape it exactly is, so that I
know I want to change the radius or the length of the shape.
Here is one situation where you would want to use instanceof or
getClass().getName(); Let me know if I am wrong. Also let me know if
you need any clarifications. Sooner or Later, I will get back.
Listing 1.
//save in YourClass.java
public class YourClass {
public double getArea(MyCircle circle) {
return circle.calcArea() ;
}
public double getArea(MySquare square) {
return square.calcArea();
}
public static void main(String args[]) {
YourClass y = new YourClass();
MyCircle circle = new MyCircle();
MySquare square = new MySquare();
circle.setRadius(10.0);
square.setLength(10.0);
System.out.println(y.getArea(circle));
System.out.println(y.getArea(square));
}
}
class MyCircle
{
private double dRadius_;
public void setRadius(double r)
{
this.dRadius_ = r;
}
public double calcArea()
{
return (3.14159 * dRadius_ * dRadius_);
}
}
class MySquare
{
private double dLength_;
public void setLength(double l)
{
this.dLength_ = l;
}
public double calcArea()
{
return (dLength_ * dLength_);
}
}
=====================================
listing 2
public class YourClass {
public MyShape getShape()
{
MyShape shape = new MyCircle(10.0);
return shape;
}
public static void main(String args[]) {
YourClass y = new YourClass();
MyShape s = y.getShape();
System.out.println(s.calcArea());
}
}
abstract class MyShape {
public abstract double calcArea() ;
}
class MyCircle extends MyShape
{
private double dRadius_;
public MyCircle()
{
this.dRadius_ = 0.0;
}
public MyCircle(double d)
{
this.dRadius_ = d;
}
public void setRadius(double r)
{
this.dRadius_ = r;
}
public double calcArea()
{
return (3.14159 * dRadius_ * dRadius_);
}
}
class MySquare extends MyShape
{
private double dLength_;
public void setLength(double l)
{
this.dLength_ = l;
}
public double calcArea()
{
return (dLength_ * dLength_);
}
}
First of all, Thanks a lot to all of you. Helped me clear up my
confusion to some extent.
I was trying out examples posted by people on this thread. I picked up
on examples given by Shakah, Thomas Weidenfeller and Dale King, and
worked upon it.
See Listing 1. I was thinking what if it is not possible to have a
common base class for the two subclasses, for example, I decide that
MyCircle and MySquare are two different classes altogether. Now I
create a MyCircle instance and a MySquare instance. And call a local
overloaded getArea() method. Whatever is passed, it recognizes the
correct call, and calls the appropriate class method to calc the area.
Note that passing MyShape instance will not work, in case you pass
MyShape and do not overload getArea() to receive MyShape parameter.
Polymorphism is one way here.
The Listing 2 changes a bit. I added getShape() method which returns
MyShape. Now MyCircle and MySquare are subclasses of MyShape. I do not
know what getShape() will return me. I can very well put MySquare
there, instead of MyCircle. And it works fine.
One dead end. I cannot modify MyCircle properties using the MyShape
ref, like setting the radius (in case i do not use the constructor to
set radius). I would need to know what shape it exactly is, so that I
know I want to change the radius or the length of the shape.
Here is one situation where you would want to use instanceof or
getClass().getName(); Let me know if I am wrong. Also let me know if
you need any clarifications. Sooner or Later, I will get back.
Listing 1.
//save in YourClass.java
public class YourClass {
public double getArea(MyCircle circle) {
return circle.calcArea() ;
}
public double getArea(MySquare square) {
return square.calcArea();
}
public static void main(String args[]) {
YourClass y = new YourClass();
MyCircle circle = new MyCircle();
MySquare square = new MySquare();
circle.setRadius(10.0);
square.setLength(10.0);
System.out.println(y.getArea(circle));
System.out.println(y.getArea(square));
}
}
class MyCircle
{
private double dRadius_;
public void setRadius(double r)
{
this.dRadius_ = r;
}
public double calcArea()
{
return (3.14159 * dRadius_ * dRadius_);
}
}
class MySquare
{
private double dLength_;
public void setLength(double l)
{
this.dLength_ = l;
}
public double calcArea()
{
return (dLength_ * dLength_);
}
}
=====================================
listing 2
public class YourClass {
public MyShape getShape()
{
MyShape shape = new MyCircle(10.0);
return shape;
}
public static void main(String args[]) {
YourClass y = new YourClass();
MyShape s = y.getShape();
System.out.println(s.calcArea());
}
}
abstract class MyShape {
public abstract double calcArea() ;
}
class MyCircle extends MyShape
{
private double dRadius_;
public MyCircle()
{
this.dRadius_ = 0.0;
}
public MyCircle(double d)
{
this.dRadius_ = d;
}
public void setRadius(double r)
{
this.dRadius_ = r;
}
public double calcArea()
{
return (3.14159 * dRadius_ * dRadius_);
}
}
class MySquare extends MyShape
{
private double dLength_;
public void setLength(double l)
{
this.dLength_ = l;
}
public double calcArea()
{
return (dLength_ * dLength_);
}
}