Polymorphism, examples of
This:
PersonInterface personinterface2 = new Person( );
Is creating a Person object called personinterface2 with a PersonInterface type.
This allows this object to use the PersonInterface methods.
Poymorphism simple explaintion:
First, you have to understand what Polymorphism is. In a nut shell you have a super
class, like Dog which has methods for eating, barking and wagging its tail. You
make another class which extends dog ie class GermanShepard extends Dog now
polymorphism is that GermanShepard can have it's own method of PlayingWithBall(),
and it can use the methods from Dog, ie Bark(), Eat(), or WagTail() when they are
overridden. So now, Dog has a constructor with 3 parameters, and GermanShepard has
a constructor with four parameters. The let's say you have an instance of Dog and
an instance of GermanShepard. Polymorphism means that the Object determines which
type it is and therefore which method it calls, the base method or the overridden
method.
Therefore the homework is to modify the MyOwnAutoShopProject so that:
First, you make interfaces for each of the sub-classes of car Car is your super, so
Ford is a sub-class Your interface for Ford has it's own method statement which
will be defined in the Class Ford, ie class Ford extends Car implements
FordInterface() when you have this, Ford not only has it's own method of getWeight
and setWeight but it now can use the methods of Car getSpeed, setSpeed,
getRegularPrice, setRegularPrice, all of which are now also methods of Ford,
overridden. So, the constructor for an Instance of Car would be public Car(int
speed, double regularPrice, String color, double getSalePrice){...your code} and
for Ford it would be public Ford(int speed, double regularPrice, String color,
double getSalePrice, int year, int manufacturerDiscount){...your code}
then the Object called determines which regularPrice method to use. now class Ford
can be by itself or much more dynamic in that it can extends Car, and you don't
have to worry about which of the regularPrice methods it is going to use, the
Object itself will determine that. Hope that points you in the right direction.