![]() |
Simple Java Tutorial |
![]() |
Java OOP Tutorial |
Java OOP Tutorial.
About this tutorial.
- This Java tutorial is about learning Java Object Oriented Programming, using interface, abstract class and overriding methods.
- In this tutorial we will create an application that controls messages send to difference participants in a project.
- If you just want to look through the solution of this tutorial you can download it here.
Create a Netbeans project
- As first action we need to create a project in Netbeans with the name Java02, which contain a java main class with the name app.Company.
Creating the Java OOP Application.
First a rough sketch of what to do:
We want to send different types of messages to a project, which distributes the messages to the participants in the project.
As participants, we have only a Manager and some Employees.
The participants shall return a response message back to the Project.
Then we must create a Manager class, Employee class, Project class and a Company class (the last one is already created as the main class with our main() method).
For the message delivery, we shall use interfaces, but first we like to create a base class for the participants.
-
Create a Java class , Person, with the package app.
The Person class shall be the super-class for the Employee class and the Manager class.
-
Make the Person class abstract as we do not want to create any instance of that class.
We want sub-classes of the Person class to implement the toString() method which already exist in the Object class as a concrete method.
-
To do this we define the toString() in the Person class and makes the method abstract.
The Person class must also have some member variables that is common for all sub-classes.
The Person class should now look like:package app; public abstract class Person { private String name; private int age; private double earnings; @Override public abstract String toString(); }
- Place the cursor after the last member variable and inside the class and right click to get the menu for inserting get accessors for the member variables.
- Select Insert code... from the menu and then Getter... from the new menu.
- Select all fields and click the Generate button. You should now have get accessors for all the member variables in the class.
- Place the cursor after the last member variable again and right click to get the menu for inserting a constructor.
- Select Insert code... from the menu and then Constructor... from the new menu.
-
Select all fields as argument for the constructor and click the Generate button.
The Person class should now look like:
package app; public abstract class Person { private String name; private int age; private double earnings; public Person(String name, int age, double earnings) { this.name = name; this.age = age; this.earnings = earnings; } public int getAge() { return age; } public double getEarnings() { return earnings; } public String getName() { return name; } @Override public abstract String toString(); }
As we want Message to be distributed through interfaces we will start to create an interface with name ResponseEvent.
-
Create a Java Interface , ResponseEvent, with the package app.
The ResponseEvent interface shall be used to send a response message back to the sender of other messages. We need a method, SendResponse, in the interface for this.
-
You must then make changes in the interface, ResponseEvent, to look like:
package app; public interface ResponseEvent { public void SendResponse (String message); }
When a project event appear we need to send other messages and thereby other interfaces.
-
Create a Java Interface , ProjectEvent, with the package app.
The Interface ProjectEvent shall handle all types of messages send by the project management so we need one method, sendNotification, with one argument for the message type, one String argument for the message and one reference argument for who is the sender of the message.
We must also define some constants to hold values for the different types of messages.
-
You must then make changes in the interface, ProjectEvent, to look like:
package app; public interface ProjectEvent { public int SOCIAL_Message = 1; // messageType for all participants public int PROJECT_Message = 2; // messageType for the Project (only manager) public int WORK_Message = 3; // messageType for all Employees (through manager) public void sendNotification(int messageType, String message, ResponseEvent responseEvent); }
The Project class must implement this interface, since the objects of the Class is responsible in distributing the various types of messages to participants in the project.
We want to differentiate between WORK and SOCIAL type messages and will then create two interfaces for that.
-
Create a Java Interface , WorkEvent, with the package app.
The Interface WorkEvent shall handle the WORK_message type and have one method with one String argument for the message and one reference argument for the sender of the message.
-
You must then make changes in the interface, WorkEvent, to look like:
package app; public interface WorkEvent { public void sendWorkMessage(String message, ResponseEvent responseEvent); }
The Employee class must implement this interface, as the objects of the classes must be able to respond to WORK_message.
-
Create a Java Interface , SocialEvent, with the package app.
The Interface SocialEvent shall handle the SOCIAL_Message type and have one method with one String argument for the message and one reference argument for the sender of the message.
-
You must then make changes in the interface, SocialEvent, to look like:
package app; public interface SocialEvent { public void sendSocialMessage(String message, ResponseEvent responseEvent); }
The Employee class must implement this interface, as object of the Employee class shall react on the SOCIAL_messages. (The Manager will react on this message through the ProjectEvent implementation).
-
Create a Java class , Employee, with the package app.
The Employee class must extend the Person class and implements the interfaces SocialEvent and WorkEvent. We need to write implementation codes for these interfaces methods as well.
The toString() method is defined abstract in the Person class and must be implemented.
In the Employee class we want to have a member variable reference to the Manager, which we want to use in our reporting or response messages.
We must have a Constructor with the same arguments as the Constructor in the Person class including an argument that has a reference to the Manager.
In the Constructor we must make a super() method call to the super class constructor and set the class member variable reference to the Manager.
-
You must then make changes in the class, Employee, to look like:
package app; public class Employee extends Person implements SocialEvent, WorkEvent { private Manager manager = null; public Employee(Manager manager, String name, int age, double earnings) { super(name, age, earnings); this.manager = manager; } public void sendSocialMessage(String message, ResponseEvent responseEvent) { String ret = "I am the Employee, " + getName() + ", and accept the message \"" + message + "\""; responseEvent.SendResponse(ret); } public void sendWorkMessage(String message, ResponseEvent responseEvent) { String ret = getName() + " got: \"" + message + "\""; responseEvent.SendResponse(ret); } @Override public String toString() { return "I am " + getName() + ", " + getAge() + " old, earn $" + getEarnings() + " and my manager is " + manager.getName(); } }
In both interface implemented methods (sendSocialMessage and sendWorkMessage) we simply create a new message and send it back to the sender as a response.
-
Create a Java class , Manager, with the package app.
The Manager class must extend the Person class and implements the interface ProjectEvent. We need to write implementation codes for this interface method(s) as well.
The toString() method is defined abstract in the Person class and must be implemented.
In the Manager class, we want to have a member variable reference to the Project, which we want to use in our reporting or response messages.
As Manager want to send WORK messages to Employees we must have an array of member variable references to the WorkEvent interfaces in the Manager class.
To control which Employee we want to send WORK messages to we must create in the Manager class a method that adds the WorkEvent, which the Employee class should implement, to the array of member variable WorkEvent references.
We must have a Constructor in the Manager Class with the same arguments as the Constructor in the Person class, which the Manager class extends, including an argument that has a reference to the Project.
In the Constructor we must first of all make a super() call to the super class constructor, set member variable reference to the Project and add the manager to the Project as a participant.
-
You must then make changes in the class, Manager, to look like:
public class Manager extends Person implements ProjectEvent { private Project project = null; private WorkEvent[] workEvents = null; private static int EXTENDING = 2; private int currIndex = 0; public Manager(Project project, String name, int age, double earnings) { super(name, age, earnings); this.project = project; project.addPerson(this); } public void addWorkEvent(WorkEvent workEvent) { if (workEvents == null) { workEvents = new WorkEvent[EXTENDING]; } else { if (workEvents.length == currIndex) { WorkEvent[] temp = new WorkEvent[currIndex + EXTENDING]; for (int i = 0; i < workEvents.length; i++) { temp[i] = workEvents[i]; } workEvents = temp; } } workEvents[currIndex++] = workEvent; project.addPerson((Person)workEvent); } public void sendNotification(int messageType, String message, ResponseEvent responseEvent) { switch (messageType) { case WORK_Message: { String ret = "As Manager (" + getName() + ") I will send the message \"" + message + "\" to all workers!"; responseEvent.SendResponse(ret); for (WorkEvent workEvent : workEvents) { if (workEvent == null) { break; } workEvent.sendWorkMessage("Work intruction from your Manager, " + getName() + ": " + message, responseEvent); } break; } case SOCIAL_Message: { String ret = "I am the Manager, " + getName() + ", and accept the message \"" + message + "\""; responseEvent.SendResponse(ret); break; } case PROJECT_Message: { String ret = "I am the Manager, " + getName() + ", and I shall take actions about \"" + message + "\""; responseEvent.SendResponse(ret); break; } } } @Override public String toString() { return "I am "+getName()+", "+getAge()+" old, earn $"+ getEarnings()+" and is the manager "; } }
In the implementation of the method, sendNotification(), we have to create different code implementation which depends on the incoming messageType argument.
If the messageType is a WORK_Message, the Manager will response with a message back to the sender (the Project) and send the work message to each of the objects that is of a class that has implemented the WorkEvent Interface (Employees).
If the message type is a SOCIAL_Message or PROJECT_Message, the Manager only response with a message back to the sender (the Project).
-
Then we have to
create a Java class , Project, with the package app.
The Project class must implements the interface ResponseEvent. We need to write implementation codes for this interface method(s) as well.
As the Project should send messages to project participants we must have an array of member variable reference to the Person, which is the super-class to all participants.
Then we need to implement a method that adds Person objects to the array of member variable.
At last we must implement a sendMessage() method that have arguments of the message type to send and a message of String type argument. This method should send message of Project type (to Manager) or Social type (to all project participants).
-
You must then make changes in the class, Project, to look like:
package app; public class Project implements ResponseEvent { private Person[] persons = null; private static int EXTENDING = 2; private int currIndex = 0; public void addPerson(Person person) { if (persons == null) { persons = new Person[EXTENDING]; } else { if (persons.length == currIndex) { Person[] temp = new Person[currIndex + EXTENDING]; for (int i = 0; i < persons.length; i++) { temp[i] = persons[i]; } persons = temp; } } persons[currIndex++] = person; } public void sendMessage(int messageType, String message) { for (Person person : persons) { if (person == null) { break; } if (person instanceof ProjectEvent) { ((ProjectEvent) person).sendNotification(messageType, message, this); } if (person instanceof SocialEvent) { if (messageType == ProjectEvent.SOCIAL_Message) { ((SocialEvent) person).sendSocialMessage(message, this); } } } } public void SendResponse(String message) { String outStr = "Response on Event: "; System.out.println(outStr + message); } public Person[] getAllInProject() { return persons; } }
In the implementation of the method, sendResponse(), we only print on the standard out received message from project participants.
We have already created the Company class so now we need to put some code into the main() method of that class to test out application.
-
Copy then this code into the main() method:
Project project = new Project(); Manager ricard = new Manager(project, "Ricard", 45, 70000); ricard.addWorkEvent(new Employee(ricard, "Cindy", 34, 50000)); ricard.addWorkEvent(new Employee(ricard, "John", 23, 45000)); ricard.addWorkEvent(new Employee(ricard, "James", 28, 48000)); // list all persons engaged in the project for (Person person : project.getAllInProject()) { if (person != null) { System.out.println(person.toString()); } } // Send som instructions to the project project.sendMessage(ProjectEvent.PROJECT_Message, "Wood material has arrived."); project.sendMessage(ProjectEvent.WORK_Message, "Everybody must work overtime today."); project.sendMessage(ProjectEvent.SOCIAL_Message, "A extra bonus of $500 to everybody to day.");
Running the Java Application.
- Right click the project, Java02, and select run from the menu.
I am Ricard, 45 old, earn $70000.0 and is the manager
I am Cindy, 34 old, earn $50000.0 and my manager is Ricard
I am John, 23 old, earn $45000.0 and my manager is Ricard
I am James, 28 old, earn $48000.0 and my manager is Ricard
Response on Event: I am the Manager, Ricard, and I shall take
actions about "Wood material has arrived."
Response on Event: As Manager (Ricard) I will send the message
"Everybody must work overtime today." to all workers!
Response on Event: Cindy got: "Work intruction from your Manager,
Ricard: Everybody must work overtime today."
Response on Event: John got: "Work intruction from your Manager,
Ricard: Everybody must work overtime today."
Response on Event: James got: "Work intruction from your Manager,
Ricard: Everybody must work overtime today."
Response on Event: I am the Manager, Ricard, and accept the message
"A extra bonus of $500 to everybody to day."
Response on Event: I am the Employee, Cindy, and accept the message
"A extra bonus of $500 to everybody to day."
Response on Event: I am the Employee, John, and accept the message
"A extra bonus of $500 to everybody to day."
Response on Event: I am the Employee, James, and accept the message
"A extra bonus of $500 to everybody to day."