1. a. Create a parent class "Vehicle" with one method "turnLeft" that prints "Vehicle turning left".
b. Create a child class "Bike" which overrides the "turnLeft" method and prints "Bike turning left".
c. Create a Vehicle reference referring to a Bike object. Is it legal? What happens when the "turnLeft" method is called through the Vehicle reference?

2. a. Create a parent class "Animal" with one method "play" that prints "Animal is playing".
b. Create a child class "Dog" with two methods "bark" and "play" that prints "Dog is barking" and "Dog is playing" respectively. Override the "play" method in the "Dog" class.
c. Create a "Dog" object and invoke the play method. Which play method gets called?
d. Create an Animal reference to the Dog object. Is it legal? What happens when the play method is called through the Animal reference? What happens when the bark method is called through the Animal reference?
e. What happens when you create a Dog reference to the Animal object?

3. Write an object oriented program to simulate an Orchestra with a collection of few musical instruments like Guitar, Piano and Drum. Define a polymorphic method called "Play" in each of the instrument. The Play method will print "Guitar" when Guitar is played, "Piano" when Piano is played, and "Drum" when a Drum is played.

Let us say Cacofonix now joins the Orchestra with his Violin. Extend your program to demonstrate that you don\'t have to touch the existing code corresponding to the other instruments.

Recommended Answers

All 2 Replies

1. a. Create a parent class "Vehicle" with one method "turnLeft" that prints "Vehicle turning left".
b. Create a child class "Bike" which overrides the "turnLeft" method and prints "Bike turning left".
c. Create a Vehicle reference referring to a Bike object. Is it legal? What happens when the "turnLeft" method is called through the Vehicle reference?

A vehicle that only turns left. That is scary. I don't want to be in the road with you driving:)

Create a parent class Daniweb, with one method "help me with my homework" which takes two parameters: problemDefinition and whatIDidSoFar.
If the second parameter is null, throw a NoHelpWithoutEffort Exception

commented: I will honestly implement that +7
commented: I do not want to be sarcastic, but you got that one right +19
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.