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. =) 






--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to