# Java Inheritance Example ## Example1 App.java ```java public class App{ public static void main(String []args){ Employee emp = new Employee(); emp.setName("James"); } } ``` Employee.java ```java public class Employee{ int age; String name; public void setName(String name){ this.name = name; System.out.printf("My Name is %s",this.name); } public String getName(){ return this.name; } } ``` ## Example2 Employee.java ```java import java.util.Date; public class Employee { protected String name; protected Date hireDate; public Employee(){} public Employee(String theName, Date theDate){ name = theName; hireDate = theDate; } public Date getHireDate(){ return hireDate; } public String getName(){ return name; } } ``` HourlyEmployee.java ```java import java.util.Date; public class HourlyEmployee extends Employee{ private double wageRate; public HourlyEmployee(String theName, Date theDate, double rate){ name = theName; hireDate = theDate; wageRate = rate; } } ``` Company.java ```java import java.util.Date; public class Company { public static void main(String[] args){ HourlyEmployee hourlyEmployee = new HourlyEmployee("Josephine", new Date(114,0,1), 100); System.out.println(hourlyEmployee.getName()); } } ``` ## Example 3 Employee.java ```java import java.util.Date; public class Employee { protected String name; protected Date hireDate; public Employee(){} public Employee(String theName, Date theDate){ name = theName; hireDate = theDate; } public Date getHireDate(){ return hireDate; } public String getName(){ return name; } public double getSalary(int hour){ return 1000.0*hour; } } ``` HourlyEmployee.java ```java import java.util.Date; public class HourlyEmployee extends Employee{ private double wageRate; public HourlyEmployee(String theName, Date theDate, double rate){ name = theName; hireDate = theDate; wageRate = rate; } public double getSalary(int hour){ return wageRate*hour; } } ``` Company.java ```java import java.util.Date; public class Company { public static void main(String[] args){ HourlyEmployee hourlyEmployee = new HourlyEmployee("Josephine", new Date(114,0,1), 100); System.out.println(hourlyEmployee.getSalary(24)); } } ``` ## Example 4 Employee.java ```java import java.util.Date; public class Employee { protected String name; protected Date hireDate; public Employee(){} public Employee(String theName, Date theDate){ name = theName; hireDate = theDate; } public Date getHireDate(){ return hireDate; } final public String getName(){ return name; } public double getSalary(int hour){ return 1000.0*hour; } } ``` HourlyEmployee.java ```java import java.util.Date; public class HourlyEmployee extends Employee{ private double wageRate; public HourlyEmployee(String theName, Date theDate, double rate){ name = theName; hireDate = theDate; wageRate = rate; } public double getSalary(int hour){ return wageRate*hour; } // 看看把下面反註解會發生甚麼事 // public String getName(){ // return name; // } } ``` Company.java ```java import java.util.Date; public class Company { public static void main(String[] args){ HourlyEmployee hourlyEmployee = new HourlyEmployee("Josephine", new Date(114,0,1), 100); System.out.println(hourlyEmployee.getName()); } } ``` ## Example 5 B.java ```java class A { int a; public A() { a = 7; System.out.println("Step 1"); } } class B extends A { public B() { System.out.println("Step 2"); } public static void main(String[] args){ B bo = new B(); } } ``` ## Example 6 Employee.java ```java import java.util.Date; public class Employee { private String name; protected Date hireDate; public Employee(){} public Employee(String theName, Date theDate){ name = theName; hireDate = theDate; } public Date getHireDate(){ return hireDate; } public String getName(){ return name; } } ``` HourlyEmployee.java ```java import java.util.Date; public class HourlyEmployee extends Employee{ private double wageRate; public HourlyEmployee(String theName, Date theDate, double rate){ name = theName; hireDate = theDate; wageRate = rate; } public double getSalary(int hour){ return wageRate*hour; } public String getName(){ return "Hourly Employee:" + super.getName(); } } ``` Company.java ```java import java.util.Date; public class Company { public static void main(String[] args){ HourlyEmployee hourlyEmployee = new HourlyEmployee("Josephine", new Date(114,0,1), 100); System.out.println(hourlyEmployee.getName()); } } ``` ## Example 7 ```java import java.util.Date; public class Employee { private String name; private Date hireDate; public Employee(){} public Employee(String theName, Date theDate){ name = theName; hireDate = theDate; } public Date getHireDate(){ return hireDate; } public String getName(){ return name; } public boolean equals(Employee otherone){ if(otherone.getName().equals(this.name) && otherone.getHireDate().equals(this.hireDate)){ return true; }else{ return false; } } } ``` ## Example 8 ```java= import java.util.Date; public class CompareTest { public static void main(String[] args) { Employee employeeA = new Employee("Josephine", new Date(114,0,1)); Employee employeeB = new Employee("Josephine", new Date(114,0,1)); System.out.println(employeeA.equals(employeeB)); System.out.println(employeeA == employeeB); if(employeeA instanceof Employee){ System.out.println(employeeA.getName() + "is an object of Employee"); } System.out.println(employeeA.getClass().getName()); } } ```