Demir
Demir

Reputation: 859

How to call for method of an object that is in an arraylist collection?

I was doing my lab hw. I have been trying to solve that problem for more than an hour The polymorphism btw classes Shape(Triangle + Rectangle) Triangle(ScaleneTriangle, EquilateralTriangle, RightTriangle) Rectangle(Square)

Java solution also will be fine for me.

    
            Triangle scalene = new ScaleneTriangle(3,4,5);
            Triangle equilateral= new EquilateralTriangle(3,3,3);
            Triangle right = new RightTriangle(3,4,5);
            Rectangle square = new Square(4);
            Rectangle rectangle = new Rectangle(2,5);
            
           List<Shape> shapes = new List<Shape>();
           //All the shapes are derived classes of Shape
            shapes.Add(equilateral);
            shapes.Add(right);
            shapes.Add(scalene);
            shapes.Add(rectangle);
            shapes.Add(square);
            foreach (var shape in shapes)
            {
                if (shape is Triangle)
                {
                    shape.        //I want to call the method of equilateral, right, scalene which are derived 
                                 //classes of Triangle

                }
                else if (shape is Rectangle)
                {
                    shape.    //I want to call the method of the rectangle(base) and square(derived)
                }
                    
            }

Upvotes: 1

Views: 80

Answers (3)

Caius Jard
Caius Jard

Reputation: 74740

C#

You probably want as:

            if (shape is Triangle)
            {
                (shape as Triangle).GetHypotenuseLength();        
                (shape as Triangle).GetSmallestCornerAngle();        
                (shape as Triangle).GetTypeOfTriangle();        
            }

You can make your life easier not having to as all the time:

            if (shape is Triangle t)
            {
                t.GetHypotenuseLength();        
                t.GetSmallestCornerAngle();        
                t.GetTypeOfTriangle();      
            }

Note that a lot of this is against polymorphism; the whole cool thing about polymorphing is that you don't write a bunch of code that is "if it's a triangle then.. if it's a square then..." because that code can only ever cope with the shapes it knows about at the time. If your mate adds a plugin that implements pentagon your code will flat out ignore it

The whole idea of polymorphism is that ALL your shapes can (because they descend from Shape which has a DrawYourselfOnThisCanvasPlz(Canvas c) method) is that each different shape knows how to draw itself on a canvas, so you can just:

foreach(Shape s in listOfShapes)
  s.DrawYourselfOnThisCanvasPlz(c);

Now your triangles and squares will draw themselves, and your mate's pentagon will draw itself if the user of your paint program has added pentagons into the list of shapes...

Aim to try and find ways of treating all these things as generic shapes, and not as particular instances of exact shapes that you know about right now.. Rather than if triangle then call_those_triangle_methods just have a shape.PrintSomeInfoAboutYourselfToTheConsole() and the square will say "hey, i'm a square and my sides are..." and the triangle says "i'm a triangle with angles of .. and a hypotenuse of ..."..

Upvotes: 0

WJS
WJS

Reputation: 40057

You need to cast it to the Triangle or Rectangle before calling the method. Here is the Java solution.

for (Shape shape : shapes) {
        if (shape instanceof Triangle) {
             ((Triangle)shape).call method(s) for triangle here
        } else if (shape instanceof Rectangle) {
             ((Rectangle)shape).call method(s) for Rectangle here
        }
}

Upvotes: 0

slevin
slevin

Reputation: 318

In Java, you can cast an object if you know its type:

if (shape instanceof Rectangle) {
    Rectangle rectangle = (Rectangle) shape;
    // whatever the method is called
    rectangle.getWidth()
}

Upvotes: 1

Related Questions