CodingBison

Inheritance is one of the key concepts of object oriented programming. Inheritance defines the hierarchical relationships between various classes. A simple way to understand is that inheritance is how we inherit things/characteristics from our parents like money, color of eyes, etc. Here is a simple illustration of a child inheriting money from the parent and then passing on to his own child.



Figure: We Inherit From Our Parents

With inheritance, a generic class (aka super-class) can be defined with a set of generic properties. This generic class can then be inherited by any other class (sub-class). Sub-class can define its own specific properties, along with the ones that are inherited from its super-class. Thus a sub-class can pick and choose from its parent (or ancestor) class and also define its own.

Let us understand this more using the Figure provided below. The figure below shows a super-class (su1) that is being inherited by two different sub-classes (sb1 and sb2).



Figure: Inheritance in Java

If needed, a sub-class can become a parent itself. This is called multilevel inheritance. As shown in the above figure, Java allows inheritance at multiple levels. For example, we can have a sub- class sb3 inherit the properties of sb2, which in turn inherit the properties of sb1 and su1.



Figure: Multilevel Inheritance in Java

There is also yet another concept called multiple inheritance. In this method, a sub-class inherits properties from more than one super-classes. While multiple inheritance can be useful in some cases, Java does not support it because of the complexity it brings in. In order to gain the advantages of multiple inheritance, Java supports a concept called "Interface", we will see more about "Interface" in the coming sections. The following figure shows the basic elements of multiple inheritance -- once again, this is not supported in Java.



Figure: Multiple-Inheritance is NOT supported in Java

Inheritance in Java

Now that we have understood the concept of inheritance, let us see how it is achieved in Java. The keyword "Extends" is used by a sub-class to inherit the properties from a super-class.



Figure: Using Inheritance

As show in the above figure, a super-class "movie" is defined with a common set of properties that are applicable to any movie. Now, that we have one super-class with common properties, 2 sub-classes (jamesBondMovies and harryPotterMovies) are defined from the same super-class. Each of the sub-classes will inherit the common properties from its super-class and also have their own set of specialized properties.

If you are wondering what is the need for this hierarchy of classes, why not define all of them in one class and get rid of this complexity that comes with inheritance. Notice the special properties defined in each of the sub-classes "bondActor, bondGirl or mRoleCharacterName" none of them makes sense to any other type of movies except "James bond movies". Similarly, with "harryPotterRole or dumberdoorRole" will only make sense for "Harry potter movies". Let us now see how the code looks like, if we were to achieve the same thing with out using the concept of inheritance.



Figure: Inheritance Example

Like shown in the above figure, inheritance helps in reducing the complexity, clear isolation between generic and specialized properties and helps in reducing memory wastage.

Here is a sample program that demonstrates the concept of "Inheritance".

 class movie {
     String name;
     int    length;
     String director;
     /**
      * Default constructor.
      */
     movie() {
         this.name = "";
         this.length = 0;
         this.director = "";	
     }
     /**
      * @param name
      * @param length
      * @param director
      */
     movie(String name, int length, String director) {
         this.name = name;
         this.length = length;
         this.director = director;
     }
     /**
      * @return the name
      */
     public String getName() {
         return name;
     }
     /**
      * @param name the name to set
      */
     public void setName(String name) {
         this.name = name;
     }
     /**
      * @return the length
      */
     public int getLength() {
         return length;
     }
     /**
      * @param length the length to set
      */
     public void setLength(int length) {
         this.length = length;
     }
     /**
      * @return the director
      */
     public String getDirector() {
         return director;
     }
     /**
      * @param director the director to set
      */
     public void setDirector(String director) {
         this.director = director;
     }
 }

 class jamesBondMovies extends movie {
     String bondActor;
     String bondGirl;
     String mRoleCharacterName;
     /**
      * Default constructor.
      */
     jamesBondMovies() {
         super();
         this.bondActor = "";
         this.bondGirl = "";
         this.mRoleCharacterName = "";
     }
     jamesBondMovies(String name, int length, String director, String bondActor, 
             String bondGirl, String mRoleCharacterName) {
         /*
          * This is similar to calling the super-class constructor
          * "movie(String name, int length, String director)".
          * We will see more about "super" in the next section. 
          */
         super(name, length, director);

         this.bondActor = bondActor;    
         this.bondGirl = bondGirl;
         this.mRoleCharacterName = mRoleCharacterName;
     }

     /**
      * @return the bondActor
      */
     public String getBondActor() {
         return bondActor;
     }

     /**
      * @param bondActor the bondActor to set
      */
     public void setBondActor(String bondActor) {
         this.bondActor = bondActor;
     }

     /**
      * @return the bondGirl
      */
     public String getBondGirl() {
         return bondGirl;
     }

     /**
      * @param bondGirl the bondGirl to set
      */
     public void setBondGirl(String bondGirl) {
         this.bondGirl = bondGirl;
     }

     /**
      * @return the mRoleName
      */
     public String getMRoleName() {
         return mRoleCharacterName;
     }

     /**
      * @param mRoleName the mRoleName to set
      */
     public void setMRoleName(String mRoleName) {
         mRoleCharacterName = mRoleName;
     }
 }

 class harryPotterMovies extends movie {
     String harryPotterRole;
     String dumbeldoreRole;
     /**
      * Default constructor.
      */
     harryPotterMovies() {
         super();
         this.harryPotterRole = "";
         this.dumbeldoreRole = "";
     }
     harryPotterMovies(String name, int length, String director, 
                String harryPotterRole, String dumbeldoreRole) {
         super(name, length, director);
         this.harryPotterRole = harryPotterRole;
         this.dumbeldoreRole = dumbeldoreRole;		
     }

     /**
      * @return the harryPotterRole
      */
     public String getHarryPotterRole() {
         return harryPotterRole;
     }

     /**
      * @param harryPotterRole the harryPotterRole to set
      */
     public void setHarryPotterRole(String harryPotterRole) {
         this.harryPotterRole = harryPotterRole;
     }

     /**
      * @return the dumbeldoreRole
      */
     public String getDumbeldoreRole() {
         return dumbeldoreRole;
     }

     /**
      * @param dumbeldoreRole the dumbeldoreRole to set
      */
     public void setDumbeldoreRole(String dumbeldoreRole) {
         this.dumbeldoreRole = dumbeldoreRole;
     }
 }

 class InheritanceExample {
      public static void main(String[] args) {
     jamesBondMovies jbondObj1 = new jamesBondMovies();
     jamesBondMovies jbondObj2 = new jamesBondMovies("Casino Royale", 
                 144,"Martin Campbell", "Daniel Craig","Eva Green","Judi Dench");
     harryPotterMovies harryObj1 = new harryPotterMovies();
     harryPotterMovies harryObj2 = new harryPotterMovies(
                 "Harry potter and the Prisoner of Azkaban", 141, "Alfonso Cuaron",
                 "Daniel Radcliffe", "Sir Michael Gambon");	
     jbondObj1.setName("Die Another Day");
     jbondObj1.setDirector("Lee Tamahori");
     jbondObj1.setLength(133);
     jbondObj1.setBondActor("Pierce Brosnan");
     jbondObj1.setBondGirl("Halle Berry");
     jbondObj1.setMRoleName("Judi Dench");

     harryObj1.setName("Harry potter and the Philosopher's stone");
     harryObj1.setDirector("Chris Columbus");
     harryObj1.setLength(152);
     harryObj1.setHarryPotterRole("Daniel Radcliffe");
     harryObj1.setDumbeldoreRole("Richard Harris");

     System.out.println(" James Bond Movie: " + jbondObj1.getName() + "\n\tDirector: " + 
            jbondObj1.getDirector() + " \n\tLength in Min: " + jbondObj1.getLength()
            + "\n\tBond Character played by: " + jbondObj1.getBondActor() + 
            "\n\tBond Girl Played by: " + jbondObj1.getBondGirl() + "\n\tM role played by: " +
            jbondObj1.getMRoleName());

     System.out.println("\n James Bond Movie: " + jbondObj2.getName() + "\n\tDirector: " + 
            jbondObj2.getDirector() + "\n\tLength in Min: " + jbondObj2.getLength()
            + "\n\tBond Character played by: " + jbondObj2.getBondActor() + 
            "\n\tBond Girl Played by: " + jbondObj2.getBondGirl() + "\n\tM role played by: " +
            jbondObj2.getMRoleName());

     System.out.println ("\n Harry Potter Movie: " + harryObj1.getName() +  
            "\n\tDirector: " + harryObj1.getDirector() + "\n\tLength in Min: " +
            harryObj1.getLength() + "\n\tHarry Potter Character played by: " +
            harryObj1.getHarryPotterRole() + "\n\Albus Dumbledore Character played by: "
            + harryObj1.getDumbeldoreRole());

     System.out.println ("\n Harry Potter Movie: " + harryObj2.getName() +  
            "\n\tDirector: " + harryObj2.getDirector() + "\n\tLength in Min: " +
            harryObj2.getLength() + "\n\tHarry Potter Character played by: " +
            harryObj2.getHarryPotterRole() + "\n\Albus Dumbledore Character played by: "
            + harryObj2.getDumbeldoreRole());
     }
 }

Compile/Run this program and here is the output:

  James Bond Movie: Die Another Day
 	Director: Lee Tamahori 
 	Length in Min: 133
 	Bond Character played by: Pierce Brosnan
 	Bond Girl Played by: Halle Berry
 	M role played by: Judi Dench

  James Bond Movie: Casino Royale
 	Director: Martin Campbell
 	Lento in Min: 144
 	Bond Character played by: Daniel Craig
 	Bond Girl Played by: Eva Green
 	M role played by: Judi Dench

  Harry Potter Movie: Harry potter and the Philosopher's stone
 	Director: Chris Columbus
 	Length in Min: 152
 	Harry Potter Character played by: Daniel Radcliffe
 	Albus Dumbledore Character played by: Richard Harris

  Harry Potter Movie: Harry potter and the Prisoner of Azkaban
 	Director: Alfonso Cuaron
 	Length in Min: 141
 	Harry Potter Character played by: Daniel Radcliffe
 	Albus Dumbledore Character played by: Sir Michael Gambon




comments powered by Disqus