javasprint.com  
 
 
  Object Oriented Programming Concept with an Example
A Design Tutorial...

written by Bharat Chhajer, Java Sprint

Subscribe
 

Overview

Object Oriented Programming is a way of programming where you create a represtation of real world in the computer's memory by creating Objects and establishing relationships between them. To create Objects, you need to create Classes first.

Definition of Object Oriented Programming

Some of the definitions i came across and like:
Simple definition: Object-oriented programming (OOP) is programming using "objects" to develop computer programs.
Another definition: Object-oriented programming (OOP) is a method of programming based on well-defined and cooperating objects.
Yet another definition: Object-oriented programming (OOP) refers to a programming methodology based on objects, instead of just functions and procedures.
One more definition: Computer program as a combination of data structures (called objects)

Explation of Object Oriented Programming Concept with an Example:

In a program, you create an object for every thing in the real world and establish relationships between them. Hence you recreate the world in Computer's memory. How the relationship is established between objects is shown below using two diagrams. Lets say, you are creating a software for running a University. You will create an Object of University Class, Five Objects of Department Class if there are five departments in the University. 25 Objects of Professor Class if there are 25 professors in the University (Lets say 5 professors per department,) 20 Semester Objects if there are four semesters per department, 600 Student Objects if there are 30 students per semester.

After creating the Objects now we need to establish relation ships between them. How do we do that ? There are only two kinds of relations possible in Java. "Is A" and "Has A". Their meaning in Java is very literal. Like, An apple "Is A" fruit and An apple "Has A" seed (actually has lot of seeds). Also like, An Orange "Is A" fruit. The "Is A" relation is established between two classes by means of Inheritance (using extends keyword). The "Has A" relation is established between two classes by means of Association (using fields).

Note: Each Object has details about the thing in the real world. Like Professor Object will have details like, the professor's name, age, speciality, etc... and a Department Object will have details like name of the department, name of the department head, etc...

Now lets establish relationships between Objects. Following shows the application of "Has A" relation. So a University Object will contain five Objects of Department class. Each Department Object will contain five Objects of Professor Class and four Objects of Semester class. Each Object of Semester will contain 30 Objects of Student Class. Each of the above objects will also have other appropriate fields to store details like University Name, Department Name, Profesor's name, age, speicality, Student's name, age, semester number etc.... It feels like assembling of Objects. And, that's what it is.

Note: If anything is confusing in this article you can email info@javasprint.com . This article will get updated. Learn and help learn.

Object Oriented Programming (OOP) Diagram "Has A relation between Objects"

Object Oriented Programming Diagram, Has A relation

What about the "Is A" relationship ? That's the next question ? Right ? The only place i see a "Is A" relationship is between Student Class and Professor Class. In our example both Student and Professor have fields "name" and "age". So, we would create a Class called may be "Person" with fields "name" and "age". Then Create a Class Student that inherits from Person Class. The Student class will inherit the two fields from Person and have a field called majorSubject of its own. The Professor class will also inherit from Person and can have zero or more fields of its own like "subject", etc... The "Is A" relation is between Classes. It helps remove duplication(redundancy) of code in various parts of program. The "Has A" relation is between Objects.

Object Oriented Programming (OOP) Diagram "Is A relation between Classes"

Object Oriented Programming Diagram, Has A relation

Note: I did not talk much about the methods but never mind. Its more imporntant to get clear with how the data is structured. I would like to say one thing though. There will be appropriate method in every Class. Like say "setHeadOfDepartment(String newHead)" method in Department Class, or "incrementAge()" method in Person Class. You call methods of a Class on its Objects.

Subscribe

Object Oriented Programming (OOP) is programming an application program in an Object Oriented way. Here everything is an Object. A memory full of Objects with relationships between them. You think in terms of Objects, you design in terms of Objects, you program in terms of Objects. Is it possible to create the above program with out using Object Oriented Design and Programming ? Yes, its possible to create the above program WITHOUT using Object Oriented Design and Programming. But the problem is that such a program will be difficult to maintain after it has been released. Such a program will be difficult to enhance with new features in the future.

So, What if you have been working in COBOL for a lot of years ? Or What if you were a Manager for a lot of years and hence never tried to code your self. Or What if you enjoyed being a House Wife for so many years and since your kids have now grown up and left to be on their own, you want to learn Object Oriented Programming now. Object Oriented Programming is not rocket science, it is simply as described above.

We have a small 14 hour course, Which focusses only on the very core concepts of the Core Java including Object Oriented Programming (OOP). Its designed to make one very independent. You do hands on coding through out the course. A tutor is with you all the time. You get 100% attention as it is one on one. Write down a paragraph on how you feel about Java and how much you know about Java before you take up the java training course (if you take it up). Write down another paragraph after you finish the java training course. Then read what you wrote before taking the java training course. You will know.

Note: Interfaces are important as well. They give flexibility to your program. They too give "Is A" relationship. They allow multiple Inheritance in Java as otherwise you can only inherit from a single class.

Note: If anything is confusing in this article you can email info@javasprint.com . This article will get updated. Learn and help learn.


written by Bharat Chhajer, Java Sprint

Subscribe

 
  Important definitions  
 

What is Java? - Definition

A high-level programming language developed by Sun Microsystem. By now, owned by Oracle. Java was originally called OAK, and was designed for handheld devices. It became popular in web programing since around 1995. Now, its popular with cell phones, televisions, gaming devices, almost every device type. Java is a Object oriented language like "C++" minus some error prone features like pointers and multiple inheritance from Classes.

What is inheritance? - Definition

Inheritance is a concept in which a sub class(child) inherits fields and methods of a super class(parent). Inheritance establishes a "Is A" relation between classes. Example: Student "is a" Person. Teacher "is a" Person.

What is polymorphism? - Definition

Polymorphism is where two methods have same name but different behaviour. Its of two types, Over ridding and Over loading.

What is encapsulation? - Definition

Encapsulation is hidding information with in an Object

What is association? - Definition

Association is a concept that enables relating one Object with another with a "Has A" relation. Example: Car "has a" wheel.

Subscribe
 
 

Java®,all Java-based marks are trademarks of Sun Microsystems,Inc. Any other trademarks belong to their respective owners.

 
 
 
TRAINING BY PROFESSIONALS
Private Java Training-JavaSprint
Experience the training by a live trainer for an hour
 
BLOGS ON JAVA TRAINING
Web Site Design and Development
Object Oriented Programming Concept with an Example
Exception Handling
The Story of JEE Versions
 
CONCEPT DIAGRAMS, JAVA
Arrays as in memory
Objects as in memory
 
 
 
 
Enquiry Enquiry Evaluate Yourself Demo