Java Polimorphism & Inheritance


POLYMORPHISM
a.      Employee.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package praktikumenam;

/**
 *
 * @author arhiday
 */
public class Employee
{
   private String name;
   private String address;
   private int number;
   public Employee(String name, String address, int number)
   {
      System.out.println("Constructing an Employee");
      this.name = name;
      this.address = address;
      this.number = number;
   }
   public void mailCheck()
   {
      System.out.println("Mailing a check to " + this.name
       + " " + this.address);
   }
   public String toString()
   {
      return name + " " + address + " " + number;
   }
   public String getName()
   {
      return name;
   }
   public String getAddress()
   {
      return address;
   }
   public void setAddress(String newAddress)
   {
      address = newAddress;
   }
   public int getNumber()
   {
     return number;
   }
}

b.      Salary.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package praktikumenam;

/**
 *
 * @author arhiday
 */
public class Employee
{
   private String name;
   private String address;
   private int number;
   public Employee(String name, String address, int number)
   {
      System.out.println("Constructing an Employee");
      this.name = name;
      this.address = address;
      this.number = number;
   }
   public void mailCheck()
   {
      System.out.println("Mailing a check to " + this.name
       + " " + this.address);
   }
   public String toString()
   {
      return name + " " + address + " " + number;
   }
   public String getName()
   {
      return name;
   }
   public String getAddress()
   {
      return address;
   }
   public void setAddress(String newAddress)
   {
      address = newAddress;
   }
   public int getNumber()
   {
     return number;
   }
}

c.       VirtualDemo.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package praktikumenam;

/**
 *
 * @author arhiday
 */
/* File name : VirtualDemo.java */
public class VirtualDemo
{
   public static void main(String [] args)
   {
      Salary s = new Salary("Mohd Mohtashim", "Ambehta,  UP",
                                 3, 3600.00);
      Employee e = new Salary("John Adams", "Boston, MA",
                                 2, 2400.00);
      System.out.println("Call mailCheck using Salary reference --");
      s.mailCheck();
      System.out.println("\n Call mailCheck using Employee reference--");
      e.mailCheck();
    }
}




INHERITANCE

a.      Parent.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package praktikumenam;

/**
 *
 * @author arhiday
 */
public class Parent
{
  public String abc=null;
  public String xyz=null;

  public void printOutput()
  {
    System.out.println("Output of Inheritance  :"+abc);
    System.out.println("Output of Second Inheritance  :"+xyz);
  }
}

b.      Child.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package praktikumenam;

/**
 *
 * @author arhiday
 */
public class Child extends Parent
{
  public void parentMethod()
  {
      abc="This is inheritance java program";
      xyz="This is child class where extends used";

      printOutput();
  }
  public static void main(String[] args)
  {
      Child ic=new Child();
      ic.parentMethod();
  }
}

0 komentar:

Posting Komentar

pembaca yang baik adalah pembaca yang meninggalkan komentar