Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

simple qustion in polymorphism

polymorphism

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1 azazo

azazo

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 68 posts

Posted 12 June 2013 - 04:10 AM

hi ,

 

i have qustion in  polymorphism

 

 

 Which of the  statements  are examples of polymorphism in main class ?

public class Tester {

    public static void main(String[] args) {
    
                
            Circle cir = new Circle(10);
            Rectangle rect = new Rectangle(5, 9);
            GeomatricObject geo;
            geo = cir;
            System.out.println(geo.getArea());
            System.out.println(geo.getPerimeter());
            geo = rect;
            System.out.println(geo.getArea());
            System.out.println(geo.getPerimeter());

        }
    }

 

 

 

 

 

 

 



#2 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts

Posted 12 June 2013 - 05:10 AM

Without the source code, I can make an educated quess that it's:

geo = cir;
System.out.println(geo.getArea());
System.out.println(geo.getPerimeter());

geo = rect;
System.out.println(geo.getArea());
System.out.println(geo.getPerimeter());

because you are using the base class to store the child class, then call it's methods.


My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth

#3 azazo

azazo

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 68 posts

Posted 12 June 2013 - 07:00 AM

Without the source code, I can make an educated quess that it's:

geo = cir;
System.out.println(geo.getArea());
System.out.println(geo.getPerimeter());

geo = rect;
System.out.println(geo.getArea());
System.out.println(geo.getPerimeter());

because you are using the base class to store the child class, then call it's methods.

 

 

you mean those codes are  polymorphism??



#4 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts

Posted 12 June 2013 - 07:05 AM

It is in the sense that geo is not of class circle or rectangle, nor does it have knowledge they even exist, yet the program calls the respective class's methods correctly.


My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download