Hey Hec! Great example, but let me ask you this: Taking your example, if you had coded:
Dog dog = new Dog(); instead of: Animal dog = new Dog(); Wouldn't the dog object in the first scenario above still inherit the "talk" method just through inheritance from the superclass? Thanks to everyone who's helping, much respect, -m On Wed, Apr 1, 2009 at 3:13 PM, hec-ubuntu <[email protected]> wrote: > > Let's say you have three Animals, a dog, a cat and man. > > > public class Animal{ > > void talk(){ > System.out.println("I'm talking"); > } > > void walk(){ > System.out.println("I'm walking"); > } > } > > public class Man extends Animal{} > public class Dog extends Animal{} > public class Cat extends Animal{} > > If you declare them like this: > > Animal dog = new Dog(); > Animal cat = new Cat(); > Animal man = new Man(); > dog.talk(); > cat.talk(); > man.talk(); > > They would do exactly the same thing since they've inherited the talk() > method which simply prints "I'm talking". Same rules apply to the walk() > method. > > Now the problem is that dogs bark and cats purr. Try placing this under > the Dog class: > > public class Dog { > @Override > public void talk(){ > System.out.println("bark"); > } > } > > Calling the dog.talk() will now produce a different result. > > To make the long story short, super classes provide common tasks that > can be used by it's subclasses. Feel free to correct / add if I've > missed out something here. =) > > > > > > > > > -- Love is a snowmobile racing across the tundra and then suddenly it flips over, pinning you underneath. At night, the ice weasels come. - Matt Groening --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/javaprogrammingwithpassion?hl=en -~----------~----~----~----~------~----~------~--~---
