Let’s examine the following example of inheritance in OOP (Java in particular):

First class


1.  public class Pere 
2.  {
3.	public void m1()
4.	{
5.	    System.out.println("Pere.m1");
6.	    m2();
7.	}
8.	
9.	protected void m2()
10.	{
11.	    System.out.println("Pere.m2");
12.	}	
13.  }

Second class


1.  public class Fils extends Pere
2.  {
3.	public void m1()
4.	{
5.	    super.m1();
6.	    System.out.println("Fils.m1");
7.	    m2();
8.	}
9.	
10.	protected void m2()
11.	{
12.	    System.out.println("Fils.m2");
13.	}
14.  }

And the programme


1.  public class TestHeritage
2.  {
3.	public static void main(String[] args)
4.	{
5.	    Pere p = new Fils();
6.	    p.m1();
7.	}
8.  }

The result of the execution is:


1.	Pere.m1
2.	Fils.m2
3.	Fils.m1
4.	Fils.m2

At the line 5 of TestHeritage, we use a Pere reference but create a Fils instance,
then at the line 6, we call the method m1() of the instance (the Fils)

The method m1() of Fils first call the method m1() of Pere (method that it redefined),
then it print “Fils.m1” and call its m2() method (who print “Fils.m2”)

The interresting part is the call to the method m1() of Pere (line 5 of Fils).


	super.m1();

We know that the m1 method of Pere print “Pere.m1” and then call the method m2 of Pere
(who print “Pere.m2”). But at the result, we see that the method m2() of Fils is called
twice, once in the method m1() of Pere (super.m1()) and once in the method m1() of Fils
and the method m2() of Pere is not call.

This is because the object we use to call methods is an instance of Fils, and Fils has
redefined all methods of Pere (m1 and m2)

Using this feature, we can avoid doing one thing twice (if we want to call Pere.m2(),
just do it in the method of Fils like that super.m2() ).

For example, suppose that the method Pere.m2() parse a XML file and extract some infos
from the file, the method Fils.m2() parse the same XML file but extract some others infos.
When we use Fils, we may not need the infos extracted by Pere.m2() but only those of Fils.m2().
But we do need the work of Pere.m1(). So in the method Fils.m1(), we could simply put


    public void m1()
    {
	super.m1();
    }

The programme will:
– call Pere.m1() to do the work we need
– but instead of calling Pere.m2() (that we don’t need), it will call Fils.m2() to do
exactly what we need.

Another example:
Suppose you have the method Pere.m1() as following


    public class Pere 
    {	
	public void m1()
	{
	    m2();
	    m3();
	    m4();
	    m5();
	    m6();
	}
		
	protected void m3() 
	{
	    // do something very heavy
	}
		
	private void m6() {}
    }

You need the result of Pere.m1() with some modification: not include the result of
Pere.m3().
Suppose that m3() is a very heavy method that you don’t need in the Fils, and
you cannot call super.m6() from Fils because the method is private. You can write
Fils as following


    public class Fils extends Pere 
    {	
	public void m1()
	{
	    super.m1();
	}
		
	protected void m3()
	{
	    // do nothing
	}
    }

If the method Fils.m1() does nothing else, you can eliminate it from your code


    public class Fils extends Pere 
    {	
	protected void m3()
	{
	    // do nothing
	}
    }
Advertisements