Learning Java: Day 7

Inheritance and Polymorphism

After two days of being too busy to work on java, I'm glad to finally be back. I begin to feel antsy if I'm away from the computer for too long.

Chapter 7 of Heads Up Java teaches inheritance and polymorphism concepts.

Inheritance should be thought of as a top down thing: children inherit from parents, not the other way around. Inheriting code from the superclass above it should have an "is a" relationship. Example (Cup is the superclass, CoffeCup is the subclass of Cup, and EspressoCup is the subclass of CoffeCup) ExpressoCup is a CoffeCup AND EspressoCup is a Cup.

Inheritance means you avoid duplicate code by having all the common code in one place and letting the subclasses inherit the code from the superclass.  If you need to change behavior you just have to make the change in the superclass, then all the subclasses see the changes too. This makes it so you don't break the code further down the line when you have to make modifications.

A subclass inherits all public instance variable and methods of the superclass, but does not inherit the private instance variables and methods of the superclass.

Polymorphism is used in making arrays. The superclass is the array type, the subclasses fill the array, and the superclass method is used on each member of the array.
Example:

MarineLife [] marine = new MarineLife [5];
marine [0] = new Fish();
marine [1] = new Squid();
marine [2] = new Crab();
marine [3] = new Shark();
marine [4] = new MantaRay();

for (int i = 0; i < marine.length; i++) {
     marine[i].eat();  // when 'i' is 0, a Fish is at index 0 in the array, so you get the Fish's eat() method. When 'i' is 4, you get the MantaRay's eat() method.

     marine[i].hide(); // same with the hide() method.

You can then use the polymorphic arguments and return types.
Example:
dave.approach(lionFish); 

class Diver {
     public void approach(MarineLife m) {
          // try to get close to MarineLife at the other end of the 'm' parameter
          m.curious();
          }
}
The 'm' parameter can take ANY marineLife type as the argument, and when the Diver approaches it tells the MarineLife to act curious.  Whichever marineLife is called, that's whose curious() method will run.

class Photographer {
 dave.approach(whaleShark); 
     public void takePhotos() {
          Diver dave = new Diver();
          Shark whaleShark = new Shark();
          Fish lionFish = new Fish();
          dave.approach(whaleShark);
          dave.approach(lionFish);
     }
}
The Diver's approach() method can take any marineLife you give it. As long as the object you pass in as the argument is a subclass of marineLife, it will work.

You can override a method from the superclass (the subclass Shark has a different method for 'curious' than the subclass Squid)

You can overload a method from the superclass (the subclass Crab adds more stuff to the superclass method for 'eat')

The chapter ends with exercises and puzzles.

0 comments:

Post a Comment