Learning Java/Inheritance

Inheritance is rather simple. In this lesson you will learn how to extend classes and why this is useful

Usefulness

edit

Inheritance is useful way to reuse code when classes share an is-a relationship. More specifically, if two or more classes share common behaviors because one is a more general type of the other, this suggests that the more specific type can "inherit" those behaviors from the more general type.

For example, a frog is a specific kind of animal, which means all behaviors associated with animals are also associated with frogs. So, if we define the behavior eat() on the Animal class, by having our Frog class inherit from Animal, it automatically gets the behavior. Similarly, if we define other types of animals like Dog, it also inherits that behavior without having to explicitly define it. Best of all, if we decide to change the code contained in the eat() method, all animals get the change. There's no need to update each and every one—the code that represents eating is localized in one place in our codebase.

You will become very familiar with inheritance as you use the Java standard library, as it makes heavy use of this core principle of object-oriented programming.

Extending

edit

Subclasses and super classes are made from extending. Let's pretend we have 2 classes - WaterVehicle and Ship. Which is more general? Or else, which is a type of the other. The answer is a ship is a type of water vehicle.

The class WaterVehicle could have some methods that the class Ship could use. Instead of copying and pasting, you could extend it.

When you extend another class, you gain all of its methods and fields in that class. For example, if WaterVehicle had an int called speed, you would not have to re-declare it when writing Ship. It would be automatically there.

To extend a class, simply type:

class UseExtend extends OtherClass

In this case, UseExtend would become a subclass of OtherClass, and OtherClass a super class of UseExtend.

Here is an example of a simple program that writes integers:

public class WritesIntegers
{
   int writeMe;

   public WritesIntegers(int write)
   {
      writeMe=write;
   }

   public void writeTheInteger()
   {
      System.out.println(writeMe+"");
   }
}

Here is a class which writes integers AND strings. Instead of rewriting...

public class WritesStringsAndIntegers extends WritesIntegers
{
   String printMe;

   public WritesStringsAndIntegers(int write, String print)
   {
      super(write);
      printMe=print;
   }

   public void printTheString()
   {
      System.out.println(printMe);
   }
}

Notice that you do not need to declare int write. Also note the super(write). Essentially, this calls the super classes (in this case, WritesIntegers) constructor. The parameter, and int, is the same as the super classes. If you made an object of this class, you could call both printTheString and writeTheInt.

However, you can only extend one class. You will learn about interfaces in this lesson before learning about implementing...

More...

edit

You can, of course, extend your own classes in the same way. As long as they are in the same directory, just follow the regular way. You will get all the methods and variables from that class into your class. You could also use objects, so you need to decide which one is right for your code.





You are ready to move on! Java Error Handling II.
Back to Java Main Page