Popular Posts

Monday, February 23, 2009

Exercise 11.1: Inheritance - Constructor

Steps to follow:

1. Write Person.java as shown below in Code-11.1.a under personpackage directory. (You are welcome to do this work using either command line tools or NetBeans. The instruction for using NetBeans is given below.)

* cd \myjavaprograms (if you are not in this directory already)
* mkdir personpackage (to reflect the packaging structure)
* jedit personpackage\Person.java (to create Person.java under personpackage directory)

// The Person class belongs to personpackage package. What this means is
// that the Person.java file should reside under personpackage directory in the
// local file system.

package personpackage;

public class Person {

// Fields of the Person class. They are private. So no other class can
// access these fields directory. Instead, they are likely to use getter
// and setter methods of these fields to access and set the fields.
private String name;
private String address;

// This is the first Constructor method of the Person class.
public Person(){
System.out.println("Inside Person:Constructor");
}

// Thisis the second Constructor method of the Peson class.
public Person (String name, String address){
System.out.println("Inside Person:Constructor 2 receiving two parameters: " + name + ", " + address);
this.name = name;
this.address = address;
}

// Accessor method (getter method) of the name field.
public String getName(){
System.out.println("Person: getName()");
return name;
}

// Mutator method (setter method) of the name field.
public void setName(String s){
name = s;
}

// Accessor method (getter method) of the address field.
public String getAddress(){
return address;
}

// Mutator method (setter method) of the address field.
public void setAddress(String s){
address = s;
}
}
Code-11.1.a: Person.java

2. Write Student.java as shown below in Code-11.1.b under personpackage directory.

* cd \myjavaprograms (if you are not in this directory already)
* jedit personpackage\Student.java (to create Student.java under personpackage directory)


// The Student class belongs to personpackage package. What this means is
// that the Student.java file should reside under personpackage directory in the
// local file system.

package personpackage;

// The Student class extends the Person class.
// In other words, the Student class and Person class has an inheritance
// relationship. The Person class is a parent class and the Student class
// is a child class in this relationship.

public class Student extends Person {

private String hobby;

// Constructor of the Student class. Before this constructor method
// gets invoked, the constructor chain of the ancester classes will be
// invoked first.
public Student(){
System.out.println("Inside Student:Constructor");
}

public String getHobby(){
return hobby;
}

public void setHobby(String s){
hobby = s;
}
}
Code-11.1.b: Student.java

3. Write Main.java as shown below in Code-11.1.c below under personpackage directory.

* cd \myjavaprograms (if you are not in this directory already)
* jedit personpackage\Main.java (to create Main.java under personpackage directory)


package personpackage;

public class Main {

public static void main(String [] args ){
Student student1 =new Student();
}

}
Code-11.1.c: Main.java

4. Compile and run the code using a directory structure.

* cd \myjavaprograms
* javac personpackage\*.java
* java -classpath . personpackage.Main

5. Verify the result is as following

* C:\myjavaprograms>java -classpath . personpackage.Main
Inside Person:Constructor
Inside Student:Constructor

6. Modify the Student.java as following. The code fragment that needs to be added is highlighted in bold and blue-colored font.

package personpackage;

public class Student extends Person {

private String hobby;

public Student(){
super("Sang", "1 Dreamland");
System.out.println("Inside Student:Constructor");
}

public String getHobby(){
return hobby;
}

public void setHobby(String s){
hobby = s;
}
}
Code-11.1.d: Modified Student.java

7. Compile and run the code using a directory structure.

* cd \myjavaprograms
* javac personpackage\*.java
* java -classpath . personpackage.Main

8. Verify the result is as following

* C:\myjavaprograms>java -classpath . personpackage.Main
Inside Person:Constructor 2 receiving two parameters: Sang, 1 Dreamland
Inside Student:Constructor


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: /bin/netbeans or click NetBeans IDE desktop icon

2. Write 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 PersonPackage
o Click Finish

3. Replace the code in the NetBeans generated Main.java with the code of Code-11.1.c above.
4. Write Person.java

* Right personpackage package node (not PersonPackage 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.1.a above
6. Write Student.java

* Right personpackage node (not PersonPackage 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.1.b above
8. Right click personpackage package node (not PersonPackage project node) and select Compile Package (F9)
9. Right click Main select Run File
10.Modify the Student.java as shown in Code-11.1.d.
11. Right click personpackage pacakge node (not PersonPackage project node) and select Compile Package (F9)
12. Right click Main select Run File


Homework:

1. Write TuftsStudent.java as following

* TuftsStudent class extends Student class
* Write a constructor of the TuftsStudent class as following
o public TuftsStudent(){
System.out.println("Inside TuftsStudent:Constructor");
}

2. Modify the Main.java to create an instance of TuftsStudent class as following

* TuftsStudent student2 =new TuftsStudent();
Student student3 =new TuftsStudent();

3. Compile and run the code. You should see the following:

* Inside Person:Constructor
Inside Student:Constructor
Inside Person:Constructor
Inside Student:Constructor
Inside TuftsStudent:Constructor
Inside Person:Constructor
Inside Student:Constructor
Inside TuftsStudent:Constructor

No comments:

Post a Comment