1. Write Person.java as shown below in Code-11.3.a under polypackage directory. (You are welcome to do this work using either command line tools or NetBeans. The instruction for using NetBeans is given below.)
This is the same Person.java as in the previous exercise except the package name. Person class is a parent class of both Student and Employee classes, which you will write in the subsequent steps.
* cd \myjavaprograms
* mkdir polypackage
* jedit polypackage\Person.java
package polypackage;
public class Person {
private String name;
private String address;
public Person(){
System.out.println("Inside Person:Constructor");
}
public Person (String name, String address){
System.out.println("Inside Person:Constructor 2 receiving two parameters: " + name + ", " + address);
this.name = name;
this.address = address;
}
public String getName(){
System.out.println("Person: getName()");
return name;
}
public void setName(String s){
name = s;
}
public String getAddress(){
return address;
}
public void setAddress(String s){
address = s;
}
}
Code-11.3.a: Person.java
2. Write Student.java as shown below in Code-11.3.b. Student class is a subclass of a Person class.
* cd \myjavaprograms (if you are not in this directory already)
* jedit polypackage\Student.java
package polypackage;
public class Student extends Person {
private String hobby;
public Student(){
System.out.println("Inside Student:Constructor");
}
public Student (String name, String address){
super(name, address);
System.out.println("Inside Student:Constructor 2 receiving two parameters: " + name + ", " + address);
}
public String getHobby(){
return hobby;
}
public void setHobby(String s){
hobby = s;
}
// Override getName() method of the parent class
public String getName(){
System.out.println("Student: getName()");
return "Passionate Student " + super.getName();
}
}
Code-11.3.b: Student.java
3. Write Employee.java as shown below in Code-11.3.c. Employee class is subclass of Person class.
* cd \myjavaprograms (if you are not in this directory already)
* jedit polypackage\Employee.java
package polypackage;
public class Employee extends Person {
private String hobby;
public Employee(){
System.out.println("Inside Employee:Constructor");
}
public Employee(String name, String address){
super(name, address);
System.out.println("Inside Employee:Constructor 2 receiving two parameters: " + name + ", " + address);
}
public String getHobby(){
return hobby;
}
public void setHobby(String s){
hobby = s;
}
// Override getName() method of the parent class
public String getName(){
System.out.println("Employee: getName()");
return "Not so Passionate Employee " + super.getName();
}
}
Code-11.3.c: Employee.java
4. Write Main.java as shown below in Code-11.3.d.
* cd \myjavaprograms (if you are not in this directory already)
* jedit polypackage\Main.java
package polypackage;
public class Main {
public static void main( String[] args ) {
Person ref;
Student studentObject = new Student("Sang", "1 Dreamland");
Employee employeeObject = new Employee("Young", "2 Dreamland");
ref = studentObject; //Person ref. points to a
// Student object
//getName of Student class is called
String temp1=ref.getName();
System.out.println( temp1 );
ref = employeeObject; //Person ref. points to an
// Employee object
//getName of Employee class is called
String temp2 = ref.getName();
System.out.println( temp2 );
}
}
Code-11.3.d: Main.java
5. Compile and run the code using a directory structure.
* cd \myjavaprograms
* javac polypackage\*.java
* java -classpath . polypackage.Main
6. Verify the result is as following. Note that depending on what object type the ref variable refers to, Employee type or Student type, proper method gets invoked.
* C:\myjavaprograms>java -classpath . polypackage.Main
Inside Person:Constructor 2 receiving two parameters: Sang, 1 Dreamland
Inside Student:Constructor 2 receiving two parameters: Sang, 1 Dreamland
Inside Person:Constructor 2 receiving two parameters: Young, 2 Dreamland
Inside Employee:Constructor 2 receiving two parameters: Young, 2 Dreamland
Student: getName()
Person: getName()
Passionate Student Sang
Employee: getName()
Person: getName()
Not so Passionate Employee Young
Steps to follow if you are using NetBeans
1. Start the NetBeans IDE (if you have not done so yet)
* Windows: Start > All Programs > NetBeans 5.0 > NetBeans IDE or click NetBeans IDE 5.0 desktop icon
* Solaris/Linux:
2. Create a new NetBeans project and Main.java main class
* Select File from the menu bar and select New Project.
* Under Choose Project, select General and Java Application
* Click Next.
* Under Name and Location pane, (Figure-10 below)
o For Project Name field, fill it with PolyPackage
o Click Finish
3. Replace the code in the NetBeans generated Main.java with the code of Code-11.3.d above.
4. Write Person.java
* Right polypackage package node (not PolyPackage project node) and select New->Java Class
* Under Name and Location pane,
o for Class Name field, type Person
o Click Finish
5. Replace the code in the NetBeans generated Person.java with the one of Code-11.3.a above
6. Write Student.java
* Right polypackage node (not PolyPackage project node) and select New->Java Class
* Under Name and Location pane,
o for Class Name field, type Student
o Click Finish
7. Replace the code in the NetBeans generated Student.java with the one of Code-11.3.b above
8. Write Employee.java
* Right polypackage node (not PolyPackage project node) and select New->Java Class
* Under Name and Location pane,
o for Class Name field, type Employee
o Click Finish
9. Replace the code in the NetBeans generated Employee.java with the one of Code-11.3.c above
10. Right click polypackage package node (not PolyPackage project node) and select Compile Package (F9)
11. Right click Main select Run File
No comments:
Post a Comment