Tutor HuntResources Computing Resources

Object Oriented Programming Concept With An Example

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.

Date : 15/11/2013

Author Information

Bharat

Uploaded by : Bharat
Uploaded on : 15/11/2013
Subject : Computing

In a program, you create an object to represent every thing in the real world and establish relationships between them. Hence you recreate the world in Computer`s memory. 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.

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.

This resource was uploaded by: Bharat

Other articles by this author