The Syntax of Object-Oriented Programming Languages
Comparative Programming Languages with Kristoffer Hebert
--
Overview
Every language mentioned in this article uses the class keyword. C++, Java, and Node.js have similar syntax for extending Classes. The same is true for instancing new classes. Python does not use extends syntax but uses the parent class as the first argument.
Extending Classes
Java and Node both use the extends keyword, when extending classes. Python passes the class as the first argument, since the first argument of a function is always self. C++ uses the colon syntax as it’s extension keyword.
// C++
class TodoApp : public Todo {…}// Java
public class TodoApp extends Todo {…}// Node.js
class TodoApp extends Todo {…}// Python
class TodoApp(Todo):
Instancing new classes
All the languages described use the class = new Class(args) syntax. C++ has multiple ways to instantiate a class, but the two most common have been provided. Python does not use the new keyword.
// C++
TodoApp todoapp(args);
TodoApp todoapp = new TodoApp(args);// Java
TodoApp todoApp = new TodoApp(args);// Node.js
let todoApp = new TodoApp(args);// Python
todo_app = TodoApp(args)
Protecting methods and variables
Java and C++ both have public and private declaration modifiers. Python and Node.js are not strict about public and private attributes, but have work language specific ways of achieving it.
// C++
class TodoApp {
private:
int privateValue
void privateFunction(){...} public:
void publicFunction(){...}}// Java
public class TodoApp extends Todo {
public static function {...}
private function {...}
}// Node.js
// Declare a function that is not a method of a Class
// Then call that function from the Class methodfunction __privateFunction(){...}class TodoApp {
publicFunction(){
__privateFunction()
}
}// Python
Class Todo():
___private_method(self):
...
public_method(self):
...
Examples
Provided is the same application coded in multiple languages.
C++
Java
Python
Example Object Oriented Applications
The following is a interactive Node.js Todo application, written for your terminal.