OOP and inheritance

We have seen how we can use other people's hard work by instantiating/creating objects from the classes of an API like Android. But this whole OOP thing goes even further than that.

What if there is a class that has loads of useful functionality in it but not quite what we want? We can inherit from the class and then further refine or add to how it works and what it does.

You might be surprised to hear that we have done this already. In fact, we have done this with every single app we have created. I mentioned this near the start of the chapter. When we use the extends keyword we are inheriting. Here is the code from the previous mini-app:

public class MainActivity extends Activity ...

Here we are inheriting the Activity class along with all its functionality—or more specifically, all the functionality that the class designers want us to have access to. Here are some of the things we can do to classes we have extended.

We can even override a method and still rely in part on the overridden method in the class we inherit from. For example, we overrode the onCreate method every time we extended the Activity class. But we also called on the default implementation provided by the class designers when we did this.

super.onCreate(... 

We discuss inheritance mainly so that we understand what is going on around us and as the first step towards being able to eventually design useful classes that we or others can extend.

Let's look at some example classes and see how we can extend them, just to see the syntax and as a first step, and also to be able to say we have done it.

When we look at the final major topic of this chapter, polymorphism, we will also dig a little deeper into the topic of inheritance at the same time. Here is some code using inheritance.

This code would go in a file named Animal.java.

public class Animal{

   // Some member variables
   public int age;
   public int weight;
   public String type;
   public int hungerLevel;

   public void eat(){
      hungerLevel--;
   }

   public void walk(){
      hungerLevel++;
   }


}

Then in a separate file named Elephant.java, we could do this:

public class Elephant extends Animal{
      
   public Elephant(int age, int weight){
      this.age = 57;
      this.weight = 1000;
      this.type = "Elephant";
      int hungerLevel = 0;
   }

}

We can see in the previous code that we have implemented a class called Animal and it has four member variables: age, weight, type, and hungerLevel. It also has two methods eat and walk.

We then extended Animal with Elephant. An Elephant can now do anything an Animal can and it also has an instance of all its variables. We initialized the variables from Animal which Elephant also has in the Elephant constructor. Two variables (age and weight) are passed into the constructor when an Elephant object is created and two variables (type and hungerLevel) are assigned the same for all Elephant objects.

We could go ahead and write a bunch of other classes that extend Animal, perhaps, Lion, Tiger, and ThreeToedSloth. Each would have an age, weight, type, and hungerLevel and each would be able to walk and eat.

As if OOP where not useful enough already, we can now model real-world objects. We have also seen we can make OOP even more useful by sub-classing/extending/inheriting from other classes.

The terminology we might like to learn here is that the class which is extended from is the superclass and the class which inherits from the superclass is the subclass. We can also say parent and child class.

Tip

As usual, we might find ourselves asking this question about inheritance. Why? The reason is something like this: We can write common code once, in the parent class, we can update that common code and all classes that inherit from it are also updated. Furthermore, a subclass only gets to use public/protected instance variables and methods. So, designed properly this also, further enhances the goals of encapsulation.

Let's write another mini-app to demonstrate inheritance then we will take a closer look at the final major OOP concept. We will then be in a position to start the next game.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.152.144