Steps to follow:
1. Modify Main.java as shown below in Code-11.2.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.) The code fragment that needs to be added is highlighted in bold and blue-colored font.
* cd \myjavaprograms
* jedit personpackage\Main.java
package personpackage;
public class Main {
public static void main(String [] args ){
Student student1 =new Student();
// Calling methods defined in Person class, which is a parent class of Student class
student1.setName("Sang");
System.out.println("Calling getName() method: name is " + student1.getName());
}
}
Code-11.2.a: Modified Main.java
2. Compile and run the code using a directory structure.
* cd \myjavaprograms
* javac personpackage\*.java
* java -classpath . personpackage.Main
3. Verify the result is as following
* C:\myjavaprograms>java -classpath . personpackage.Main
Inside Person:Constructor
Inside Student:Constructor
Person: getName()
Calling getName() method: name is Sang
5. Modify the Student.java as shown below in Code-11.2.b. The Student class is overriding the getName() method of its parent class, Person class. The code fragment that needs to be added is highlighted in bold and blue-colored font.
* cd \myjavaprograms
* jedit personpackage\Student.java
package personpackage;
public class Student extends Person {
private String hobby;
public Student(){
System.out.println("Inside Student:Constructor");
}
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" + super.getName();
}
}
Code-11.2.b: Modified Student.java
6. Compile and run the code using a directory structure.
* cd \myjavaprograms
* javac personpackage\*.java
* java -classpath . personpackage.Main
7. Verify the result is as following
* C:\myjavaprograms>java -classpath . personpackage.Main
Inside Person:Constructor
Inside Student:Constructor
Student: getName()
Person: getName()
Calling getName() method: name is PassionateSang
Steps to follow if you are using NetBeans
1. Modify the Main.java as shown in Code-11.2.a.
2. Right click personpackage package node (not PersonPackage project node) and select Compile Package (F9)
3. Right click Main select Run File
4. Modify the Student.java as shown in Code-11.2.b.
5. Right click personpackage pacakge node (not PersonPackage project node) and select Compile Package (F9)
6. Right click Main select Run File
Homework:
1. In your TuftsStudent class, override getHobby() and setHobby() methods of the Student class as following
* public String getHobby(){
System.out.println("Inside TuftsStudent:getHobby() method");
return "My hobby is " + super.getHobby();
}
public void setHobby(String s){
System.out.println("Inside TuftsStudent:setHobby() method");
super.setHobby(s);
}
2. Change Main.java to invoke setHobby() and getHobby() methods of the newly created TuftsStudent object instances as followoing.
* // set hobbies of student2 and student3
student2.setHobby("swimming");
student3.setHobby("dancing");
// get hobbies of student2 and student3
String hobby2 = student2.getHobby();
System.out.println("Hobby of student2 " + hobby2);
String hobby3 = student3.getHobby();
System.out.println("Hobby of student3 " + hobby3);
3. Compile and run the code. You should see the following result.
* Inside Person:Constructor
Inside Student:Constructor
Inside Person:Constructor
Inside Student:Constructor
Inside TuftsStudent:Constructor
Inside Person:Constructor
Inside Student:Constructor
Inside TuftsStudent:Constructor
Inside TuftsStudent:setHobby() method
Inside TuftsStudent:setHobby() method
Inside TuftsStudent:getHobby() method
Hobby of student2 My hobby is swimming
Inside TuftsStudent:getHobby() method
Hobby of student3 My hobby is dancing
Popular Posts
-
Files in VB .NET Working with Directories We will work with the File and Directory classes in this section. We will create a directory and c...
-
Please do this exercise at the command line instead of using NetBeans. This is to learn the concept of classpath without the help of NetBean...
-
Now I will show you how to retrieve the data posted from a HTML file in a JSP page. Consider an html page that prompts the user to enter his...
-
Lab exercises and homeworks: * Things to check before you start the lab * Chapter 3 (Class #1, Jan. 16th homework) o Exerc...
-
Polymorphism Polymorphism is one of the crucial features of OOP. It means "one name, multiple forms". It is also called as Overlo...
-
The SQL AVG aggregate function selects the average value for certain table column. Have a look at the SQL AVG syntax: SELECT AVG(Column1) FR...
-
Getting Started Note that all the images in this tutorial taken from Visual Basic version 6.0. If you using other version of Visual Basic, t...
-
Introduction: If you are new to Exception handling, please read "Exception Handling Statements" section of the Java Progamming Tut...
-
Introduction: In this exercise, you are going to build and run a sample Java program called CommandLineusing NetBeans. The sample program c...
-
SQL Server Express is a free, easy to use, redistributable version of SQL Server 2005 designed for building simple data-driven applications....
No comments:
Post a Comment