Skip to content
Advertisement

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 following assignmennts

How would I make a new fraction if a fraction cannot be simplified?

So I’m writing an assignment for my OOP class that asks the user for a number of fractions, then randomly generates that number of fractions. Part of the assignment states that if one of the fractions cannot be simplified then skip it and create a new fraction that can be simplified. However the unreducible fractions still make it through. I

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 piece

Advertisement