Introduction to Object Oriented Programming
Object Oriented Programming or OOP, as it is commonly known as is a very crucial aspect of modern day application development. Object oriented Programming is based on the foundations and uses of classes and objects (Clark and Sanders 2013). Classes can be defined as the design plans of blueprints of any program. Classes consist of all the required data members and the member methods, which encapsulate into one unit in order to resemble a real life entity. Classes can be used to produce several such similar entities of its own type, these are known as objects. The objects are there known as the real life instances of a class. Objects consists of data and member methods of their own. They inherit these characteristics and behaviors from their respective classes. These characteristic features for every object are also known as member variables because their values are updatable at any point of time for the particular object. The behavioral set for the objects are defined by the member methods that it contains. The methods or functions assist the objects to efficiently execute and perform different operations. It is said that the objects communicate among themselves with the help of methods. The methods helps to pass values within the object oriented programming paradigm, thus assisting the processing mechanism. These processed information are then either produced to the user or stored within the object’s instances (Dennis, Wixom and Tegarden 2015).
Clark and Sanders (2013), says Object Oriented Programming makes use of many features to enhance the programming and development experience. The major object oriented features are listed and explained in details below:
- Classes and Objects: As depicted before, classes characterize the information designs and the techniques while objects are made as the examples of such classes to take after genuine elements of the similar sort or type. A few attributes impart between each other utilizing the inward part behaviors or methods.
- Encapsulation and Data abstraction: Encapsulation is an extremely regular OOP idea that ties a few member data and member methods together into one unit known as the class. Wrapping of data and functions into a solitary unit is known as Encapsulation. These data and member methods are kept protected from the outer cases that may meddle with the information and the functionalities. The object oriented programming languages allow its classes to implement explicit access limitations such us to signify inner data as private or protected before the outer world. This goes under the pennant of the data abstraction ideas. Data abstraction, therefore can be defined as the process of providing only important information to the outside modules by hiding the inner implementation details (Milanesi 2018).
Example:
class Employee{
private int ssn;
private String empName;
private int empAge;
//Getter and Setter methods
public int getEmpSSN(){
return ssn;
}
public String getEmpName(){
return empName;
}
public int getEmpAge(){
return empAge;
}
public void setEmpAge(int newValue){
empAge = newValue;
}
public void setEmpName(String newValue){
empName = newValue;
}
public void setEmpSSN(int newValue){
ssn = newValue;
}
}
public class Office{
public static void main(String args[]){
Employee obj = new Employee ();
obj.setEmpName(“Mario”);
obj.setEmpAge(32);
obj.setEmpSSN(112233);
System.out.println(“Employee Name: ” + obj.getEmpName());
System.out.println(“Employee SSN: ” + obj.getEmpSSN());
System.out.println(“Employee Age: ” + obj.getEmpAge());
}
}
Output:
Employee Name: Mario
Employee SSN: 112233
Employee Age: 32
- Inheritance: Inheritance is the procedure by which a class inside a specific object oriented program bundle acquires certain individuals from a main or existing class. The child class as it is known as, get the available properties from another class, which is regularly known as the parent or the root class. This idea gives the various leveled arrangements or hierarchy of the classes and objects that are expected to connect with each other in order to execute a greater framework. Inheritance is one of the key highlights of OOP paradigm, which helps the programmers with the re-usage of existing code and in the meantime instantiate the various objects of classes with relevance to the hierarchical architecture. This proposes a module or data that exists in an earlier class can be added to another with no compelling reason to change it. This additionally decreases the space complexity of bigger projects as similar information isn’t being put away or introduced again and again. Rather, the data individuals are being initialized once in the parent class and are further being exquisitely utilized by the child classes by simply making occurrences of themselves (Zeigler 2014).
Examples:
class Teacher {
String designation = “Teacher”;
String collegeName = “Beginnersbook”;
void does(){
System.out.println(“Teaching”);
}
}
public class PhysicsTeacher extends Teacher{
String mainSubject = “Physics”;
public static void main(String args[]){
PhysicsTeacher obj = new PhysicsTeacher();
System.out.println(obj.collegeName);
System.out.println(obj.designation);
System.out.println(obj.mainSubject);
obj.does();
}
}
Output:
Beginnersbook
Teacher
Physics
Teaching
Source: (Java et al., 2018)
- Polymorphism: Polymorphism is the feature that enables anything to achieve more than just a single form. In Object oriented programming, the programs executes the idea of polymorphism to enable methods or functions of the same name to be written and used but with a varied sets of input types or arguments, numbers and functionalities. This conduct relies on the information kind of the contentions that are passed into a strategy. Polymorphism are of two kinds, Overloading and Overriding. Method overriding is defined as the process in which a function with the same name is defined in two different classes having the parent-child relationship. The striking feature of this is that both can be individually programmed to perform a different set of operations. This is executed at run time. Method overriding then again is a dynamic binding process. Here, similar methods that are composed in the parent class get re-written in the child classes and these are the ones that are executed rather than the parent class variants. This kind of polymorphism is just relevant in instances of inheritance. Method overriding helps to use specific class methods when it comes to a complex project execution with detailed hierarchical features.
Method overloading on the other hand is a static binding mechanism in object oriented programming. This ensures that functions can be written with the same name as long as they have a different set of arguments. These functions are so designed to perform differently depending of the set of arguments that are passed into them while calling. These functions get recognized during compile time and are therefore also known as Compile time polymorphism.
Example:
- Compile Time Polymorphism (Method Overloading)
class Overload
{
void demo (int a)
{
System.out.println (“a: ” + a);
}
void demo (int a, int b)
{
System.out.println (“a and b: ” + a + “,” + b);
}
double demo(double a) {
System.out.println(“double a: ” + a);
return a*a;
}
}
class MethodOverloading
{
public static void main (String args [])
{
Overload Obj = new Overload();
double result;
Obj .demo(10);
Obj .demo(10, 20);
result = Obj .demo(5.5);
System.out.println(“O/P : ” + result);
}
}
Output:
a: 10
a and b: 10,20
double a: 5.5
O/P : 30.25
- Runtime Polymorphism (Method Overriding)
public class Animal{
Classes and Objects
public void sound(){
System.out.println(“Animal is making a sound”);
}
}
Horse.java
class Horse extends Animal{
@Override
public void sound(){
System.out.println(“Neigh”);
}
public static void main(String args[]){
Animal obj = new Horse();
obj.sound();
}
}
Output:
Neigh
Cat.java
Some advantages of object oriented programming in software development include:
- Improved software-development productivity: Object oriented programming is a highly appreciated modular approach towards programming as it provides the separation of duties in object based program or software development. Objects are extensible to produce or inherit new characteristics and behavior. (Kelly 2016)This gives the programmers the opportunity to replicate real life instances in a more realistic fashion. Because of the three very important factors namely extensibility, modularity and reusability, OOP is appraised for providing improved software development productivity over the general or traditional procedural programming models.
- Improved software maintainability: Owing to the reasons and features stated above, the object oriented programming approach helps the software developers with better maintainability for the software they build. The design being modular, singular parts of the system can be taken down for repair or modifications while the other parts would still continue working. This averts the need to take down the whole system every time an update is required and also helps the developers and the concerned stakeholders to avoid large-scale changes.
- Inheritance and Modularity: Inheritance is one of the key concepts of OOP paradigm, which helps the software developers with the re-usage of existing code and in the meantime instantiate the various objects of classes with relevance to the hierarchical architecture (Mezini 2013). This allows the programmers to instantiate real life relationships between different classes. Liang and Tsai (2013), Explains that modularity is the concept of breaking down the programs into different modules that perform specific tasks each. This helps the developers to break down the application into various small sections and test each module separately to attain perfection (Cs.uccs.edu, 2018).
- Faster development: Reusing ability of the different modules within the program enables faster software development. The rich library classes and their components that the object oriented programming languages deliver help a great zeal in the software development phase (Derezi?ska and Rudnik 2012). This ability to store pre-compiled data in different classes for re-accessing them to build something new, is one of the key features that proves the worth of Object oriented programming.
- Lower cost of development: Since, reusability is a key feature of object oriented programming, programmers can reuse the pre-generated modules and therefore this reduces the timeframe of the software building, thus in turn lowering the cost of production.
- Higher-quality software: The faster development process and low cost production procedures that OOP provides, allows greater amount of time to invested into the verification of the being built system. This helps to produce quality products that are thoroughly tested and mended to hold strength at any circumstance.
The Room Carpet program will require the two main classes namely the RoomCarpet and the RoomDimension class. In addition, the Calculator class acts as the driver class for the program. It is a form page on the Graphics user Interface, where the user interacts with the system. Here the user enters the basic dimensional values for all the rooms and the objects are created and appended to an array of objects of the type RoomCarpet.
The calculator class has the roomIndex member variable that keeps track of the number of room objects created. This class calls the constructor of the RoomCarpet class when in need to create the object. This RoomCarpet class in turn has a RoomDimension class instance as its member variable. It uses this instance variable to create an object of the Room with the height and width that was passed into its arguments by the Calculator class. For each RoomCarpet object created, the calcArea() method of the RoomDimension is called to return and display the area of the concerned room.
Finally, when all the n-number of rooms are done creating and there are has been calculated, the Price form class is opened. For each room, the price is calculated and displayed. The user enters the price per room and the object calls the calcPrice() method in order to get the price of each room based on their area. Finally, a summation the price is made to generate the net price of the entire carpeting project of all the rooms.
Testing item |
Reason |
Expected Output |
Actual Output |
Success? |
Number of rooms = -65 |
To test if the system can handle negative room numbers |
Valid Error message |
The negative number is automatically converted to 0 and the pop-up is displayed. |
Yes |
Number of rooms = 0 |
To test if the system can handle 0 room numbers |
Valid error message |
Yes |
|
Number of rooms = 6 |
To check if the system identifies a valid input |
Enter the calculator part. |
Yes |
|
length = 0 width = 0 |
To test if the system can handle 0 dimension inputs |
Valid error message |
Yes |
|
Length = 9 Width = 8 |
To check if the system handles integer inputs properly |
No error |
Yes |
|
Length = 9.5 Width = 7.8 |
To check if the system handles decimal figure inputs properly |
No error message |
Yes |
|
Price = 0 |
To test if system can handle 0 value for price |
Valid error message |
Yes |
|
Price = 3.0 |
To test valid input |
No error |
Yes |
Testing item |
Reason |
Expected Output |
Actual Output |
Success? |
Number of rooms =3 |
To test if 3 room’s entries are taken one by one. |
Stops after 3 room inputs and proceeds to price page. |
Yes |
|
Length = 6.0 Width = 5.50 |
To check if area is calculated properly |
Area = 23 |
Yes |
|
Price = 15.25 |
To check if Price is calculated properly and all room prices are individually displayed |
Net price = 1330.50 |
Yes, the price value is displayed properly. However, the output box to display the net price seems to be falling short of space. Design Flaw |
The users are required to run the RoomCarpetApplication.exe file in order to use the application. They will be greeted by the opening screen that will ask them to enter the number of rooms that they wish to calculate area and price for. Furthermore, they will enter the Length and Width for each room and calculate the area of the rooms by clicking the Calculate Area button. The Area will be displayed below.
On completion of the dimension entry process, the user will have to enter the price per meter square and then get the net price of all the rooms. They will also be able to view the price for each room in another text box.
The users will be given the option to quit the system once the price calculation is done or they can also restart the system and start with new values to calculate.
References
Clark, D. and Sanders, J., 2013. Beginning C# object-oriented programming. Apress.
Cs.uccs.edu. (2018). [online] Available at: https://www.cs.uccs.edu/~qyi/UTSA-classes/cs3723/slides/module-object.pdf [Accessed 7 Jun. 2018].
Dennis, A., Wixom, B.H. and Tegarden, D., 2015. Systems analysis and design: An object-oriented approach with UML. John wiley & sons.
Derezi?ska, A. and Rudnik, M., 2012, May. Quality evaluation of object-oriented and standard mutation operators applied to C# programs. In International Conference on Modelling Techniques and Tools for Computer Performance Evaluation(pp. 42-57). Springer, Berlin, Heidelberg.
Java, C., I/O, J., Tutorial, C. and Singh, C. (2018). Inheritance in Java Programming with examples. [online] beginnersbook.com. Available at: https://beginnersbook.com/2013/03/inheritance-in-java/ [Accessed 7 Jun. 2018].
Kelly, S., 2016. Object-Oriented Programming. In BlitzMax for Absolute Beginners (pp. 101-114). Apress, Berkeley, CA.
Kulak, D. and Guiney, E., 2012. Use cases: requirements in context. Addison-Wesley.
Larman, C., 2012. Applying UML and Patterns: An Introduction to Object Oriented Analysis and Design and Interative Development. Pearson Education India.
Lengstorf, J. and Wald, K., 2016. Object-Oriented Programming. In Pro PHP and jQuery (pp. 85-114). Apress, Berkeley, CA.
Liang, Y.D. and Tsai, M.J., 2013. Introduction to Java programming: brief version. Pearson.
Madduri, V., Singh, C., Singh, C. Java, C., I/O, J., Tutorial, C., Singh, C., m, j., and kumar, A. (2018). Polymorphism in Java with example. [online] beginnersbook.com. Available at: https://beginnersbook.com/2013/03/polymorphism-in-java/ [Accessed 7 Jun. 2018].
Mezini, M., 2013. Variational Object-Oriented Programming Beyond Classes and Inheritance (Vol. 470). Springer Science & Business Media.
Milanesi, C., 2018. Object-Oriented Programming. In Beginning Rust (pp. 273-294). Apress, Berkeley, CA.
Singh, C., vardhan, H., Dickinson, R., raghuwanshi, a., Singh, R., Patel, k., Ghetiya, K., Singh, C. and Java, C., I/O, J., Tutorial, (2018). Encapsulation in Java with example. [online] beginnersbook.com. Available at: https://beginnersbook.com/2013/05/encapsulation-in-java/ [Accessed 7 Jun. 2018].
Smith, B., 2015. Object-oriented programming. In Advanced ActionScript 3 (pp. 1-23). Apress, Berkeley, CA.
Zeigler, B.P., 2014. Object-oriented simulation with hierarchical, modular models: intelligent agents and endomorphic systems. Academic press.