Jan 20, 2008

Extending the Possibilities

Alright! Hello everyone! Today we are going to cover inheritance! Inheritance is a fairly simple very powerful tool in the JAVA toolkit. A quick breakdown of what it is is that it allows you to take bits and pieces of code that are common among a group of relatable objects (Objects like say cats and dogs because they are both animals and will both have some of the same characteristics). It is similar to factoring out terms in math (or atleast that's how I see it though there are limits to this similarity that we'll get to). If you have:
2x +2
then you can do this...
2(x+1)

Same with our objects.

public Class Cat{
public void eat(){
//eating code
}
}
public Class Dog{
public void eat(){
//eating code
}
}

Both are animals and animals must eat. So this is how we factor out in java.

public Class Animal{
public void eat(){
//eating code
}
}

public Class Cat extends Animal{
}

public Class Dog extends Animal{
}

Since both the Cat and Dog classes extend animal they both will have the eat() method (The word extend is a keyword for this thing we call inheritance). This works for nearly every method that we put in animal now (We'll get to a couple of exceptions).

public Class Animal{
public void eat(){
//eating code
}
public void scratch(){
//scratching code
}
public void doTrick(){
//trick code
}
}

public Class Cat extends Animal{
}

public Class Dog extends Animal{
}

All these methods are now inherited by Cat and Dog. Cat and Dog will both inherit everything that is inheritable from the superclass (the class from which they inherit from). That being said you don't want a Dog to inherit a meow() method so all the methods within the superclass must be useable by the subclasses (the classes that inherit everything). The subclasses will also be able to inherit the instance variables, the variables that tell you about the state of the object.

public Class Animal{
String nameOfPet;
Int age;
}

Our Cat and Dog will now have both a nameOfPet and age. But what if there are two things that both Cat and Dog do but do differently? They both play but they play differently. We have a way to handle that and it links up rather nicely to the previous post on constructors (This is also where the factoring example loses it's usefulness).

public Class Animal{
String name;
Int age;
public void eat(){
//eating code
}
public void scratch(){
//scratching code
}
public void doTrick(){
//trick code
}
public void play(){
//standard play code that works for most animals
}
}

We'll say a dog plays like most animals

public Class Dog extends Animal{
}

We'll say a cat only likes to play with a ball on a string
public Class Cat extends Animal{
public void play(){
//specific code for playing with a ball on a string
}
}

That method in Cat overrides the method in Animal. The compiler will always use the most specific method when deciding which to allow the class to use. Our Cat play() method is more specific then the Animal play() method so it will use the Cat play() method. Now some different cases.

public class Animal{
private void morph(){
//morphing code
}
public void play(){
//playing code
}
}
public class Cat extends Animal{
public void play(Boolean haveALaserPointer){
//playing with laserpointer code
}
}

Alright so this is what happens here:
- our class Cat does not inherit morph(), this is because morph() is private (we'll do more with public, private, default and protected states later but right now public and private are what we are dealing with and for something to be inherited it cannot be private!)
- our class Cat inherits play() from Animal and creates it's own play method with the parameter Boolean haveALaserPointer. This connects very nicely with constructors because we had overloaded constructors which were different constructors with different parameters but they all existed. The same goes for methods and in this case we have two methods which means we have overloaded methods. The Cat class will have both in this case!

So now you have the basic breakdown of inheritance. But when do you use it? You use inheritance when two objects have a IS-A relationship. That is when some object is something else. For example a cat IS-A animal (I know the grammar isn't quite right) and a car IS-A vehicle. You don't use inheritance if the IS-A relationship doesn't work like a bear IS-A mule or a beer IS-A hotdog. Just doesn't work, they may have some of the same attributes like the bear and mule both being animals and the beer and hotdog both being something we consume but one does not stem from the other in the family tree. A beer may have something like this: Food to beverage to alchohol to beer. The hotdog may have some similarities but it does not come after beer in the family tree: Food to snack to hotdogs.

Summary and extra tidbits:
-inheritance is when one class inherits the attributes (methods, instance variables) from another class called the superclass that relates to it by a IS-A relationship
-the class that inherits these attributes is called the subclass
-methods can be overridden(another method in the subclass with the same name and parameters) and overloaded(same name different parameters)
-subclasses will have the overloaded methods and the original methods from the superclass
-inheritance can happen in many steps: Graeme can inherit from boys which can inherit from people which can inherit from ... and so on and so on.
-you cannot change public to private when overriding but you can when overloading (we'll talk about private in a later post).


I think that covered a lot. There is more, there always seems to be more with Java. But once again I'm tired. If you have any questions or you need anything cleared up comment and I'll see what I can do for ya.