File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Ball example - Inheritance ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Ball example - Inheritance ?" Watch "Ball example - Inheritance ?" New topic
Author

Ball example - Inheritance ?

Pavan Kumar Dittakavi
Ranch Hand

Joined: Feb 12, 2011
Posts: 104

Hello All,

I am wondering whether the following example comes under Inheritance or not. Now, the example that I have chosen is a cricket ball construction.

Just a brief back ground: A cricket ball looks like this. [ http://3.bp.blogspot.com/-X2232vHYQwM/UPlY3_-2qbI/AAAAAAAAAAw/G3juwFNBGVI/s1600/cricket+ball.jpg ]. Now, if you actually take a look at its internal structure, it looks like this. [ http://www.cricketstoreonline.com/product_images/uploaded_images/img-0233.jpg ]. It has an internal cork ball around which 3-4 layers are created and finally a Cricket Ball [ first pic ] is created.

Now, I am taking this core cork ball as my base class and am explaining the creation of the cricket ball [ my sub class ] as a customization of the base class.

Before I go further, I want to know if taking the base cork ball as base class and the cricket ball as sub class with its own features seem logical/reasonable. [ the reason why Im asking is sometimes I ask myself if this should have been composition where the Cricket Ball contains a cork ball at its center and not an inheritance relationship ].

Please share your thoughts.

Thanks,
Pavan.
Piet Souris
Ranch Hand

Joined: Mar 08, 2009
Posts: 659
    
  11
hi Pavan,

sometimes it is all a matter of taste, or feeling, or whatever.

Would you say that a 'car' extends 'engine'? In the case of a cricket ball, I would say that a cricket ball is more or less a ball, something that you can kick and beat, and so I would see it more like an extended ball, which happen to contain a cork part. But that cork may not be essential, maybe one day it will be made of a cheaper replacement ball or so.
In snooker, would you consider a snooker ball to extend 'ivory'?

But if you define 'extends' as 'is (mainly) made of' then yes, a cricket ball would extend cork.

So, all in all, I think your own opinion is at least as good as anyone else's opinion.

Greetz,
Piet
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8043
    
  22

Pavan Kumar Dittakavi wrote:Please share your thoughts.

I'd say your last thought is closer to the mark - composition, not inheritance - although analogies like this are always fraught with danger.

Most similes of inheritance actually use examples that we already understand as inheritance, eg a biological tree:
Animal→Reptile→Crocodile
although there are more abstract examples (usually involving interfaces), eg:
Collection→List→LinkedList

HIH

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

It all boils down to whether a cricket ball is-a cork ball or has-a cork ball.


luck, db
There are no new questions, but there may be new answers.
manish ghildiyal
Ranch Hand

Joined: Jan 12, 2013
Posts: 136


Output of this program may help you in understanding things.

Manish
Pavan Kumar Dittakavi
Ranch Hand

Joined: Feb 12, 2011
Posts: 104

manish ghildiyal wrote:

Output of this program may help you in understanding things.

Manish


Manish, please see the question again. Iam familiar with this stuff. I want to know where this category fall in..Inheritance/Composition thats it.

Winston/Piet Thanks for your response. I am actually interested in explaining the concept of inheritance by taking a very basic example which people are quite familiar with [ atleast in India ]...Cricket. Guess I need to come up with another example.

Thanks guys..
manish ghildiyal
Ranch Hand

Joined: Jan 12, 2013
Posts: 136
Pavan Kumar Dittakavi wrote:
manish ghildiyal wrote:

Output of this program may help you in understanding things.

Manish


Manish, please see the question again. Iam familiar with this stuff. I want to know where this category fall in..Inheritance/Composition thats it.

Winston/Piet Thanks for your response. I am actually interested in explaining the concept of inheritance by taking a very basic example which people are quite familiar with [ atleast in India ]...Cricket. Guess I need to come up with another example.

Thanks guys..


What the output suggests is that composition can't be the case, else there would be a parent class object and method call would have been made on that.

Manish
Pavan Kumar Dittakavi
Ranch Hand

Joined: Feb 12, 2011
Posts: 104

Hello Guys,

I have come up with another example where in I would have a core class that offers me a very basic text editor[notepad?] mechanism. Then layers/customizations on it would give me a better editor[notepad++/wordpad].
So, in this example, my base class would be NOTEPAD and my sub classes would be [hypothetical] notepad++/wordpad.


I am pretty sure this is much more close to the concept of Inheritance than the ball I presented earlier. .How do you feel?.

Thanks,
Pavan.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8043
    
  22

Pavan Kumar Dittakavi wrote:I am pretty sure this is much more close to the concept of Inheritance than the ball I presented earlier. .How do you feel?.

I think you really need to re-read Darryl's post because the answer is (usually) quite simple: Is a Notepad++ a Notepad?

Inheritance almost always indicates a specialization, which in database parlance is a "subset" relationship. Some cars, for example, might be made for racing, so all RacingCars are Cars, but not all Cars are RacingCars. All SavingsAccounts in a Bank are Accounts, but not all Accounts are SavingsAccounts.

You can also have "sibling" inheritance relationships (in database parlance, an exclusive subset): eg, a Shape, which can be a Circle or a Square or a Triangle, but not more than one.

You could certainly design an extendable Editor class that could be specialized, but I'm not so sure that things like Notepad/Notepad++ are actually examples of it, except in the most general sense of "features" - especially since the latter wasn't written by MS (at least I assume it wasn't ).

Winston
Pavan Kumar Dittakavi
Ranch Hand

Joined: Feb 12, 2011
Posts: 104

Winston Gutkowski wrote:

You could certainly design an extendable Editor class that could be specialized, but I'm not so sure that things like Notepad/Notepad++ are actually examples of it, except in the most general sense of "features" - especially since the latter wasn't written by MS (at least I assume it wasn't ).


Winston


Thanks for the inputs Winston. And yes, Notepad++ as such does not make any sense. But I have mentioned that my new specialization of notepad would be a hypothetical Notepad++. So, when I say this Im trying to convey that this hypothetical Notepad++ would have better handling of text/enhanced features blah blah blah. Im sorry if I was not clear. Thanks again .
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Ball example - Inheritance ?