Object Oriented Programming(OOP) Explained with Java Examples (Part 1)

In this lesson, we would understand the idea of object oriented programming and then we would show some examples of how Object Oriented Programing works using Java.
We would use Netbeans IDE to run our program codes, so make sure you have it installed in your system.

This lesson would be based on the following sub-topics:

  • What is Object Oriented Programming
  • Create a Class in Java
  • Create  a Subclass
  • Concept of Inheritance
  • Concept of Polymorphism
  • Encapsulation

So let’s start with the first one

What is Object Oriented Programming?
 Object oriented programming is a programming method that models entities in the program like real objects with attributes.

 To Illustration OOP in Java, we would try to build a hospital personnel database made up of both patients and physicians.

Create a Class in Java (Superclass)
Let’s create a class in Java called Person. To do this you would need Netbeans installed in your system.
Step 1: Open Netbeans and create a Java Application
Step 2: Add  a new class and name it Person
Step 3: Add the following attributes to the class

  • SSN
  • Firstname
  • Lastname
  • Email
  • Phone
  • DateOfBirth

Step 4: Add one constructor to the class that takes one parameter: SSN
Step 5: Add another constructor that takes three parameters: SSN, Firstname and Lastname
Step 6: Add a third constructor that take no parameter.

At the end of this step, your class file would look like shown below:

Step 7: Add Getters and Setters
You could do this manually, but you could save a lot of time by allowing Netbeans to generate these for you. To do that:

  • Right-click on inside the class file (in an open space)
  • In the list that appears, click on Insert Code
  • In the list that appears, click on Getter and Setter. The window below appears:

  • Tick Encapsulate Fields
  • Click on Select All to select all the fields
  • Click on Generate. At this point the getters and setters are added to your class.

Step 8: Create the Patient Class (Subclass)
Add another class to your program and name it Patient. Remember to place this class in MyClasses package.
After the class name add ‘extends Person’. This tell the compiler that the Patient class is a subset of the Person class. This means that “a Patient is a Person”.
Add three attributes:

  • AdmissionDate
  • Allergies
  • Diagnosis

Right-click and choose Insert Code
Choose Constructor from the list that appears. This would display the Generate Construction dialog box as shown below:

In the Select Super Construction section, select everything.

Click on Generate.
After then, your patient class would be as shown in the figure below:
 Add Getter and Setter. The same way you did in the Person class.
Step 9: Add the Physician Class
Add a class to the project and name it Physician. Remember to put this class in the MyClasses folder.
Make the class extend Person
Follow the same procedure as with the Patient class but this time you will add the following attributes:

  • Specialty
  • Department
  • Office

Add the Constructors the way you did for the Patient class
Add Getter and Setter the way you did for the Patient class

The UML Diagram below summarizes the three classes you have in  your project.


Try to examine the class diagram to make sure you understand various parts of it. In the next lesson, we would use this three classes to illustrate the concept of Inheritance. The video tutorial of this lesson would be published in couple of days.