Amazon.com Widgets Question on exercise 5
Welcome, Guest. Please login or register.
Did you miss your activation email?
September 14, 2014, 06:59:41 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Programming in Objective-C, 4th edition
| |-+ Exercises
| | |-+ Chapter 3
| | | |-+ Question on exercise 5
Pages: [1] Go Down
Print
Author Topic: Question on exercise 5 (Read 3078 times)
jonr
Full Member
***
Posts: 118






on: April 12, 2012, 10:46:10 AM

I don't understand how the question at the end of the exercise relates to the scenario described.

There is a class called Vehicle and an object from that class called myVehicle, which can be either a car, motorcycle or boat.  The question then asks, 'do you see any advantages of being able to apply an action to an object that could be from one of several classes?

In the scenario there are not several classes, only one.  There is also one object (myVehicle) but it could be 3 types of vehicles (car, motorcycle, or boat).

Logged
rue
Jr. Member
**
Posts: 53






Reply #1 on: April 23, 2012, 05:51:16 PM

There is what you call inheritance in object oriented programming.
http://en.wikipedia.org/wiki/Parent_class#Subclasses_and_superclasses

In this example, Vehicle is a class. (think of it as a superclass, parent class, ancestor class)
And Car, Motorcyle, and Boat are also classes derived from the Vehicle class. So they are subclasses, or child classes, and their parent is the Vehicle class.

If the Vehicle class has a method called prep, getGas, and service, then the same methods will also be available/will work with Car, Motorcyle and Boat even though in your program you may not have specifically created these methods for Car/Motorcyle/Boat. The Car/Motorcyle/Boat will use the methods of it's parent class, in this case Vehicle.

Note how Car, Motorcyle, Boat is capitalized... the naming convention for a class.

... that's how I understand it. If I'm wrong, experts please chime in. Smiley
Logged
Edward.Seshoka
Newbie
*
Posts: 1


Email




Reply #2 on: January 01, 2013, 05:56:31 AM

Well the question is also confusing me because myVehicle is not made from several classes rather those classes are made from myVehicle, unless I am really not getting it, maybe applying the same method to several class objects?
Logged
MacD
Newbie
*
Posts: 20






Reply #3 on: January 01, 2013, 10:38:25 AM

My understanding of it is that myVehicle is an object (an instance) of the Vehicle class. This object has functionality (getGas, service prep) common to Car, Motorcycle, and Boat which are considered vehicles. With Object Oriented Programing this functionality can be reused (inherited) by Car, Motorcycle, and Boat, no need to write the same code for each of them. With inheritance Car, Motorcycle, and Boat can all use the  getGas, service prep methods of  Vehicle class.

If anyone sees it different speak now or forever hold your peace.  Smiley
Logged
jonr
Full Member
***
Posts: 118






Reply #4 on: January 28, 2013, 08:00:30 PM

What you say seems correct, at least I think so (it's in line with my understanding).  However, your point doesn't really address the question I had about the poorly worded question (IMHO) with regards to its mismatch to the scenario. 

It seems the question was trying to get recognition of the fact that actions that are applied to classes that could contain different types of vehicles is convenient.  If you define a class so that it could contain different vehicle types (that so happen to have some common traits), you can apply the same actions to all of them, since they are in the same class.  However, the wording of the question seemed to be off-base...that's what I want to know:  Whether I'm correct in thinking the question was incorrect.

It's always a little frustrating in tech books when language useage, particularly at the beginner stage is off, even just a little bit.  Sometimes even little things can throw off beginners, and sometimes you actually get *penalized* when reading closely because that's when you start noticing inconsistencies.  People that skim through things have the benefit of being oblivious to contradictions and little inaccuracies.
Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.