Object Oriented Programming In Java – Inheritance

Object oriented programming in java is very powerful and a clear understanding is important. While I was trying to dig deeper, I realized that there is more to it than I actually knew. So, I looked around the web and spent some time on StackOverflow! Now let us create a simple file here Base.java

public class Base{
   public static void main(String[] args]){
   //do some cool stuff here
   }
}

You will find out that most software developers will favor composition over inheritance and that is totally fine. I agree with that. It is however important to understand what inheritance is about. Now let us pick a clear example or analogy that will not throw people off guard! I am thinking of an animal class. Let us use it.

Animal.java
What we are alluding to here is the fact that most animals have certain characteristics and behaviors in common. Most animals can move and eat. We can consider the above mentioned behaviors as methods. What else come to mind? I am thinking about the “state” of being either “alive” or “dead”. Now let us create our class.
Animal.java

public class Animal{
   //public methods of the Animal class
   public void move(){
      System.out.println("I can actually move faster than you");
   }

   public void eat(){
      System.out.println("I eat all kinds of food");
   }
}

Now we can create our classes that will inherit non-private members of the Animal class. I am sticking with the two methods to make this simpler. Think of an animal like a cat. Let us also consider our other friend dog for now!

public class Dog extends Animal{
   private String name;
   private int age;
   private String color;

   //let us now define a constructor for our dog class
   public Dog(String name, int age, String color){
      this.name = name;
      this.age = age;
      this.color = color;
   }

   //add getter and setter methods here for private members

}

Now our cat class goes here with its properties:
Cat.java

public class Cat extends Animal{
   private String color;
   private boolean bites;

   //our constructor goes here
   public Cat(String color, boolean bites){
      this.color = color;
      this.bites = bites;
   }

   //add getter and setter methods here for private members
}

Perhaps the easiest way to explain this concept is by saying that both animal types (in this case; dog and cat) can use both move and eat methods even though they have not defined their own. This is possible because we use the extends keyword to enable inheritance. Now consider this inside our Base.java class:

public class Base{
   public static void main(String args[]){
      //instantiate our Dog here
      Dog dog = new Dog("Spot", 10, "Golden-yellow");
      Cat cat = new Cat("black", false);

     //now let us call the inherited methods on our
     //brand new dog and cat!
     dog.move(); //results: I can actually move faster than you
     cat.eat(); //results: I eat all kinds of food
   }
}

The coolest thing that you can do is override the inherited methods by defining special methods(with same names) inside the subclasses (Dog and Cat). You can then specify what the dog eats or what the cat eats. Example:

public class Cat extends Animal{
   //private members here

   //constructor here

   //now override the inherited methods here
   @override
   public void eat(){
       System.out.println("I only eat cat food!");
   }

   @override
   public void move(){
      System.out.println("I move slowly when sneaking up on you!");
   }
}

You can repeat the same overriding inside the Dog class and specify the behaviors respectively.

Now inside our Base.java we can call our Cat’s methods and they should do more specific things.

public class Base{
   public static void main(String args[]){
      Cat kitty = new Cat("white", true);

      //now call the method
      kitty.eat(); // results: I only eat cat food
      kitty.move() // results: I move slowly when sneaking up on you!

      //try this code while you are at it and see what happens:
      System.out.println(kitty instanceof Cat);
      System.out.println(kitty instanceof Animal);
   }
}

When it comes to private members inside the superclass, subclasses do not inherit them. That being said however, using public or protected getter/setter methods inside the superclass, the subclass can access the private members.

Perhaps one other important question to ask yourself is this: based on the fact that private members are not inherited by subclasses, where do they go? How would you answer the question of whether they are inherited or not during an interview and why? The obvious answer is NO. They don’t get inherited. What if your interviewer tells you they get inherited? There is more to this than I can describe here. Try looking around for more information and you are welcome to share them with me.

Thank you and hope this helped you learn something about object oriented programming in java. See you soon and take care. Have a great weekend!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">