Playing with inheritance and polymorphism in Java

Published:

Add / read comments

In previous article we were demonstrating polymorphism based on different parameters count or type (method overloading) which is not possible in PHP. Another attitude is runtime polymorphism essentially referred as method overriding. Method overriding is possible in PHP.

I put all examples under package start. For testing method overriding in Java I wrote following class:

/**
 * Parent class 
 */
class TestParent {
 
  public void talk ( ) 
  {
    System.out.println("Parent is talking");
  }
}
 
 
/**
 * Access modifier Final means class can not be extended 
 */
final class TestChild extends TestParent {
 
  public void talk ( ) 
  {
    System.out.println("Child is talking");
  }
 
}

So Java class is ready. We only need runable program, which is using (calling) Java class methods. For this purpose I wrote following program:

/**
 * @file Overriding.java
 */
public final class Overriding {
 
  /**
   * @param	Array	args
   */
  public static void main(String[] args) 
  {
    // Parent reference and object
    TestParent p = new TestParent();
 
    // Parent reference but Child object
    TestParent c = new TestChild(); 
 
    p.talk(); // runs the method in Parent class
 
    c.talk(); //Runs the method in Child class		
  }
 
}

Program is within same package as Java class TestParent, so we have direct access to it from program Overriding. So now if I run Java program as Java application in Eclipse IDE, I will get the following result into the console:

Parent is talking
Child is talking 

Method talk() in class TestParent was overriden by method talk() in extending subclass TestChild. If we want to call parent's method execution from child method, it can be done simply by using word super:

final class TestChild extends TestParent {
 
  public void talk ( ) 
  {
    // execute parent method
    super.talk();
 
    System.out.println("Child is talking");
  }
}

So now if I run Java program, I will get the following result into the console:

Parent is talking
Parent is talking
Child is talking 

In class Overriding I was refferencing to object parent. Let's try to modify class TestChild by adding another method.

final class TestChild extends TestParent {
 
  /**
   * @access public
   * @param void
   * @return void
   */
  public void cry ( )
  {
    System.out.println("Child is crying");
  }
}

So now if I try to call method cry():

p.cry(); 

it will throw a compile time error since p's reference type TestParent doesn't have a method by the name of cry(). That's why I am trying to as much as possible to reference to the same object's instance in my codes. Then I am sure that called method is present in object reference.

/**
 * @file Overriding.java
 */
public final class Overriding {
 
  /**
   * @param	Array	args
   */
  public static void main(String[] args) 
  {
    // Parent reference and object
    TestParent p = new TestParent();
 
    // Child reference and Child object
    TestChild c = new TestChild(); 
 
    // runs the method in Parent class
    p.talk(); 
 
    //Runs the methods in Child class
    c.talk(); 
    c.cry();
  }
}

So now if I run Java program, I will get the following result into the console:

Parent is talking
Parent is talking
Child is talking
Child is crying 

It is possible also inherit class properties. For example surname can be inherited from parent to child. Following code shows how to set and get surname:

/**
 * Parent class 
 */
class TestParent {
 
  protected String surname = ""; 
 
  /**
   * Set family surname
   * 
   * @access	public
   * @param	String
   * @return	void 
   */		
  public void setSurname ( String pa_surname )
  {
    if ( pa_surname.length() > 0 ) {
      this.surname = pa_surname;
    }		
  }
}
 
 
/**
 * Access modifier Final means class can not be extended 
 */
final class TestChild extends TestParent {
 
  /**
   * @access public
   * @param void
   * @return String
   */	
  public String getChildSurname ( )
  {
    return this.surname;
  }
}

Now we can set surname for parent and child. If we try to run next program:

/**
 * @file Overriding.java
 */
public final class Overriding {
 
  /**
   * @param	Array	args
   */
  public static void main(String[] args) 
  {
    // Parent reference and object
    TestParent p = new TestParent();
 
    // Child reference and Child object
    TestChild c = new TestChild(); 
 
    // Parent reference and parent method
    p.setSurname("Dubak senior");
 
    // parent reference
    System.out.println("Parent surname: "+ p.getParentSurname() );		
 
    // Child reference but parent method
    c.setSurname("Dubak junior");		
 
    // child reference
    System.out.println("Child surname: "+ c.getChildSurname() );				
  }
}

we will get the following result into the console:

Parent surname: Dubak senior
Child surname: Dubak junior

From previous example we can see, that method setSurname() is accessible from both clasess (parent and child). Also class property surname is accessible from Child class because is set as protected, which means can be inhereted but is protected from outside access. The value returned as surname depends on which class reference is used.

Published:

Add / read comments

FIND ME

Share, follow or connect with me.