clicking on this will visually collapse the text block directly below it

OO Programming Tutorial - Part 3, Inheritance, November 2002

Up to this point we have been dealing with very simple objects but the world of programming is a touch more complicated than that. Our Animal object although quasi useful doesn't cover the gamut of creatures that we need to cover. We need to create some subclasses to handle specific situations BUT our Animal class will remain useful as our superclass......uh oh, there he goes again with the goofy terms! OK, OK lets create one new class before I explain, I think it will help in the explanation. Here is our new Dog class that extends our Animal class (is the child of it). As you can see the class is declared as extends our Animal class. This means several things to you as the programmer but first let's look at this in simple terms. A dog Is An animal. This relationship is key to object oriented inheritance. When designing classes and extending you should always ask yourself, "Is this class a xxxx" If the answer is no you probably don't want to inherit from it. Also remember the fact that the Dog is the subclass (child) of Animal and likewise the Animal is the superclass (parent). So our dog is definitely an animal so what?

OK, here's where it gets fun! Because we have extended our Animal class we inherit all of its functionality without having to re-write all the code! What's that you say? Yes its just like having another complete set of the variables and methods in our Dog class, pretty neat huh? If you notice in the second constructor of Dog the first line (and it MUST be the first line) we call the constructor of the superclass, passing our variables to it. Once this is done we have access to them just as we would if they were in our Dog class. Now I know what your thinking here, "Why go through all this, why not just do everything in the Animal class?" By doing this we inherit the base of what we need from the superclass and are able to add more specific items to our subclasses. so if we create an instance of Dog as such:

Dog myDog = new Dog(24, 40, 7, true, "worker", "fido");

and then call this method: myDog.eat(); the JVM first looks in the subclass for the method, if it doesn't find it it then looks to the superclass and our dog gets to eat! Now you will also notice that we do have a method in our Dog class that is also in the superclass. This is called method overriding, which we will get into in a bit. For now just realize that when myDog.sleep(4); is called the method in the subclass is called NOT the superclass! One last thing to take note of here is the fact that the private variables are NOT directly accessible to us in the subclass. If you needed the weight of your new Dog you CANNOT call myDog.height, NOPE, it won't work because even though we are extending from animal we are still attempting to access a private variable from outside of the class. Inheriting does not override access modifiers! The correct way to get our dog's weight is int weight = myDog.getWeight();

So now we've touched upon inheritance, but there is more...namely Multiple Inheritance. Boy I can hear the java programmers screaming right now....WHAT??? OK, hold your britches. Let's say you want the functionality of multiple classes, how do we do that? Extend from them both? I don't think so! Java limits multiple inheritance somewhat due to the fact that it can create a massive amount of confusion and problems but there is a way. You may only extend from one class but you may implement many, many more. It works almost the same as extending. Here's how it would look in your class declaration: public class Dog extends Animal implements MeatEater, Canine. Now you can use all the functionality of all these classes.

Constructors Revisited

Before we venture on I promised we would touch back on constructors and I am a man of my word. Constructors, as we mentioned earlier are the workhorse of instantiation. In java you do NOT need to explicitly code a constructor. If you do not the compiler will create a default one (no params) for you....BUT, if you do code one this will no longer happen! In other words if I made a constructor such as my second one In Dog that accepts six parameters and failed to create the default no param constructor I could have problems. So remember, when coding any constructor of your own ALWAYS drop that no param one in!

The other thing to mention is constructor overloading. You can have as many constructors as you want as long as the signature (parameters) are different for each one. This means your constructor can be like this Dog(int num), Dog(int num, String thing) or Dog(String thing, String stuff, String moreStuff). When a specific constructor is called during instantiation the JVM will look for the one that matches the call BUT you cannot do this: Dog(int num1, String thing2), Dog(int num4, String thing3).......the JVM doesn't look at the variable names, it only looks at the type of the parameters so to it these constructors are IDENTICAL....the compiler wont let you get away with that!

One final thing I have to mention because I see so many beginners getting confused on is the order constructors are called in when extending classes. Lets say we have a class A that extends B, furthermore our class B extends class C. When an instance of A is created the first thing the JVM does is go to the superclass of A, which is B, since B extends C the JVM then goes up another level to the constructor of C. If in our constructors we had a line of code that said System.out.print(x); with x being the class name and we create an instance of A we would see C B A printed out to the screen. Try it for yourself with this code and make sure you understand this or your hard worked code could do some strange things when you run it. With that said let's move on to a topic that really whacks most new programmers out for quite a while.....Polymorphism

Part Four - Polymorphism

Customer Comments

clicking on this will visually collapse the text block directly below it
From the start they are very helpful, open to your ideas and extremely creative. I know I made the right choice.
Chris Marksbury, ChrisMarksbury.com | More Testimonials >>

Latest Articles / Tutorials

clicking on this will visually collapse the text block directly below it |
Our RSS Article Feed Available as RSS Feed!

Check here often for articles & tutorials that will help make your business grow!

| More Articles & Tutorials >>

Partner Comp. & Friends

clicking on this will visually collapse the text block directly below it