Home > Free Essays > Tech & Engineering > Programming > Object-oriented Programming. Java and C++ Programming

Object-oriented Programming. Java and C++ Programming Essay

Exclusively available on IvyPanda Available only on IvyPanda
Updated: Sep 11th, 2022

Introduction

Object-oriented (OO) programming has become an essential approach to program organization for nearly all current software development. This approach tries to eliminate several challenges experienced in conventional programming paradigm by integrating the best features of structured programming with powerful new concepts. The features or concepts of OO include classes, methods, objects, inheritance, polymorphism, and dynamic binding (Jana, 2005).

Besides, software engineers are required to understand the languages that are suitable for object orientation since not all languages can implement the OO programming concepts. Java and C++ are examples of languages that can be used to implement OO features and techniques. However, there exist some differences in the implementation of OO features, with Java being the most efficient in this context. Therefore, this paper seeks to present a comparative analysis of the two languages with regards to OO paradigm.

Classes and Methods

Classes are the abstract representation of objects because they depict the data attributes and methods that implement those data. In other words, classes are used to implement the concept of data abstraction and encapsulation. In Java, all the program implementation must be in a class. C++ accepts codes outside a class with the help of global functions which are not required in Java. In addition, Java requires a programmer to define methods within the body of a class. This is not the case in C++ since functions, even if in-lined, are defined outside the class (Eckel, 2006). The following language rules show how classes and methods are handled by the two languages.

C++:

class class_name {

private:

Data and Methods

public:

public data and Methods

}; //Methods are implemented outside the class using class_name.method_name()

Java:

class class_name {

//member variables declarations

//method definitions

//method implementation

}

Objects

Objects are the occurrences of a class. They are the core feature of OO standard since programming problems are viewed in terms of objects and the way they communicate. Deitel (2004) explains that objects can communicate without having to know the internal facts of the other objects, hence the concept of abstraction. Proper definition and implementation of objects is the key to successful OO programming. A software engineer is required to understand how objects are treated in Java and C++. Technically, a class definition, as explained in the previous section creates a new data type. Thus, to create an object in C++, let’s say an item object called bag, we use a declaration statement in the main() function, such as: item bag;

In Java, objects are created and used within the class, making use of the new keyword to allocate memory to objects (Oracle, 2011). The following statement is the general syntax for creating an object in Java:

class_name object_name = new constructor (Paramenter list);

The new keyword is important in Java because it helps in the elimination of pointers which are used in C++. When new is used in Java, a reference is created, unlike C++ which requires the initialization of references using pointers. The references in C++ cannot be reused in a different location as it is common in Java. In addition, Java codes require no pointers because there are no global functions, rather classes allows a programmer to pass references to objects.

Moreover, Martin (1997) outlines that garbage collection in the light of object use, enables Java programmers to use memory efficiently. A null keyword can be assigned to an object to show that its lifetime is complete, thus enabling the Java runtime system to restore the memory used by the object. C++ lacks this capability, but many designers have implemented their own ways of garbage collection.

Inheritance

Inheritance is a technique by which classes share properties from other classes. It describes the process of hierarchical classification. Each derived class assumes the properties of its parent class. In OO programming, the idea of inheritance is important in realizing the notion of re-usability. This implies that additional properties can be added to an existing class without modifying it (Deitel & Deitel, 2009).

Java and C++ employ inheritance in different forms, but the idea is the same. Java requires the use of extends keyword to depict inheritance from a parent class, and super keyword to access methods in the parent class which normally precedes the subclass. Technically, Java uses multilevel (single) inheritance for classes. The following is the format:

public class Square extends shape

{

public Square (double d1) {

super(d1); //calls base constructor

public double area(int dim) {//override

super.area(dim); //calls base method }

}

In the case of C++, there can be multiple or single inheritance with the help of visibility modes which are not supported by Java. Visibility mode can be public, private, or protected. Any overriding cannot change the accessibility of the inherited method like in the case of Java. The general syntax for inheritance in C++ is:

class derived_classname:visibility mode base_classname

{//members of derived class}

However, implementing multiple inheritances in Java require the incorporation of interfaces. Martin (1997) states that a Java interface is nearly similar to a C++ class with virtual functions. Interfaces are not treated like classes and thus they cannot be used to implement methods. Several definitions of interfaces do to result to the drawbacks that led to the incorporation of “virtual” inheritance in C++. Thus, interfaces avoid the need for virtual base classes. Eckel (2002) suggests that to instantiate an interface, the keyword implements is used, such as the one below:

public interface Shape {

public void area(); }

public class Rectangle extends Big implements Shape {

public void area( ) {

System.out.println(“area is length*width”); } }

Dynamic Binding

Dynamic binding is the process of executing code associated to a certain method at run-time, during which the method is called. In C++, dynamic binding is implemented using virtual keyword. This is not the case for Java because all the functions which are not static always employ dynamic binding. The use of virtual functions is important in C++ because they help in implementing polymorphism. In C++, a virtual function is a member function declared as virtual and later overridden in subsequent inheritances (Deitel & Deitel, 2009).

Polymorphism

Polymorphism in object orientation is the ability of objects of different types to share the same properties. It is also used in creating inheritance since the concept of re-usability is essential in OO paradigm. Polymorphism is realized in two categories: operator overloading and function overloading. C++ overloads many operators, including +, !, >, &, %, ==, and +=. In Java, the essence of operators is technically absolute, but the + and += operators can be overloaded in Strings (Deitel & Deitel, 2009).

In function overloading, C++ uses the virtual keyword, as explained in the previous section, to implement polymorphism. For instance:

class A

{

public: virtual void see()

};

class B: public A

{

public: void see()

};

Again, Java’s methods are all virtual, and there is no need for keywords to implement polymorphism.

Conclusion

Understanding the different formats of program organization and presentation is important in software engineering. This paper has compared the two common OO programming languages; particularly Java and C++. From the discussion it is clear that both languages employ OO techniques in different forms. These techniques or concepts include classes, methods, objects, inheritance, dynamic binding, and polymorphism. Depending on the language chosen, any good programmer can come up with software systems which embrace the concepts of object orientation.

References

Deitel, D. (2004). C++ How to Program. (4th Ed). New York: Prentice Hall.

Deitel, P. & Deitel, H. (2009). Java for Programmers. New York: Prentice Hall.

Eckel, B. (2006). Thinking in Java. (4th Ed.). New York: Prentice Hall.

Jana, D. (2005). Object-Oriented Programming Paradigm. (2nd Ed.). New Delhi: Prentice-Hall India Pvt. Ltd.

Martin, R.C. (1997). Java and C++: A Critical Comparison. Web.

Oracle. (2011). Learning the Java Language. The Java Tutorials. Web.

This essay on Object-oriented Programming. Java and C++ Programming was written and submitted by your fellow student. You are free to use it for research and reference purposes in order to write your own paper; however, you must cite it accordingly.
Removal Request
If you are the copyright owner of this paper and no longer wish to have your work published on IvyPanda.
Request the removal

Need a custom Essay sample written from scratch by
professional specifically for you?

801 certified writers online

Cite This paper
Select a referencing style:

Reference

IvyPanda. (2022, September 11). Object-oriented Programming. Java and C++ Programming. https://ivypanda.com/essays/object-oriented-programming-java-and-c-programming/

Reference

IvyPanda. (2022, September 11). Object-oriented Programming. Java and C++ Programming. Retrieved from https://ivypanda.com/essays/object-oriented-programming-java-and-c-programming/

Work Cited

"Object-oriented Programming. Java and C++ Programming." IvyPanda, 11 Sept. 2022, ivypanda.com/essays/object-oriented-programming-java-and-c-programming/.

1. IvyPanda. "Object-oriented Programming. Java and C++ Programming." September 11, 2022. https://ivypanda.com/essays/object-oriented-programming-java-and-c-programming/.


Bibliography


IvyPanda. "Object-oriented Programming. Java and C++ Programming." September 11, 2022. https://ivypanda.com/essays/object-oriented-programming-java-and-c-programming/.

References

IvyPanda. 2022. "Object-oriented Programming. Java and C++ Programming." September 11, 2022. https://ivypanda.com/essays/object-oriented-programming-java-and-c-programming/.

References

IvyPanda. (2022) 'Object-oriented Programming. Java and C++ Programming'. 11 September.

Powered by CiteTotal, best citing machine
More related papers