class Person { String name ; int age ; Person() { System.out.println( "Person()" ) ; } Person( String nameP , int ageP ) { name = nameP ; age = ageP ; System.out.println( "Person() with arguments" ) ; } void print() { System.out.println("name: " + name + " age: " + age ) ; } } class Employee extends Person { String jobTitle ; String DeptName ; String ssn ; Employee( String nameP, int ageP, String jobTitleP, String DeptNameP, String ssnP ) { //super() //If we don't supply super then the default super // ( without any args constructor ) if // it exists will be called for us //jobTitleP, DeptNameP, ssnP super( nameP , ageP ) ; jobTitle = jobTitleP ; DeptName = DeptNameP ; ssn = ssnP ; System.out.println( "Employee with arguments()" ) ; } //Good idea. Compile time checking . //if base class does not have this method we //get an error @Override void print() { //Calling a base class method from derived class //if it has the same method name super.print() ; System.out.println("Job Title: " + jobTitle + " Dept: " + DeptName + " ssn:" + ssn ) ; } } public class Upcasting { public static void main( String args[] ) { Employee employeeObject1 = new Employee("Joe Bugner", 35 , "Programmer", "Engineering" , "119701024" ) ; employeeObject1.print() ; //Allowed since an employee is a Person conceptually Person personObject = employeeObject1 ; //ok to call base class functions personObject.print() ; //Downcasting is not allowed // A person may be a student not an employee //employeeObject1 = personObject ; //Allowed. If it fails while running we get a run time error employeeObject1 = (Employee)personObject ; } //public static void main( String args[] ) }