Skip to content

Tag: oop

How to use class type of method parameter?

I have the following classes: Foo1 extends Bar and Foo2 extends Bar I would like to pass the type of either Foo1, or Foo2 in my method to create either a Foo1, or a Foo2, like this: Is there any approach to achieve this? Answer You can have some flexibility using generics, by declaring: With this all the foll…

How to call the method of the subclass using method of Parent class?

I have a Shape method that has two arguments first is the width and the second is the height. I have two subclasses, one is the rectangle and another is triangle. I want to call the area() the method defined in both triangle and rectangle with the help of the area() of the Shape class. I have written this pie…