Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Question about Casting and Inheritance

Question about Casting and Inheritance

Scheduled Pinned Locked Moved C#
questionoop
6 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tsunsau717
    wrote on last edited by
    #1

    Suppose I have Class: 1) class Shape{} 2) class Square : Shape {} 3) class Circle : Shape {} and Method: 1) void f(Shape s){} 2) void f(Square s){} 3) void f(Circle c){} Now, I declare 3 variables: 1) Shape a1 = new Shape(); 2) Square a2 = new Square(); 3) Circle a3 = new Circle(); If I call f(a1), it will go to method 1. If I call f(a2), it will go to method 2. If I call f(a3), it will go to method 3. Right? But If I call f((Shape)a2), it will go to method 1. Is there anyway to make if I call f((Shape)a2), and it will go to method 2 directly?

    P R K 3 Replies Last reply
    0
    • T tsunsau717

      Suppose I have Class: 1) class Shape{} 2) class Square : Shape {} 3) class Circle : Shape {} and Method: 1) void f(Shape s){} 2) void f(Square s){} 3) void f(Circle c){} Now, I declare 3 variables: 1) Shape a1 = new Shape(); 2) Square a2 = new Square(); 3) Circle a3 = new Circle(); If I call f(a1), it will go to method 1. If I call f(a2), it will go to method 2. If I call f(a3), it will go to method 3. Right? But If I call f((Shape)a2), it will go to method 1. Is there anyway to make if I call f((Shape)a2), and it will go to method 2 directly?

      P Offline
      P Offline
      partyganger
      wrote on last edited by
      #2

      I'm not sure what you want to do, but here's my two cents: If your parameter is cast to Shape, and Square is derived from Shape, first the runtime / compiler will look in the class Square if it can find a method with the f(Shape s) signature. Since it can only find the f(Square s) method, it'll go up on class (which is Shape) and there it WILL find the wanted menthod. If you want class Square to have it's own f(Shape s) method, you'll have to overload the original one, i.e. recreate the f(Shape s) method in the Square class...

      T 1 Reply Last reply
      0
      • P partyganger

        I'm not sure what you want to do, but here's my two cents: If your parameter is cast to Shape, and Square is derived from Shape, first the runtime / compiler will look in the class Square if it can find a method with the f(Shape s) signature. Since it can only find the f(Square s) method, it'll go up on class (which is Shape) and there it WILL find the wanted menthod. If you want class Square to have it's own f(Shape s) method, you'll have to overload the original one, i.e. recreate the f(Shape s) method in the Square class...

        T Offline
        T Offline
        tsunsau717
        wrote on last edited by
        #3

        Actually the method f is not a member method of Shape, Square, Circle. It is static method of another class, which can takes "Shape" or "Square" or "Circle" as parameter. Actually, what I am doing now is: public static f(Shape s){ if (s is Square){ f((Square)s); } else if (s is Circle){ f((Circle)s); } } public static f(Square s){ MessageBox.Show("It is a Square"): } public static f(Circle s){ MessageBox.Show("It is a Circle"): } It works, but the problem is when there is a lot of derived class, the f(Shape c) method will be very bulky.

        P 1 Reply Last reply
        0
        • T tsunsau717

          Suppose I have Class: 1) class Shape{} 2) class Square : Shape {} 3) class Circle : Shape {} and Method: 1) void f(Shape s){} 2) void f(Square s){} 3) void f(Circle c){} Now, I declare 3 variables: 1) Shape a1 = new Shape(); 2) Square a2 = new Square(); 3) Circle a3 = new Circle(); If I call f(a1), it will go to method 1. If I call f(a2), it will go to method 2. If I call f(a3), it will go to method 3. Right? But If I call f((Shape)a2), it will go to method 1. Is there anyway to make if I call f((Shape)a2), and it will go to method 2 directly?

          R Offline
          R Offline
          Roman Rodov
          wrote on last edited by
          #4

          Why write 3 different overloads of the method if you are casting all the parameters to Shape anyway. The whole point of method overloads is that you can theoretically pass it any kind of Shape derived object(or whatever for that matter) and the runtime will work out the right overload to call. Maybe I am not clearly understanding what you are trying to do, but this seems a little bit silly.

          1 Reply Last reply
          0
          • T tsunsau717

            Actually the method f is not a member method of Shape, Square, Circle. It is static method of another class, which can takes "Shape" or "Square" or "Circle" as parameter. Actually, what I am doing now is: public static f(Shape s){ if (s is Square){ f((Square)s); } else if (s is Circle){ f((Circle)s); } } public static f(Square s){ MessageBox.Show("It is a Square"): } public static f(Circle s){ MessageBox.Show("It is a Circle"): } It works, but the problem is when there is a lot of derived class, the f(Shape c) method will be very bulky.

            P Offline
            P Offline
            partyganger
            wrote on last edited by
            #5

            I Really don't understand what you're trying to do here. WHy define a static class where you process objects that have nothing to do with that class (it is static, so the class that holds the static method can't do anything with the passed object???? The whole idea behind OO is that you put things that belong together in a class, so if you want to edit a Square, use the f in Square. Then, there's the "virtual" keyword as well, if you define f as virtual in Shape and pass it a Square it will try to locate the f method in Shapes subclasses.....

            1 Reply Last reply
            0
            • T tsunsau717

              Suppose I have Class: 1) class Shape{} 2) class Square : Shape {} 3) class Circle : Shape {} and Method: 1) void f(Shape s){} 2) void f(Square s){} 3) void f(Circle c){} Now, I declare 3 variables: 1) Shape a1 = new Shape(); 2) Square a2 = new Square(); 3) Circle a3 = new Circle(); If I call f(a1), it will go to method 1. If I call f(a2), it will go to method 2. If I call f(a3), it will go to method 3. Right? But If I call f((Shape)a2), it will go to method 1. Is there anyway to make if I call f((Shape)a2), and it will go to method 2 directly?

              K Offline
              K Offline
              killerslaytanic
              wrote on last edited by
              #6

              Seems to me you have some misunderstanding about the concepts: First of all the methods "f" belong to which class(es)? Second: The typecast (shape)a2 is redundant (don´t remember if it is syntactically ok but if so it has nonsense anyway)a2 is a Square so it IS a Shape anyway how inheritance and polymorphism works... I guess you wanted to do something like this: You have the classes and their respective methods: class Shape { **virtual** void f{} to treat the Shape object (Whatever it be) who owns it } class Square : Shape { **override** void f{}//supposedly to treat the Square object who owns it } class Circle : Shape { **override** void f{}//supposedly to treat the Circle object who owns it } if you do Shape a1 = new Shape(); Square a2 = new Square(); Circle a3 = new Circle(); Logically, a1.f() will call the Shape´s f method; a2.f() the Square´s, etc BUT ALSO if you do this....( and that´s the nice part) **Shape a1** ; Square a2 = new Square(); Circle a3 = new Circle(); **a1= a2**; a1.f(); //Will call the Square´s f method **a1= a3**; a1.f(); //Will call te Circle´s f method which is something similar to what you said and is the usual way in which these kind of things are accomplished This happens because the fmethod is declared virtual in Shape class an is overriden in the child classes otherwise, if they weren´t virtual then the calls of a1.f() always would call the Shape´s f method Notice this approach is specially useful in this case Class TestShapes{ void DoSomethingWithShapes(Shape s) { s.f(); //it will call the proper f method regarding if s is in fact a Square or a Circle or a general Shape (if this class is conceived to have instances ,thing that generally shouldn´t be taht way) } } Maybe it helps

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups