Broad Network


Overriding in Java Subclasses

Java Object Oriented Programming Core – Part 11

Forward: In this part of the series I explain how you can override a property or method of a subclass in Java. I also explain the effects.

By: Chrysanthus Date Published: 3 Sep 2012

Introduction

This is part 11 of my series, Java Object Oriented Programming Core. In this part of the series I explain how you can override a property or method of a subclass in Java. I also explain the effects.

Overriding a Property
To override a property in a subclass declaration, just type the same property variable name with the same data type, but change the value. The following program illustrates this:

class Illustration
    {
        int prop = 33;
    }

class SubIllust extends Illustration
    {
        int prop = 44;
    }

class OverrideEx
    {
        public static void main(String[] args)
            {
                Illustration objIlust = new Illustration();
                int var1 = objIlust.prop;
                System.out.println(var1);

                SubIllust objSub = new SubIllust();
                int var2 = objSub.prop;
                System.out.println(var2);
            }
    }

Read and try the code (program); also note the output. A property is overridden in a subclass. When that is done, the original property remains valid in the superclass and the new property becomes valid in the subclass. The overridden property is the original property in the superclass. In the above code, instantiation of the superclass has the original property, while instantiation of the subclass has the new property. The original property is called the overridden property, while the new property is called the overriding property.

Unfortunately Java does not recommend that you do overriding of properties. What Java recommends you to do when necessary, is overriding of methods.

Overiding a Method
To overide a method, just use the same method signature without the ending semicolon, in the subclass declaration; then you type whatever body for the method that you want. After this is done, instantiation of the superclass results in the overridden method, while instantiation of the subclass results in the overriding method. Read and try the following code that illustrates this:

class Illustration
    {
        void show(String str, boolean bool)
            {
                if (bool==true)
                    {
                        System.out.println(str);
                    }
            }
    }

class SubIllust extends Illustration
    {
        void show(String str, boolean bool)
            {
                System.out.println(str);
            }
    }

class OverrideEx2
    {
        public static void main(String[] args)
            {
                Illustration objIlust = new Illustration();
                String str1 = "I am super.";
                objIlust.show(str1, true);

                SubIllust objSub = new SubIllust();
                String str2 = "I do not need any boolean authority to show myself!";
                objSub.show(str2, false);
            }
    }

Note the output of the program that confirms the main overriding principle.

Other Things to know about Overriding
- When a member is overridden it becomes hidden to the subclass.
- Do not confuse between modifiers and method return types. Method return types are the same as data types. Modifiers are private, protected and public, static, etc. Actually, return types are a subset of modifiers. Now, the overriding method can have more access than the overridden method: this means that, if an overridden method is private, the overriding method can be protected or public (or still private), and if the overridden method is protected, the overriding method can be public (or still protected, but not private).
- the overridden method can also return a class (reference); it does not matter whether the class is a superclass or a subclass.

That is it for this part of the series. We stop here and continue in the next part.

Chrys
NEXT

Related Articles

Java Course

Comments

Become the Writer's Fan
Send the Writer a Message