Inheritance and Access Modifiers

As I mentioned in my previous post on Polymorphism, I’m not as knowledgeable on the four major features of Object Oriented Programming, known concisely as APIE. I recently had a rather foolish mistake of tying Inheritance in with Polymorphism, which may be a bit easy to do since Polymorphism does take place as a result of Inheritance, but I won’t make excuses for myself. Today we’ll talk a bit about inheritance and the impact that access modifiers make on it. (Spoiler Alert: You’ll probably see blog posts on Abstraction and Encapsulation soon just to round the four principles out!)

Inheritance is a principle in which an object acquires, or inherits, all of the properties and methods of a parent object. Why is this so important, you may ask? Well, it allows you to make new classes which are built on top of pre-existing classes; you can reuse the methods and fields of that parent class. Not only that, but you can add new methods and fields to your children objects. TLDR; inheritance can be read as a ‘is-a’ relationship. For example, a Jeep ‘is-a’ Car, a Student ‘is-a’ Human, a Dog ‘is-a’ Animal, etc.

There is something important that you have to consider when using inheritance in your projects; the access modifiers applied to your fields and methods in your parent class. True to their name, access modifiers effect how accessible the field, class or method is to other objects and classes, and the program itself. So how do the access modifiers effect inheritance? Actually, the child class will inherit all the methods and fields of it’s parent class except one; any that are declared private.

Let’s check out a quick code example for this concept. Here I have a class named Musical. It has three methods to represent properties of a musical. A public method, songs(), a protected method cast() and a private method story().

    class Musical{  
        public void songs() {
            System.out.println("La la la...");
        } 
        protected void cast() {
            System.out.println("Characters");
        } 
        private void story() {
            System.out.println("Story");
        }
    }

Next I’ll have a child class of Musical; Heathers. Notice I have gone ahead and used polymorphism to override the original definition for the method cast.

class Heathers extends Musical{  
        public void cast() {
            System.out.println("Veronica, Heather, J.D.");
        }
    }

Now let’s try and call on these methods–I’ll call on each of them within the new instance of the Heathers class and…wait a minute. My IDE is giving me an error.

What in the frick is that?!
….Ohhhhhh.

The IDE knows that story(), being a private method couldn’t be inherited from Musical to Heathers. As soon as I remove the invocation for story(), the error goes away because both the protected method cast() and the public method songs() is visible and therefore inheritable from Musical down to Heathers.

In a nutshell, that’s inheritance and how the access modifiers impact it. Access the full code used as an example in this blog here.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.